2 Test print_r() function
5 if (PHP_INT_SIZE != 8) die("skip this test is for 64bit platform only");
11 /* Prototype: bool print_r ( mixed $expression [, bool $return] );
12 Description: Prints human-readable information about a variable
15 /* Prototype: void check_printr( $variables )
16 Description: use print_r() to print variables */
17 function check_printr( $variables ) {
19 foreach( $variables as $variable ) {
20 echo "\n-- Iteration $counter --\n";
21 //default = false, prints output to screen
23 //$return=TRUE, print_r() will return its output, instead of printing it
24 $ret_string = print_r($variable, true); //$ret_string captures the output
25 echo "\n$ret_string\n";
26 //$return=false, print_r() prints the output; default behavior
27 print_r($variable, false);
32 echo "\n*** Testing print_r() on integer variables ***\n";
34 0, // zero as argument
35 000000123, //octal value of 83
37 -00000123, //octal value of 83
39 range(1,10), // positive values
40 range(-1,-10), // negative values
41 +2147483647, // max positive integer
42 +2147483648, // max positive integer + 1
43 -2147483648, // min range of integer
44 -2147483647, // min range of integer + 1
45 0x7FFFFFFF, // max positive hexadecimal integer
46 -0x80000000, // min range of hexadecimal integer
47 017777777777, // max posotive octal integer
48 -020000000000 // min range of octal integer
50 /* calling check_printr() to display contents of integer variables
52 check_printr($integers);
54 echo "\n*** Testing print_r() on float variables ***\n";
84 -0x80000001, // float value, beyond max negative int
85 0x80000001, // float value, beyond max positive int
86 020000000001, // float value, beyond max positive int
87 -020000000001 // float value, beyond max negative int
89 /* calling check_printr() to display contents of float variables
91 check_printr($floats);
93 echo "\n*** Testing print_r() on string variables ***\n";
106 "abcd\x0n1234\x0005678\x0000efgh\xijkl", // strings with hexadecimal NULL
107 "abcd\0efgh\0ijkl\x00mnop\x000qrst\00uvwx\0000yz", // strings with octal NULL
108 "1234\t\n5678\n\t9100\"abcda" // strings with escape characters
110 /* calling check_printr() to display contents of strings using print_r() */
111 check_printr($strings);
113 echo "\n*** Testing print_r() on boolean variables ***\n";
120 /* calling check_printr() to display boolean variables using print_r() */
121 check_printr($booleans);
122 var_dump( reset($booleans) );
124 var_dump( current($booleans) );
126 echo "\n*** Testing print_r() on array variables ***\n";
134 array(array(), array()),
135 array(array(1, 2), array('a', 'b')),
137 array("test" => "is_array"),
141 array("string", "test"),
142 array('string', 'test'),
144 /* calling check_printr() to display contents of $arrays */
145 check_printr($arrays);
147 echo "\n*** Testing print_r() on object variables ***\n";
151 public $public_var1 = 10;
152 private $private_var1 = 20;
153 private $private_var2;
154 protected $protected_var1 = "string_1";
155 protected $protected_var2;
157 function __construct ( ) {
159 $this->public_var2 = 11;
160 $this->private_var2 = 21;
161 $this->protected_var2 = "string_2";
164 public function foo1() {
165 echo "foo1() is called\n";
167 protected function foo2() {
168 echo "foo2() is called\n";
170 private function foo3() {
171 echo "foo3() is called\n";
174 /* class with no member */
175 class no_member_class {
179 /* class with member as object of other class */
180 class contains_object_class
184 public $class_object2;
185 private $class_object3;
186 protected $class_object4;
187 var $no_member_class_object;
189 public function func() {
190 echo "func() is called \n";
193 function __construct () {
194 $this->class_object1 = new object_class();
195 $this->class_object2 = new object_class();
196 $this->class_object3 = $this->class_object1;
197 $this->class_object4 = $this->class_object2;
198 $this->no_member_class_object = new no_member_class();
199 $this->class_object5 = $this; //recursive reference
203 /* objects of different classes */
204 $obj = new contains_object_class;
205 $temp_class_obj = new object_class();
207 /* object which is unset */
208 $unset_obj = new object_class();
214 new contains_object_class,
218 $obj->no_member_class_object,
222 /* calling check_printr() to display contents of the objects using print_r() */
223 check_printr($objects);
225 echo "\n** Testing print_r() on objects having circular reference **\n";
226 $recursion_obj1 = new object_class();
227 $recursion_obj2 = new object_class();
228 $recursion_obj1->obj = &$recursion_obj2; //circular reference
229 $recursion_obj2->obj = &$recursion_obj1; //circular reference
230 print_r($recursion_obj2);
232 echo "\n*** Testing print_r() on resources ***\n";
233 /* file type resource */
234 $file_handle = fopen(__FILE__, "r");
236 /* directory type resource */
237 $dir_handle = opendir( __DIR__ );
243 /* calling check_printr() to display the resource content type
245 check_printr($resources);
247 echo "\n*** Testing print_r() on different combinations of scalar
248 and non-scalar variables ***\n";
249 /* a variable which is unset */
253 /* unset file type resource */
256 $variations = array (
257 array( 123, -1.2345, "a" ),
258 array( "d", array(1, 3, 5), true, null),
259 array( new no_member_class, array(), false, 0 ),
260 array( -0.00, "Where am I?", array(7,8,9), TRUE, 'A', 987654321 ),
261 array( @$unset_var, 2.E+10, 100-20.9, 000004.599998 ), //unusual data
262 array( "array(1,2,3,4)1.0000002TRUE", @$file_handle, 111333.00+45e5, '/00\7')
264 /* calling check_printr() to display combinations of scalar and
265 non-scalar variables using print_r() */
266 check_printr($variations);
268 echo "\n*** Testing print_r() on miscellaneous input arguments ***\n";
269 $misc_values = array (
271 NULL, // NULL argument
272 @$undef_variable, //undefined variable
275 /* calling check_printr() to display miscellaneous data using print_r() */
276 check_printr($misc_values);
278 /* closing resource handle used */
279 closedir($dir_handle);
284 *** Testing print_r() on integer variables ***
424 *** Testing print_r() on float variables ***
554 *** Testing print_r() on string variables ***
601 abcd
\0n1234
\005678
\000efgh\xijkl
602 abcd
\0n1234
\005678
\000efgh\xijkl
603 abcd
\0n1234
\005678
\000efgh\xijkl
605 abcd
\0efgh
\0ijkl
\0mnop
\00qrst
\0uvwx
\00yz
606 abcd
\0efgh
\0ijkl
\0mnop
\00qrst
\0uvwx
\00yz
607 abcd
\0efgh
\0ijkl
\0mnop
\00qrst
\0uvwx
\00yz
618 *** Testing print_r() on boolean variables ***
639 *** Testing print_r() on array variables ***
941 *** Testing print_r() on object variables ***
948 [private_var1:object_class:private] => 20
949 [private_var2:object_class:private] => 21
950 [protected_var1:protected] => string_1
951 [protected_var2:protected] => string_2
959 [private_var1:object_class:private] => 20
960 [private_var2:object_class:private] => 21
961 [protected_var1:protected] => string_1
962 [protected_var2:protected] => string_2
970 [private_var1:object_class:private] => 20
971 [private_var2:object_class:private] => 21
972 [protected_var1:protected] => string_1
973 [protected_var2:protected] => string_2
978 no_member_class Object
982 no_member_class Object
986 no_member_class Object
991 contains_object_class Object
994 [class_object1] => object_class Object
998 [private_var1:object_class:private] => 20
999 [private_var2:object_class:private] => 21
1000 [protected_var1:protected] => string_1
1001 [protected_var2:protected] => string_2
1005 [class_object2] => object_class Object
1009 [private_var1:object_class:private] => 20
1010 [private_var2:object_class:private] => 21
1011 [protected_var1:protected] => string_1
1012 [protected_var2:protected] => string_2
1016 [class_object3:contains_object_class:private] => object_class Object
1020 [private_var1:object_class:private] => 20
1021 [private_var2:object_class:private] => 21
1022 [protected_var1:protected] => string_1
1023 [protected_var2:protected] => string_2
1027 [class_object4:protected] => object_class Object
1031 [private_var1:object_class:private] => 20
1032 [private_var2:object_class:private] => 21
1033 [protected_var1:protected] => string_1
1034 [protected_var2:protected] => string_2
1038 [no_member_class_object] => no_member_class Object
1042 [class_object5] => contains_object_class Object
1046 contains_object_class Object
1049 [class_object1] => object_class Object
1053 [private_var1:object_class:private] => 20
1054 [private_var2:object_class:private] => 21
1055 [protected_var1:protected] => string_1
1056 [protected_var2:protected] => string_2
1060 [class_object2] => object_class Object
1064 [private_var1:object_class:private] => 20
1065 [private_var2:object_class:private] => 21
1066 [protected_var1:protected] => string_1
1067 [protected_var2:protected] => string_2
1071 [class_object3:contains_object_class:private] => object_class Object
1075 [private_var1:object_class:private] => 20
1076 [private_var2:object_class:private] => 21
1077 [protected_var1:protected] => string_1
1078 [protected_var2:protected] => string_2
1082 [class_object4:protected] => object_class Object
1086 [private_var1:object_class:private] => 20
1087 [private_var2:object_class:private] => 21
1088 [protected_var1:protected] => string_1
1089 [protected_var2:protected] => string_2
1093 [no_member_class_object] => no_member_class Object
1097 [class_object5] => contains_object_class Object
1101 contains_object_class Object
1104 [class_object1] => object_class Object
1108 [private_var1:object_class:private] => 20
1109 [private_var2:object_class:private] => 21
1110 [protected_var1:protected] => string_1
1111 [protected_var2:protected] => string_2
1115 [class_object2] => object_class Object
1119 [private_var1:object_class:private] => 20
1120 [private_var2:object_class:private] => 21
1121 [protected_var1:protected] => string_1
1122 [protected_var2:protected] => string_2
1126 [class_object3:contains_object_class:private] => object_class Object
1130 [private_var1:object_class:private] => 20
1131 [private_var2:object_class:private] => 21
1132 [protected_var1:protected] => string_1
1133 [protected_var2:protected] => string_2
1137 [class_object4:protected] => object_class Object
1141 [private_var1:object_class:private] => 20
1142 [private_var2:object_class:private] => 21
1143 [protected_var1:protected] => string_1
1144 [protected_var2:protected] => string_2
1148 [no_member_class_object] => no_member_class Object
1152 [class_object5] => contains_object_class Object
1157 contains_object_class Object
1160 [class_object1] => object_class Object
1164 [private_var1:object_class:private] => 20
1165 [private_var2:object_class:private] => 21
1166 [protected_var1:protected] => string_1
1167 [protected_var2:protected] => string_2
1171 [class_object2] => object_class Object
1175 [private_var1:object_class:private] => 20
1176 [private_var2:object_class:private] => 21
1177 [protected_var1:protected] => string_1
1178 [protected_var2:protected] => string_2
1182 [class_object3:contains_object_class:private] => object_class Object
1186 [private_var1:object_class:private] => 20
1187 [private_var2:object_class:private] => 21
1188 [protected_var1:protected] => string_1
1189 [protected_var2:protected] => string_2
1193 [class_object4:protected] => object_class Object
1197 [private_var1:object_class:private] => 20
1198 [private_var2:object_class:private] => 21
1199 [protected_var1:protected] => string_1
1200 [protected_var2:protected] => string_2
1204 [no_member_class_object] => no_member_class Object
1208 [class_object5] => contains_object_class Object
1212 contains_object_class Object
1215 [class_object1] => object_class Object
1219 [private_var1:object_class:private] => 20
1220 [private_var2:object_class:private] => 21
1221 [protected_var1:protected] => string_1
1222 [protected_var2:protected] => string_2
1226 [class_object2] => object_class Object
1230 [private_var1:object_class:private] => 20
1231 [private_var2:object_class:private] => 21
1232 [protected_var1:protected] => string_1
1233 [protected_var2:protected] => string_2
1237 [class_object3:contains_object_class:private] => object_class Object
1241 [private_var1:object_class:private] => 20
1242 [private_var2:object_class:private] => 21
1243 [protected_var1:protected] => string_1
1244 [protected_var2:protected] => string_2
1248 [class_object4:protected] => object_class Object
1252 [private_var1:object_class:private] => 20
1253 [private_var2:object_class:private] => 21
1254 [protected_var1:protected] => string_1
1255 [protected_var2:protected] => string_2
1259 [no_member_class_object] => no_member_class Object
1263 [class_object5] => contains_object_class Object
1267 contains_object_class Object
1270 [class_object1] => object_class Object
1274 [private_var1:object_class:private] => 20
1275 [private_var2:object_class:private] => 21
1276 [protected_var1:protected] => string_1
1277 [protected_var2:protected] => string_2
1281 [class_object2] => object_class Object
1285 [private_var1:object_class:private] => 20
1286 [private_var2:object_class:private] => 21
1287 [protected_var1:protected] => string_1
1288 [protected_var2:protected] => string_2
1292 [class_object3:contains_object_class:private] => object_class Object
1296 [private_var1:object_class:private] => 20
1297 [private_var2:object_class:private] => 21
1298 [protected_var1:protected] => string_1
1299 [protected_var2:protected] => string_2
1303 [class_object4:protected] => object_class Object
1307 [private_var1:object_class:private] => 20
1308 [private_var2:object_class:private] => 21
1309 [protected_var1:protected] => string_1
1310 [protected_var2:protected] => string_2
1314 [no_member_class_object] => no_member_class Object
1318 [class_object5] => contains_object_class Object
1327 [private_var1:object_class:private] => 20
1328 [private_var2:object_class:private] => 21
1329 [protected_var1:protected] => string_1
1330 [protected_var2:protected] => string_2
1338 [private_var1:object_class:private] => 20
1339 [private_var2:object_class:private] => 21
1340 [protected_var1:protected] => string_1
1341 [protected_var2:protected] => string_2
1349 [private_var1:object_class:private] => 20
1350 [private_var2:object_class:private] => 21
1351 [protected_var1:protected] => string_1
1352 [protected_var2:protected] => string_2
1361 [private_var1:object_class:private] => 20
1362 [private_var2:object_class:private] => 21
1363 [protected_var1:protected] => string_1
1364 [protected_var2:protected] => string_2
1372 [private_var1:object_class:private] => 20
1373 [private_var2:object_class:private] => 21
1374 [protected_var1:protected] => string_1
1375 [protected_var2:protected] => string_2
1383 [private_var1:object_class:private] => 20
1384 [private_var2:object_class:private] => 21
1385 [protected_var1:protected] => string_1
1386 [protected_var2:protected] => string_2
1391 no_member_class Object
1395 no_member_class Object
1399 no_member_class Object
1408 [private_var1:object_class:private] => 20
1409 [private_var2:object_class:private] => 21
1410 [protected_var1:protected] => string_1
1411 [protected_var2:protected] => string_2
1419 [private_var1:object_class:private] => 20
1420 [private_var2:object_class:private] => 21
1421 [protected_var1:protected] => string_1
1422 [protected_var2:protected] => string_2
1430 [private_var1:object_class:private] => 20
1431 [private_var2:object_class:private] => 21
1432 [protected_var1:protected] => string_1
1433 [protected_var2:protected] => string_2
1441 ** Testing print_r() on objects having circular reference **
1446 [private_var1:object_class:private] => 20
1447 [private_var2:object_class:private] => 21
1448 [protected_var1:protected] => string_1
1449 [protected_var2:protected] => string_2
1451 [obj] => object_class Object
1455 [private_var1:object_class:private] => 20
1456 [private_var2:object_class:private] => 21
1457 [protected_var1:protected] => string_1
1458 [protected_var2:protected] => string_2
1460 [obj] => object_class Object
1466 *** Testing print_r() on resources ***
1476 *** Testing print_r() on different combinations of scalar
1477 and non-scalar variables ***
1547 [0] => no_member_class Object
1561 [0] => no_member_class Object
1575 [0] => no_member_class Object
1664 [0] => array(1,2,3,4)1.0000002TRUE
1672 [0] => array(1,2,3,4)1.0000002TRUE
1680 [0] => array(1,2,3,4)1.0000002TRUE
1686 *** Testing print_r() on miscellaneous input arguments ***