What is the difference between var_dump() and print_r() in terms of spitting out an array as string?
What is the difference between var_dump() and print_r() in terms of spitting out an array as string?
It's too simple. The var_dump() function displays structured information about variables/expressions including its type and value. Whereas The print_r() displays information about a variable in a way that's readable by humans.
Example: Say we have got the following array and we want to display its contents.
$arr = array ('xyz', false, true, 99, array('50'));
Array
(
[0] => xyz
[1] =>
[2] => 1
[3] => 99
[4] => Array
(
[0] => 50
)
)
array(5) {
[0]=>
string(3) "xyz"
[1]=>
bool(false)
[2]=>
bool(true)
[3]=>
int(100)
[4]=>
array(1) {
[0]=>
string(2) "50"
}
}
For more details: https://stackhowto.com/how-to-display-php-variable-values-with-echo-print_r-and-var_dump/
var_dump() :-
Example :-
<?php
$a = array(1, 2, array("a", "b", "c"));
var_dump($a);
?>
output :-
array(3) {
[0]=>
int(1)
[1]=>
int(2)
[2]=>
array(3) {
[0]=>
string(1) "a"
[1]=>
string(1) "b"
[2]=>
string(1) "c"
}
}
print_r() :-
Example :-
<pre>
<?php
$a = array ('a' => 'apple', 'b' => 'banana', 'c' => array ('x', 'y', 'z'));
print_r ($a);
?>
</pre>
Output:-
<pre>
Array
(
[a] => apple
[b] => banana
[c] => Array
(
[0] => x
[1] => y
[2] => z
)
)
</pre>
We can pass multiple parameters with var_dump like:
var_dump("array1",$array1,"array2",$array2);
function test_dump (...$params)
{
$file_test_dump = 'test_dump.log';
$backtrace = debug_backtrace(0, 3);
$caller_file = $backtrace[0]['file']);
$caller_function = $backtrace[1]['function'];
$caller_line = $backtrace[0]['line'];
if (empty($params))
{
file_put_contents ($file_test_dump, "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n" , FILE_APPEND);
file_put_contents ($file_test_dump, '!!!!!! ' . date ("[Y-m-d H:i:s:u]", time()) . " - FILE: {$caller_file} | FUNCTION: {$caller_function} | LINE: {$caller_line} !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! \n\n" , FILE_APPEND);
file_put_contents ($file_test_dump, "!!! No parameter given !!! \n\n" , FILE_APPEND);
return;
}
// --- get source file ---
$source_file = file($backtrace[0]['file']);
// --- get line of source file and pre filter with regex ---
preg_match ('~\(([^*]*)\)~', $source_file [intval($backtrace[0]['line']) - 1], $parameter_line);
$parameter_line_filtered = str_replace(" ", "", $parameter_line [1]);
// put parameter names into array
$params_names = explode(',', $parameter_line_filtered);
// --- OUTPUT ----
file_put_contents ($file_test_dump, "#######################################################################################################################################################################################################################\n" , FILE_APPEND);
file_put_contents ($file_test_dump, '### ' . date ("[Y-m-d H:i:s:u]", time()) . " - FILE: {$caller_file} | FUNCTION: {$caller_function} | LINE: {$caller_line} ### \n\n" , FILE_APPEND);
$i = 0;
foreach ($params as $param)
{
$i++;
file_put_contents ($file_test_dump, " --- Parameter " . $i . ': ' . $params_names[$i - 1] . " --------------------------------------------------------------------------------------------------------------------------\n", FILE_APPEND);
if ( is_array($param) ||
is_object($param) ||
is_bool($param) )
{
ob_start();
var_dump($param);
file_put_contents ($file_test_dump, ob_get_contents() . "\n\n", FILE_APPEND);
ob_end_clean();
}
else
{
file_put_contents ($file_test_dump, $param . "\n\n", FILE_APPEND);
}
}
file_put_contents ($file_test_dump, "\n\n", FILE_APPEND);
}