php var_dump equivelant or print_r - php

since I am using json, and ajax it's annoting I cn't pass the value on a valid json.
is there away to just return the value of var dump without it echo,being output to the browser.
e.g.
$data = 'my_data';
get_var_dump($data);//not real func
//should do nothing.
$get['data'] = get_var_dump($data);
$get['error']= false;
echo json_encode($get);
//should be something like
//{"data,"string(7) my_data","error":false}
or the print_r equivalent I just want to to be assigned to a var instead of outputting it.
or if ur a wordpress fan, difference between bloginfo('url'); and get_bloginfo('url'); should make it easier :)

Sure you can! To do that you will need two PHP buffer functions: ob_start and ob_get_clean. The first one starts buffering, when the second is getting value and cleaning buffer. Example:
ob_start();
var_dump($array);
$value = ob_get_clean();

print_r has the option for a second parameter. When set to true, it returns the dump as an array instead of displaying it.
http://us2.php.net/print_r

Check the var_export() function:
http://php.net/manual/en/function.var-export.php
You pass a variable to it and a second boolean parameter, if the second parameter is true the functions return a string with a rapresentation of the variable:
<?php
$a = array(1, 2);
$dump = var_export($a, true);
print $dump;
?>
$dump contains something like
array (
0 => 1,
1 => 2,
)

Everybody has beaten me to it: var_export()

Related

Why XSLT Transformation Output has trailing number? [duplicate]

My print_r($view) function yields:
View Object
(
[viewArray:View:private] => Array
(
[title] => Projet JDelage
)
)
1 <--------------
What does the "1" at the end mean? The PHP manual isn't very clear on how to parse the output of print_r.
You probably have echo print_r($view). Remove the echo construct. And... what need do you have to parse its output? There are certainly much better ways to solve your problem.
print_r called with one argument (or with its second argument set to false), will echo the representation of its parameter to stdout. If it does this, it returns TRUE. Thus if you echo print_r($foo) you will print the contents of foo, followed by a string representation of the return value (which is 1).
When using print_r to return, than than output/print the value, pass the 2nd parameter which defines return as true.
echo print_r($view, true);
This is useful if you want to save the results to a variable or concatenate with another string.
$var = 'The array is: ' . print_r($view, true);

A character 1 after every value I get from the database using CodeIgniter [duplicate]

My print_r($view) function yields:
View Object
(
[viewArray:View:private] => Array
(
[title] => Projet JDelage
)
)
1 <--------------
What does the "1" at the end mean? The PHP manual isn't very clear on how to parse the output of print_r.
You probably have echo print_r($view). Remove the echo construct. And... what need do you have to parse its output? There are certainly much better ways to solve your problem.
print_r called with one argument (or with its second argument set to false), will echo the representation of its parameter to stdout. If it does this, it returns TRUE. Thus if you echo print_r($foo) you will print the contents of foo, followed by a string representation of the return value (which is 1).
When using print_r to return, than than output/print the value, pass the 2nd parameter which defines return as true.
echo print_r($view, true);
This is useful if you want to save the results to a variable or concatenate with another string.
$var = 'The array is: ' . print_r($view, true);

On echo return string, on print_r return array or object in PHP

Some functions I have used return a string on echo, something like this.
echo my_function();
This prints "hello" on the screen.
print_r( my_function() );
This prints an array or an object on the screen. The function is the same in both cases, even the possible in parameters.
How is this done?
The possible function
function my_function() {
// If this function is used by echo return the string
// If this function is used by print_r return an array
$string = 'Hello';
$array = array('some', 'data');
// return $string or $array depending on
}
Real life example
This function return an image on echo and an object on print_r: http://getkirby.com/docs/cheatsheet/helpers/thumb
Your my_function() is not returning anything. So calling it in echo or print_r makes no diffrence. If you return string form the function and you want to just display it then you can echo it out. But if you return array from function and echo it then it will give you a unusful output. On the other hand if you print_r the function and function return string, then it will output just like echo do. But if you return array and call with print_r then it will show the array more readable. It is useful for debugging.
Here is more about echo and print_r()
echo
Outputs one or more strings separated by commas
No return value
print_r()
Outputs a human-readable representation of any one value
Accepts not just strings but other types including arrays and objects, formatting them to be readable
Useful when debugging
May return its output as a return value (instead of echoing) if the second optional argument is given
Functions should never depend on who they called, but how they are called.
There is no sense in for example creating a function that does addition on echo and subtraction on print. That is confusing.
try to use parameters as suggested in the question's comments.

putting print_r results in variable

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
When the second parameter is set to
TRUE, print_r() will return the
information rather than print it
$var_info = print_r($var,true);
$var_info = print_r($var, true);

What does "1" mean at the end of a php print_r statement?

My print_r($view) function yields:
View Object
(
[viewArray:View:private] => Array
(
[title] => Projet JDelage
)
)
1 <--------------
What does the "1" at the end mean? The PHP manual isn't very clear on how to parse the output of print_r.
You probably have echo print_r($view). Remove the echo construct. And... what need do you have to parse its output? There are certainly much better ways to solve your problem.
print_r called with one argument (or with its second argument set to false), will echo the representation of its parameter to stdout. If it does this, it returns TRUE. Thus if you echo print_r($foo) you will print the contents of foo, followed by a string representation of the return value (which is 1).
When using print_r to return, than than output/print the value, pass the 2nd parameter which defines return as true.
echo print_r($view, true);
This is useful if you want to save the results to a variable or concatenate with another string.
$var = 'The array is: ' . print_r($view, true);

Categories