exchange the values in two variables without using additional variables for storage?
PMA01:59
To swap $a and $b:
list($a,$b) = array($b,$a);
how associate multiple elements with a single key?
PMA00:26
Store the multiple elements in an array:
$fruits = array('red' => array('strawberry','apple'), 'yellow' => array('banana'));
Or, use an object:
while ($obj = mysql_fetch_object($r)) { $fruits[ ] = $obj; }
Php get_meta_tags-Extracts all meta tag content attributes
PMA00:10
get_meta_tags — Extracts all meta tag content attributes from a file and returns an array
<?php// Assuming the above tags are at www.example.com$tags = get_meta_tags('http://www.example.com/');
// Notice how the keys are all lowercase now, and
// how . was replaced by _ in the key.echo $tags['author']; // nameecho $tags['keywords']; // php documentationecho $tags['description']; // a php manualecho $tags['geo_position']; // 49.33;-86.59?>
PHP Redirect - Redirect Script?
PMA00:07
PHP Redirect Function
header('Location: destination.php'); exit();
ou need the Location:
part so the browser knows what header it's
receiving. Also, don't forget to
do anexit()
ordie()
right after the redirect.
Finding the Position of a Value in an Array
PMA05:54
Use array_search( ) . It
returns the key of the found value. If the value is not in the array, it returns
false:
$position = array_search($value, $array); if ($position !== false) { // the element in position
//$position has $value as its
//value in array $array }
Use in_array( ) to find if an
array contains a value; use array_search( ) to discover where that value
is located. However, because array_search( ) gracefully handles searches in which the value isn't
found, it's better to use array_search( ) instead of in_array( ).
The speed difference is minute, and the extra information is potentially
useful:
$food = 'cauliflower'; $position = array_search($food, $favorite_foods);
if ($position !== false)
{
echo "My #$position favorite food is $food";
}
else {
echo "Blech! I hate $food!";
}
Use the !== check against false because if your
string is found in the array at position 0, the if evaluates
to a logical false, which isn't what is meant or wanted.
If a value is in the array multiple times, array_search(
) is only guaranteed to return one of the instances, not the first
instance.