Thursday, September 11, 2008

The print_r() function

We know that to print individual elements of an array, the echo() of the print() function could be used.
However, to print an array as such, we must use the print_r() function.
Syntax: print_r($expression, $return);

$expression the expression to be printed.
$return
if this parameter is set to TRUE, the print_r() function would return its output to a variable.
if this parameter is FALSE, the output would be printed. FALSE is the default value.

Also, remember that the print_r() function is not used only for printing arrays, but also for any mixed expressions.
Example input:
1.
$employee = array(array('John', 223,42),
array('Daisy',443,50),
array('Mathew',990,37)
);
print_r($employee);
echo "
";
print_r($sum = 100 + 300);?
2.?
$employee = array(array('John', 223,42),
array('Daisy',443,50),
array('Mathew',990,37)
);
print_r($employee,true);
echo "
";
echo $str;
Example output:
1.
Array ( [0] => Array ( [0] => John [1] => 223 [2] => 42 ) [1] => Array ( [0] => Daisy [1] => 443 [2] => 50 ) [2] => Array ( [0] => Mathew [1] => 990 [2] => 37 ) ) ?400??
2.
Array ( [0] => Array ( [0] => John [1] => 223 [2] => 42 ) [1] => Array ( [0] => Daisy [1] => 443 [2] => 50 ) [2] => Array ( [0] => Mathew [1] => 990 [2] => 37 ) )

No comments: