|
| HOW TO VIEW ARRAYS
You can view the structure if an array using either one of two functions. These are print_r() and var_dump().
These are further explained below:
Assume we have an array created as follows:
$fruits=array('apple', 'banana', 'orange', 'peach');
View Using print_r():
echo "<pre>";
print_r($fruits);
echo "</pre>";
will output to your screen:
Array
(
[0] => apple
[1] => banana
[2] => orange
[3] => peach
)
Note that if you omit <pre> and </pre>, you will get the output in a long line.
View Using var_dump():
echo "<pre>";
var_dump($fruits);
echo "</pre>";
will output to your screen:
array(4) {
[0]=>
string(5) "apple"
[1]=>
string(6) "banana"
[2]=>
string(6) "orange"
[3]=>
string(5) "peach"
}
Note that if you omit <pre> and </pre>, you will get the output in a long line. You would have noticed that the var_dump() gives you a lot more information than the print_r().
The (4) next to the word array tells you that there are 4 elements in this array. The elements keys and values are then listed one below the other. For each of the
elements, the number between [ ] is the key, string states that the value is a string, the number between ( ) after the word string tells you how many characters that string contains. The actual value is between " ".
See also:
|
|