Microsoft Word - Core PHP Programming Using PHP to Build Dynamic Web Sites

(singke) #1

print(current($colors));
?>


array explode(string delimiter, string data)


The explode function creates an array from a string. The delimiter argument divides
the data argument into elements. This function is safe for use with binary strings. The
implode function will convert an array into a string.


<?
/*
* convert tab-delimited list into an array
/
$data = "red\tgreen\tblue";
$colors = explode("\t", $data);


// print out the values
for($index=0; $index < count($colors); $index++)
{
print("$index : $colors[$index]
\n");
}
?>


extract(array variables, integer mode, string prefix)


The extract function creates variables in the local scope based on elements in the
variables argument. Elements not indexed by strings are ignored. The optional mode
argument controls whether variables overwrite existing variables or are renamed to avoid
a collision. The valid modes are listed in Table 9.1. If left out, EXTR_OVERWRITE mode is
assumed. The prefix argument is required only if EXTR_PREFIX_SAME or
EXTR_PREFIX_ALL modes are chosen. If used, the prefix argument and an underscore are
added to the name of the extracted variable.


Compare this function to compact, which creates an array based on variables in the local
scope.


<?
$new_variables = array('Name'=>'Leon', 'Language'=>'PHP');


$Language = 'English';


extract($new_variables, EXTR_PREFIX_SAME, "collision");


//print extracted variables
print($Name. "
\n");
print($collision_Language. "
\n");

Free download pdf