How do I properly use print_r or var_dump?

痞子三分冷 提交于 2019-11-30 07:25:06

var_dump always show you array in formatted data but too much extra stuff

var_dump($data);

but if you want formatted data here you need to use <pre> tags

echo '<pre>';
print_r($data);
echo '</pre>';

var_dump() echos output directly, so if you want to capture it to a variable to provide your own formatting must use output buffers:

   ob_start();
   var_dump($var);
   $s = ob_get_clean();

Once this is done the variable $s now contains the output of var_dump(), so can safely:

   echo "<pre>" . $s . "</pre>";

var_dump is used when you want the more detail about any variable .

<?php 
    $temp = "hello" ;
    echo var_dump($temp);
    ?>

it output as follows string(5) "hello" means it print the data type of variable and length of string and what is the content in the variable.

while print_r($expression) is used for printing the data like array or any other object data type which can not directly printed by echo statement.

Siki

Well, print_r() is used to print and array, but in order to display the array in a pretty way you also need html tags.

Just do the following:

echo "<pre>";
print_r($data);
echo "</pre>";
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!