Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making PHP var_dump() values display one line per value

Tags:

php

var-dump

line

When I echo var_dump($_variable), I get one long, wrapping line with all varable's and values like

["kt_login_user"]=>  string(8) "teacher1" ["kt_login_id"]=>  string(3) "973" ["kt_campusID"]=>  string(4) "9088" ["kt_positionID"]=>  string(1) "5"  

Is there a way I can make each value display on its own line for ease of reading? Something like this:

["kt_login_user"]=>  string(8) "teacher1"  ["kt_login_id"]=>  string(3) "973"  ["kt_campusID"]=>  string(4) "9088"  ["kt_positionID"]=>  string(1) "5" 
like image 770
user1320318 Avatar asked Apr 12 '12 00:04

user1320318


People also ask

What value will Var_dump show?

The var-dump is display the datatype as well as value while echo only display the value. Explanation: In Php the var-dump function is used for displaying the datatype as well the value . The var-dump () does not return anythings it means there is no datatype of var-dump() function.

Why Var_dump () is preferable over Print_r ()?

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.

What is the difference between Var_dump () and Print_r ()?

var_dump() displays values along with data types as output. print_r() displays only value as output. It does not have any return type. It will return a value that is in string format.

Which is true about Var_dump () function?

4. Which is true about var_dump() function? Explanation: var_dump() cuts off loop after getting the same element three times is true about var_dump() function.


2 Answers

Yes, try wrapping it with <pre>, e.g.:

echo '<pre>' , var_dump($variable) , '</pre>'; 
like image 146
phirschybar Avatar answered Sep 28 '22 10:09

phirschybar


I usually have a nice function to handle output of an array, just to pretty it up a bit when debugging.

function pr($data) {     echo "<pre>";     print_r($data); // or var_dump($data);     echo "</pre>"; } 

Then just call it

pr($array); 

Or if you have an editor like that saves snippets so you can access them quicker instead of creating a function for each project you build or each page that requires just a quick test.

For print_r:

echo "<pre>", print_r($data, 1), "</pre>"; 

For var_dump():

echo "<pre>", var_dump($data), "</pre>"; 

I use the above with PHP Storm. I have set it as a pr tab command.

like image 30
Dave Avatar answered Sep 28 '22 08:09

Dave