2 Test print_r() function
7 /* Prototype: bool print_r ( mixed $expression [, bool $return] );
8 Description: Prints human-readable information about a variable
11 /* Prototype: void check_printr( $variables )
12 Description: use print_r() to print variables */
13 function check_printr( $variables ) {
15 foreach( $variables as $variable ) {
16 echo "\n-- Iteration $counter --\n";
17 //default = false, prints output to screen
19 //$return=TRUE, print_r() will return its output, instead of printing it
20 $ret_string = print_r($variable, true); //$ret_string captures the output
21 echo "\n$ret_string\n";
22 //$return=false, print_r() prints the output; default behavior
23 print_r($variable, false);
28 echo "\n*** Testing print_r() on integer variables ***\n";
30 0, // zero as argument
31 000000123, //octal value of 83
33 -00000123, //octal value of 83
35 range(1,10), // positive values
36 range(-1,-10), // negative values
37 +2147483647, // max positive integer
38 +2147483648, // max positive integer + 1
39 -2147483648, // min range of integer
40 -2147483647, // min range of integer + 1
41 0x7FFFFFFF, // max positive hexadecimal integer
42 -0x80000000, // min range of hexadecimal integer
43 017777777777, // max posotive octal integer
44 -020000000000 // min range of octal integer
46 /* calling check_printr() to display contents of integer variables
48 check_printr($integers);
50 echo "\n*** Testing print_r() on float variables ***\n";
80 -0x80000001, // float value, beyond max negative int
81 0x80000001, // float value, beyond max positive int
82 020000000001, // float value, beyond max positive int
83 -020000000001 // float value, beyond max negative int
85 /* calling check_printr() to display contents of float variables
87 check_printr($floats);
89 echo "\n*** Testing print_r() on string variables ***\n";
102 "abcd\x0n1234\x0005678\x0000efgh\xijkl", // strings with hexadecimal NULL
103 "abcd\0efgh\0ijkl\x00mnop\x000qrst\00uvwx\0000yz", // strings with octal NULL
104 "1234\t\n5678\n\t9100\"abcda" // strings with escape characters
106 /* calling check_printr() to display contents of strings using print_r() */
107 check_printr($strings);
109 echo "\n*** Testing print_r() on boolean variables ***\n";
116 /* calling check_printr() to display boolean variables using print_r() */
117 check_printr($booleans);
118 var_dump( reset($booleans) );
120 var_dump( current($booleans) );
122 echo "\n*** Testing print_r() on array variables ***\n";
130 array(array(), array()),
131 array(array(1, 2), array('a', 'b')),
133 array("test" => "is_array"),
137 array("string", "test"),
138 array('string', 'test'),
140 /* calling check_printr() to display contents of $arrays */
141 check_printr($arrays);
143 echo "\n*** Testing print_r() on object variables ***\n";
147 public $public_var1 = 10;
148 private $private_var1 = 20;
149 private $private_var2;
150 protected $protected_var1 = "string_1";
151 protected $protected_var2;
153 function __construct() {
155 $this->public_var2 = 11;
156 $this->private_var2 = 21;
157 $this->protected_var2 = "string_2";
160 public function foo1() {
161 echo "foo1() is called\n";
163 protected function foo2() {
164 echo "foo2() is called\n";
166 private function foo3() {
167 echo "foo3() is called\n";
170 /* class with no member */
171 class no_member_class {
175 /* class with member as object of other class */
176 class contains_object_class
180 public $class_object2;
181 private $class_object3;
182 protected $class_object4;
183 var $no_member_class_object;
185 public function func() {
186 echo "func() is called \n";
189 function __construct() {
190 $this->class_object1 = new object_class();
191 $this->class_object2 = new object_class();
192 $this->class_object3 = $this->class_object1;
193 $this->class_object4 = $this->class_object2;
194 $this->no_member_class_object = new no_member_class();
195 $this->class_object5 = $this; //recursive reference
199 /* objects of different classes */
200 $obj = new contains_object_class;
201 $temp_class_obj = new object_class();
203 /* object which is unset */
204 $unset_obj = new object_class();
210 new contains_object_class,
214 $obj->no_member_class_object,
218 /* calling check_printr() to display contents of the objects using print_r() */
219 check_printr($objects);
221 echo "\n** Testing print_r() on objects having circular reference **\n";
222 $recursion_obj1 = new object_class();
223 $recursion_obj2 = new object_class();
224 $recursion_obj1->obj = &$recursion_obj2; //circular reference
225 $recursion_obj2->obj = &$recursion_obj1; //circular reference
226 print_r($recursion_obj2);
228 echo "\n*** Testing print_r() on resources ***\n";
229 /* file type resource */
230 $file_handle = fopen(__FILE__, "r");
232 /* directory type resource */
233 $dir_handle = opendir( __DIR__ );
239 /* calling check_printr() to display the resource content type
241 check_printr($resources);
243 echo "\n*** Testing print_r() on different combinations of scalar
244 and non-scalar variables ***\n";
245 /* a variable which is unset */
249 /* unset file type resource */
252 $variations = array (
253 array( 123, -1.2345, "a" ),
254 array( "d", array(1, 3, 5), true, null),
255 array( new no_member_class, array(), false, 0 ),
256 array( -0.00, "Where am I?", array(7,8,9), TRUE, 'A', 987654321 ),
257 array( @$unset_var, 2.E+10, 100-20.9, 000004.599998 ), //unusual data
258 array( "array(1,2,3,4)1.0000002TRUE", @$file_handle, 111333.00+45e5, '/00\7')
260 /* calling check_printr() to display combinations of scalar and
261 non-scalar variables using print_r() */
262 check_printr($variations);
264 echo "\n*** Testing print_r() on miscellaneous input arguments ***\n";
265 $misc_values = array (
267 NULL, // NULL argument
268 @$undef_variable, //undefined variable
271 /* calling check_printr() to display miscellaneous data using print_r() */
272 check_printr($misc_values);
274 /* closing resource handle used */
275 closedir($dir_handle);
280 *** Testing print_r() on integer variables ***
420 *** Testing print_r() on float variables ***
550 *** Testing print_r() on string variables ***
597 abcd
\0n1234
\005678
\000efgh\xijkl
598 abcd
\0n1234
\005678
\000efgh\xijkl
599 abcd
\0n1234
\005678
\000efgh\xijkl
601 abcd
\0efgh
\0ijkl
\0mnop
\00qrst
\0uvwx
\00yz
602 abcd
\0efgh
\0ijkl
\0mnop
\00qrst
\0uvwx
\00yz
603 abcd
\0efgh
\0ijkl
\0mnop
\00qrst
\0uvwx
\00yz
614 *** Testing print_r() on boolean variables ***
635 *** Testing print_r() on array variables ***
937 *** Testing print_r() on object variables ***
944 [private_var1:object_class:private] => 20
945 [private_var2:object_class:private] => 21
946 [protected_var1:protected] => string_1
947 [protected_var2:protected] => string_2
955 [private_var1:object_class:private] => 20
956 [private_var2:object_class:private] => 21
957 [protected_var1:protected] => string_1
958 [protected_var2:protected] => string_2
966 [private_var1:object_class:private] => 20
967 [private_var2:object_class:private] => 21
968 [protected_var1:protected] => string_1
969 [protected_var2:protected] => string_2
974 no_member_class Object
978 no_member_class Object
982 no_member_class Object
987 contains_object_class Object
990 [class_object1] => object_class Object
994 [private_var1:object_class:private] => 20
995 [private_var2:object_class:private] => 21
996 [protected_var1:protected] => string_1
997 [protected_var2:protected] => string_2
1001 [class_object2] => object_class Object
1005 [private_var1:object_class:private] => 20
1006 [private_var2:object_class:private] => 21
1007 [protected_var1:protected] => string_1
1008 [protected_var2:protected] => string_2
1012 [class_object3:contains_object_class:private] => object_class Object
1016 [private_var1:object_class:private] => 20
1017 [private_var2:object_class:private] => 21
1018 [protected_var1:protected] => string_1
1019 [protected_var2:protected] => string_2
1023 [class_object4:protected] => object_class Object
1027 [private_var1:object_class:private] => 20
1028 [private_var2:object_class:private] => 21
1029 [protected_var1:protected] => string_1
1030 [protected_var2:protected] => string_2
1034 [no_member_class_object] => no_member_class Object
1038 [class_object5] => contains_object_class Object
1042 contains_object_class Object
1045 [class_object1] => object_class Object
1049 [private_var1:object_class:private] => 20
1050 [private_var2:object_class:private] => 21
1051 [protected_var1:protected] => string_1
1052 [protected_var2:protected] => string_2
1056 [class_object2] => object_class Object
1060 [private_var1:object_class:private] => 20
1061 [private_var2:object_class:private] => 21
1062 [protected_var1:protected] => string_1
1063 [protected_var2:protected] => string_2
1067 [class_object3:contains_object_class:private] => object_class Object
1071 [private_var1:object_class:private] => 20
1072 [private_var2:object_class:private] => 21
1073 [protected_var1:protected] => string_1
1074 [protected_var2:protected] => string_2
1078 [class_object4:protected] => object_class Object
1082 [private_var1:object_class:private] => 20
1083 [private_var2:object_class:private] => 21
1084 [protected_var1:protected] => string_1
1085 [protected_var2:protected] => string_2
1089 [no_member_class_object] => no_member_class Object
1093 [class_object5] => contains_object_class Object
1097 contains_object_class Object
1100 [class_object1] => object_class Object
1104 [private_var1:object_class:private] => 20
1105 [private_var2:object_class:private] => 21
1106 [protected_var1:protected] => string_1
1107 [protected_var2:protected] => string_2
1111 [class_object2] => object_class Object
1115 [private_var1:object_class:private] => 20
1116 [private_var2:object_class:private] => 21
1117 [protected_var1:protected] => string_1
1118 [protected_var2:protected] => string_2
1122 [class_object3:contains_object_class:private] => object_class Object
1126 [private_var1:object_class:private] => 20
1127 [private_var2:object_class:private] => 21
1128 [protected_var1:protected] => string_1
1129 [protected_var2:protected] => string_2
1133 [class_object4:protected] => object_class Object
1137 [private_var1:object_class:private] => 20
1138 [private_var2:object_class:private] => 21
1139 [protected_var1:protected] => string_1
1140 [protected_var2:protected] => string_2
1144 [no_member_class_object] => no_member_class Object
1148 [class_object5] => contains_object_class Object
1153 contains_object_class Object
1156 [class_object1] => object_class Object
1160 [private_var1:object_class:private] => 20
1161 [private_var2:object_class:private] => 21
1162 [protected_var1:protected] => string_1
1163 [protected_var2:protected] => string_2
1167 [class_object2] => object_class Object
1171 [private_var1:object_class:private] => 20
1172 [private_var2:object_class:private] => 21
1173 [protected_var1:protected] => string_1
1174 [protected_var2:protected] => string_2
1178 [class_object3:contains_object_class:private] => object_class Object
1182 [private_var1:object_class:private] => 20
1183 [private_var2:object_class:private] => 21
1184 [protected_var1:protected] => string_1
1185 [protected_var2:protected] => string_2
1189 [class_object4:protected] => object_class Object
1193 [private_var1:object_class:private] => 20
1194 [private_var2:object_class:private] => 21
1195 [protected_var1:protected] => string_1
1196 [protected_var2:protected] => string_2
1200 [no_member_class_object] => no_member_class Object
1204 [class_object5] => contains_object_class Object
1208 contains_object_class Object
1211 [class_object1] => object_class Object
1215 [private_var1:object_class:private] => 20
1216 [private_var2:object_class:private] => 21
1217 [protected_var1:protected] => string_1
1218 [protected_var2:protected] => string_2
1222 [class_object2] => object_class Object
1226 [private_var1:object_class:private] => 20
1227 [private_var2:object_class:private] => 21
1228 [protected_var1:protected] => string_1
1229 [protected_var2:protected] => string_2
1233 [class_object3:contains_object_class:private] => object_class Object
1237 [private_var1:object_class:private] => 20
1238 [private_var2:object_class:private] => 21
1239 [protected_var1:protected] => string_1
1240 [protected_var2:protected] => string_2
1244 [class_object4:protected] => object_class Object
1248 [private_var1:object_class:private] => 20
1249 [private_var2:object_class:private] => 21
1250 [protected_var1:protected] => string_1
1251 [protected_var2:protected] => string_2
1255 [no_member_class_object] => no_member_class Object
1259 [class_object5] => contains_object_class Object
1263 contains_object_class Object
1266 [class_object1] => object_class Object
1270 [private_var1:object_class:private] => 20
1271 [private_var2:object_class:private] => 21
1272 [protected_var1:protected] => string_1
1273 [protected_var2:protected] => string_2
1277 [class_object2] => object_class Object
1281 [private_var1:object_class:private] => 20
1282 [private_var2:object_class:private] => 21
1283 [protected_var1:protected] => string_1
1284 [protected_var2:protected] => string_2
1288 [class_object3:contains_object_class:private] => object_class Object
1292 [private_var1:object_class:private] => 20
1293 [private_var2:object_class:private] => 21
1294 [protected_var1:protected] => string_1
1295 [protected_var2:protected] => string_2
1299 [class_object4:protected] => object_class Object
1303 [private_var1:object_class:private] => 20
1304 [private_var2:object_class:private] => 21
1305 [protected_var1:protected] => string_1
1306 [protected_var2:protected] => string_2
1310 [no_member_class_object] => no_member_class Object
1314 [class_object5] => contains_object_class Object
1323 [private_var1:object_class:private] => 20
1324 [private_var2:object_class:private] => 21
1325 [protected_var1:protected] => string_1
1326 [protected_var2:protected] => string_2
1334 [private_var1:object_class:private] => 20
1335 [private_var2:object_class:private] => 21
1336 [protected_var1:protected] => string_1
1337 [protected_var2:protected] => string_2
1345 [private_var1:object_class:private] => 20
1346 [private_var2:object_class:private] => 21
1347 [protected_var1:protected] => string_1
1348 [protected_var2:protected] => string_2
1357 [private_var1:object_class:private] => 20
1358 [private_var2:object_class:private] => 21
1359 [protected_var1:protected] => string_1
1360 [protected_var2:protected] => string_2
1368 [private_var1:object_class:private] => 20
1369 [private_var2:object_class:private] => 21
1370 [protected_var1:protected] => string_1
1371 [protected_var2:protected] => string_2
1379 [private_var1:object_class:private] => 20
1380 [private_var2:object_class:private] => 21
1381 [protected_var1:protected] => string_1
1382 [protected_var2:protected] => string_2
1387 no_member_class Object
1391 no_member_class Object
1395 no_member_class Object
1404 [private_var1:object_class:private] => 20
1405 [private_var2:object_class:private] => 21
1406 [protected_var1:protected] => string_1
1407 [protected_var2:protected] => string_2
1415 [private_var1:object_class:private] => 20
1416 [private_var2:object_class:private] => 21
1417 [protected_var1:protected] => string_1
1418 [protected_var2:protected] => string_2
1426 [private_var1:object_class:private] => 20
1427 [private_var2:object_class:private] => 21
1428 [protected_var1:protected] => string_1
1429 [protected_var2:protected] => string_2
1437 ** Testing print_r() on objects having circular reference **
1442 [private_var1:object_class:private] => 20
1443 [private_var2:object_class:private] => 21
1444 [protected_var1:protected] => string_1
1445 [protected_var2:protected] => string_2
1447 [obj] => object_class Object
1451 [private_var1:object_class:private] => 20
1452 [private_var2:object_class:private] => 21
1453 [protected_var1:protected] => string_1
1454 [protected_var2:protected] => string_2
1456 [obj] => object_class Object
1462 *** Testing print_r() on resources ***
1472 *** Testing print_r() on different combinations of scalar
1473 and non-scalar variables ***
1543 [0] => no_member_class Object
1557 [0] => no_member_class Object
1571 [0] => no_member_class Object
1660 [0] => array(1,2,3,4)1.0000002TRUE
1668 [0] => array(1,2,3,4)1.0000002TRUE
1676 [0] => array(1,2,3,4)1.0000002TRUE
1682 *** Testing print_r() on miscellaneous input arguments ***