13.09.2016 Views

PHP and MySQL Web Development 4th Ed-tqw-_darksiderg

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

106 Chapter 3 Using Arrays<br />

Table 3.2<br />

Type<br />

Continued<br />

Meaning<br />

EXTR_PREFIX_IF_EXISTS<br />

EXTR_REFS<br />

Creates a prefixed version only if the nonprefixed version<br />

already exists.<br />

Extracts variables as references.<br />

The two most useful options are EXTR_OVERWRITE (the default) <strong>and</strong> EXTR_PREFIX_ALL.<br />

The other options might be useful occasionally when you know that a particular<br />

collision will occur <strong>and</strong> want that key skipped or prefixed. A simple example using<br />

EXTR_PREFIX_ALL follows.You can see that the variables created are called prefixunderscore-keyname:<br />

$array = array( ‘key1’ => ‘value1’, ‘key2’ => ‘value2’, ‘key3’ => ‘value3’);<br />

extract($array, EXTR_PREFIX_ALL, ‘my_prefix’);<br />

echo “$my_prefix_key1 $my_prefix_key2 $my_prefix_key3”;<br />

This code again produces the following output:<br />

value1 value2 value3<br />

Note that for extract() to extract an element, that element’s key must be a valid variable<br />

name, which means that keys starting with numbers or including spaces are skipped.<br />

Further Reading<br />

This chapter covers what we believe to be the most useful of <strong>PHP</strong>’s array functions.We<br />

have chosen not to cover all the possible array functions.The online <strong>PHP</strong> manual available<br />

at http://www.php.net/array provides a brief description for each of them.<br />

Next<br />

In the next chapter, you learn about string processing functions.We cover functions that<br />

search, replace, split, <strong>and</strong> merge strings, as well as the powerful regular expression functions<br />

that can perform almost any action on a string.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!