Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Store print_r result into a variable as a string or text

Tags:

php

People also ask

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 purpose of Print_r ()? *?

The print_r() function prints the information about a variable in a more human-readable way.

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 ECHO and Print_r 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.


   $var = print_r($what, true);

You must add true into print_r.


What you do while you print or dump? Basically you send your data (result or anything) to Show it on screen. Keep your mind clear that its not saved, it is just displayed, To save the data , so a simple thing, just declare a variable and assign the data to it..

for example you are printing some array like this..

print_r(myArray);

to save this, you just have to add an option , set Return to TRUE and assign it to a variable

$myVariable=print_r(myArray, TRUE);

if you need some more information, Follow this

hoping this will help you understanding the concept


ob_start();
var_dump($someVar);
$result = ob_get_clean();

it works.