print_rDescriptionprint_r() displays information about a variable in a format meant to be easily understandable by humans. It is often used for debugging - providing a simple and easy way to display the current contents of a variable. (However, var_dump() provides more complete information and allows for the use of multiple arguments.) For simple scalar variables, such asbooleans, integers, strings, anddoubles, the value contained in the variable is printed. Resource pointers have their type (resource) and ID number displayed (such as #1, #2 or #45). The ID number assigned to a resource pointer reflects the order in which the resource pointer was created. (For example, te fifth resource pointer in a script will have an ID number of 5.) Arrays are printed as a list of keys and values, and have their typearray) printed at the top of the list. Objects are handled in a fashion similar to that of arrays. The class of which the object is an instance is displayed, followed by the typeobject). After this, a list of the object's member variables is printed. WarningIf print_r() is used on a data structure that is a reference to itself, the function enters a recursive loop - generating the same information repeatedly until the script times out or the user cancels the script. See the examples for more information. ExampleExample 1402. Simple use of print_r() for debugging // In this case, pretend that we are building a shopping cart. // If there is a subtotal for the cart, but the total equals 0 // ... then we must have messed up an algorithm. // Using print_r to display all cart values can help us find odd values. // From this information, we can more easily track down the faulty algorithm(s). if ($cart['subtotal'] > 0 && $cart['total'] == 0) print_r ($cart); Example 1403. Show how print_r() displays information about variables <pre> <? // Define a simple class for use in testing class point { var $x = 0; var $y = 0; } // Make a list of variables to pass to print_r $list_of_variables = array ( 'double' => 2.5, 'int' => 10, 'string' => "'Languages are an artistic medium.' - Larry Wall", 'array' => array ('hot dogs' => 'sauerkraut', 'peanut butter' => 'jelly'), 'object' => new point (), 'resource' => opendir ('.') ); foreach ($list_of_variables as $value){ print_r ($value); print "\n\n"; } ?> </pre>
PHP Functions Essential Reference. Copyright © 2002 by New Riders Publishing
(Authors: Zak Greant, Graeme Merrall, Torben Wilson, Brett Michlitsch).
This material may be distributed only subject to the terms and conditions set forth
in the Open Publication License, v1.0 or later (the latest version is presently available at
http://www.opencontent.org/openpub/).
The authors of this book have elected not to choose any options under the OPL. This online book was obtained
from http://www.fooassociates.com/phpfer/
and is designed to provide information about the PHP programming language, focusing on PHP version 4.0.4
for the most part. The information is provided on an as-is basis, and no warranty or fitness is implied. All
persons and entities shall have neither liability nor responsibility to any person or entity with respect to
any loss or damage arising from the information contained in this book.
|