Posts Tagged ‘print_r’

Hello all, this will be the first in a weekly tradition I am starting today. Every Thursday night I plan on publishing an article on one PHP function. I will start with the ones I use most frequently then move onto the more obscure ones so I can digest and learn all I can while passing that knowledge to others.

So to start of this week I’ll go with a function I use quit often while I am building a website; print_r(). Here is the official description taken straight form the php.net manual on what this function does:

Prints human-readable information about a variable

Now what does this mean exactly? It is really quit simple. The function takes a variable, it doesn’t matter the type. It could be an Object, Array, or a simple integer and it spits it out onto the screen so you can see exactly what it is. It is pretty useful when building and debugging a script.

One word of caution when using it though. Since it can and will most likely be displaying sesitive information about your application you want to use it in a secure environment if at all possible. If not then at least in a development environment (which should be secured anyways). If this is not possible I will use it on a refresh then quickly comment it out so it will hopefully not be seen by regular users. I hate doing this so in these cases I also add in an IF statement to only show the output to my IP address. The morale of this long rant. Be cautious, as you don’t want to allow any kind of security leak.

Now that my security rant is over lets move on. The print_r() function takes one required parameter and one optional parameter. They are as follows:

mixed $expression
bool  $return = false [optional]

The first parameter is the actual variable you are examining.

The second parameter is optional and defaults to false. All this does is tell the function to either display the results to the screen or return the results as a variable for later use. I have never yet had a reason to set this optional parameter to true. Now onto some examples.

We have just used the print_r() function to print to the screen an array of fruit names for an application we are working on. Here is the code:


$fruit = array('Apple','Mango','Banana', array('Strawberry','Blueberry','Cranberry'));
print_r($fruit);

// The output of this call is as follows:
Array ( [0] => Apple [1] => Mango [2] => Banana [3] => Array ( [0] => Strawberry [1] => Blueberry [2] => Cranberry ) )

This is pretty useful if you have a complicated script and need to see what is inside an array or object that is set in another file or are the results of a DB query. The only problem I find with this is the output. With the example above it isn’t too bad but if you have a large array or object you are examining it can get very messy¬† and confusing to look at. What I have done to overcome this shortfall to the function is create my own modification of it. The function is rather quite simple. I have seen people do this trick but since I like to write as little code as possible whenever I can and to reuse my code I created a function to accomplish it. My solution to the output problem is this:


function print_a($a) {
    echo "<pre>";
    print_r($a);
    echo "</pre>";
}

$fruit = array('Apple','Mango','Banana', array('Strawberry','Blueberry','Cranberry'));
print_a($fruit);

// The output of this call is as follows:
Array
(
     [0] => Apple
     [1] => Mango
     [2] => Banana
     [3] => Array
     (
         [0] => Strawberry
         [1] => Blueberry
         [2] => Cranberry
     )
)

Now we have a much more readable output and can understand the structure of the array much easier.What makes this possible is by echoing out the HTML <PRE> tabs. What these tags do is allow for text to display correctly within the browser as it is written. It keeps all it’s formatting and white space¬† which is normally stripped out by the browser. With the function I created to accomplish this I can call print_a() in replace of print_r() any where in my code to have nicely formatted output with out any extra code.

For a wrap up. The print_r() function is a very useful function when building an application or debugging one. It allows you to examine any variable or expression to get a better understanding of it’s structure and what it contains. It is most useful when examining arrays and objects. You also have to be mindful of where you use it as you don’t want to expose sensitive data to the world. The function also prints the results, although readable, in an ugly format when not surrounded by the HTML <PRE> tags. Using a costume function like mine above you can get nicely formatted output without having to surround each call or print_r() with the <pre> tag.

I hope this has been helpful and any and all comments are welcome. If you know any more information you think should be added to this let me know and I’ll be glad to add it and give credit to the addition. Next week I’ll discuss another function from the PHP library that I use often, array_push(). If you would like me to discuss a certain topic please let me know.

Thanks,
– Miah

Advertisements