Showing posts with label PHP print_r. Show all posts
Showing posts with label PHP print_r. Show all posts

Friday, 2 August 2019

How to print the contents of an array using PHP

//Array example
$arr = array(‘one’,’two’,’three’,’four’);
Use the print_r function.
Example: print_r($arr);
The example above will print out the following:
Array ( [0] => one [1] => two [2] => three [3] => four )
NOTE: If you want to have the contents of the array displayed in a more readable format, add a echo ‘<pre>’; statement before calling the print_r function.
Example of the results:
Array
(
    [0] => one
    [1] => two
    [2] => three
    [3] => four
)
Explanation:
print_r – Prints human-readable information about a variable (PHP 4, PHP 5)

Monday, 10 September 2018

Return information from PHP print_r instead of displaying it

print_r is a useful PHP function for displaying the values of an array or object when debugging etc. Having been a PHP programmer since the days of PHP 3.x (and not having read the manual page for this function in years) I didn't realise this function had added the capability to return the information as well as print it from PHP 4.3.0 and only discovered this in the last few days. This post gives a brief overview of PHP's print_r function.
Let's say for example you have the following array:
$myarray = array(
  "wd" => "well done",
  "m" => "medium",
  "mr" => "medium rare",
  "r" => "rare"
);
Doing this:
print_r($myarray);
will output this:
Array
(
    [wd] => well done
    [m] => medium
    [mr] => medium rare
    [r] => rare
)
If you want to instead suppress the output and return the array information into a variable (which could be emailed in an error report, for example) set the second optional parameter to print_r to true like so:
$info = print_r($myarray);
The output will now not be displayed and the example output above will instead be stored into the $info variable. This should work for any variable types. The following examples would also store the output from print_r into the $info variable instead of outputting to standard output.
$foo = "bar";
$info = print_r($foo, true);

$info = print_r(some_function_call(), true);

$foo = new some_class();
$info = print_r($foo);
It's really useful to be able to capture information like this using print_r because it means you can use it to store into error logs or send in an error email, as well as for debugging purposes. I only wish I'd known about the additional parameter when it first became available :)

Related posts:

Friday, 3 October 2014

print_r in PHP

print_r — Prints human-readable information about a variable


Syntax: 
mixed print_r ( mixed $expression [, bool $return = false ] )

print_r() displays information about a variable in a way that's readable by humans.

print_r(), var_dump() and var_export() will also show protected and private properties of objects with PHP 5. Static class members will not be shown.
Parameters: 

expression

    The expression to be printed.
return

    If you would like to capture the output of print_r(), use the return parameter. When this parameter is set to TRUE, print_r() will return the information rather than print it.

Return Values:

If given a string, integer or float, the value itself will be printed. If given an array, values will be presented in a format that shows keys and elements. Similar notation is used for objects.

When the return parameter is TRUE, this function will return a string. Otherwise, the return value is TRUE.
Notes:

    Note:

    When the return parameter is used, this function uses internal output buffering so it cannot be used inside an ob_start() callback function.

Examples:

Example #1 print_r() example
<pre>
<?php
$a = array ('a' => 'apple', 'b' => 'banana', 'c' => array ('x', 'y', 'z'));
print_r ($a);
?>
</pre>

The above example will output:

<pre>
Array
(
    [a] => apple
    [b] => banana
    [c] => Array
        (
            [0] => x
            [1] => y
            [2] => z
        )
)
</pre>

Example #2 return parameter example
<?php
$b = array ('m' => 'monkey', 'foo' => 'bar', 'x' => array ('x', 'y', 'z'));
$results = print_r($b, true); // $results now contains output from print_r
?>