Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

putting print_r results in variable

Tags:

php

In PHP I can say:

print_r($var); 

What I can't do (at least I don't know how) is:

$var_info = print_r($var); 

How can I accomplish the effect of putting the results of a print_r inside a variable?
PHP v5.3.5

like image 515
Web_Designer Avatar asked Apr 23 '11 05:04

Web_Designer


People also ask

Where do you use Print_r () statement?

It is a built-in function in print_r in PHP that is used to print or display the contents of a variable. It essentially prints human-readable data about a variable. The value of the variable will be printed if it is a string, integer, or float.

What does Print_r return?

print_r() function will return the output which it is supposed to print if this parameter is set to TRUE.

What is the difference between Print_r and Var_dump?

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.

What is difference between Print_r and echo in PHP?

The print and echo are both language constructs to display strings. The echo has a void return type, whereas print has a return value of 1 so it can be used in expressions. The print_r is used to display human-readable information about a variable.


1 Answers

When the second parameter is set to TRUE, print_r() will return the information rather than print it

$var_info = print_r($var,true); 
like image 123
Shakti Singh Avatar answered Sep 29 '22 08:09

Shakti Singh