Prev | Current Page 203 | Next

W. Jason Gilmore

"Beginning PHP and MySQL: From Novice to Professional"

While this
works great for instructional purposes, in the real world you??™ll need to know how to easily output
their contents to the screen for testing purposes. This is most commonly done with the print_r()
function. Its prototype follows:
boolean print_r(mixed variable [, boolean return])
The print_r() function accepts a variable and sends its contents to standard output, returning
TRUE on success and FALSE otherwise. This in itself isn??™t particularly exciting, until you realize it
will organize an array??™s contents (as well as an object??™s) into a readable format. For example, suppose
you want to view the contents of an associative array consisting of states and their corresponding
state capitals. You could call print_r() like this:
print_r($states);
This returns the following:
Array ( [Ohio] => Columbus [Iowa] => Des Moines [Arizona] => Phoenix )
The optional parameter return modifies the function??™s behavior, causing it to return the output to
the caller, rather than send it to standard output. Therefore, if you want to return the contents of the
preceding $states array, you just set return to TRUE:
$stateCapitals = print_r($states, TRUE);
This function is used repeatedly throughout this chapter as a simple means for displaying
example results.
Keep in mind the print_r() function isn??™t the only way to output an array, but rather offers
a convenient means for doing so.


Pages:
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215