php print array values
php print array values
1 Answer
In case you have an array and you want to print the array values
You can use one of these functions print_r, var_dump, var_export
Each of them has different uses
print_r Function
- Show data in an understandable form of humans
<?php
$array = [1,2,3,4,5,6,7,8,9];
print_r($array);
?>
output
Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 [5] => 6 [6] => 7 [7] => 8 [8] => 9 )
var_dump Function
- Show structured information includes its type and value
- Show protected data
- Show private data
<?php
$array = [1,2,3,4,5,6,7,8,9];
var_dump($array);
?>
output
array(9) { [0]=> int(1) [1]=> int(2) [2]=> int(3) [3]=> int(4) [4]=> int(5) [5]=> int(6) [6]=> int(7) [7]=> int(8) [8]=> int(9) }
var_export Function
- Show structured information
- Similar to var_dump()
<?php
$array = [1,2,3,4,5,6,7,8,9];
var_export($array);
?>
output
array ( 0 => 1, 1 => 2, 2 => 3, 3 => 4, 4 => 5, 5 => 6, 6 => 7, 7 => 8, 8 => 9, )
answer Link