Saturday, October 16, 2010

PHP: How to print/echo object properties

When you're in the process of writing code occasionally you will save your work and run the script to see what you have so far. This generally works if you're working with basic variables and arithmetic but when it comes down to arrays and objects the programmable way would be to use foreach or for loop to get results printed on the page.

Well, there is an easier way to get what you want. To print data of your array use this approach and you'll know the contents of your array and multi-dimensional array
$array[0] = "1";
$array[1] = "2";
$array[3] = "3";
print_r($array);


After you launch the script, right click the page and select View Source. The result:

Array
(
     [0] => 1
     [1] => 2
     [2] => 3
)

Use the same approach to print your object properties quickly:
class Sample{
public $example = 5;
}

$test = new Sample();
print_r($test);


The output for object properties is:
Sample Object
(
     [example:Sample:public] => 5
)

Making testing easier one step at a time.

No comments:

Post a Comment