how execute different code depending on the number and type of arguments passed to a method?
PMA02:06
PHP doesn't support method polymorphism as a built-in feature.
However, you can emulate it using various type-checking functions. The following
combine( ) function uses is_numeric(),
is_string(), is_array(), and is_bool():
// combine() adds numbers, concatenates strings, merges arrays, // and ANDs bitwise and boolean arguments function combine($a, $b) { if (is_numeric($a) && is_numeric($b)) { return $a + $b; } if (is_string($a) && is_string($b)) { return "$a$b"; } if (is_array($a) && is_array($b)) { return array_merge($a, $b); } if (is_bool($a) && is_bool($b)) { return $a & $b; } return false; }
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; }