Annotation of embedaddon/php/ext/standard/tests/general_functions/print_r.phpt, revision 1.1
1.1 ! misho 1: --TEST--
! 2: Test print_r() function
! 3: --SKIPIF--
! 4: <?php
! 5: if (PHP_INT_SIZE != 4) die("skip this test is for 32bit platform only");
! 6: ?>
! 7: --INI--
! 8: precision=14
! 9: --FILE--
! 10: <?php
! 11: /* Prototype: bool print_r ( mixed $expression [, bool $return] );
! 12: Description: Prints human-readable information about a variable
! 13: */
! 14:
! 15: /* Prototype: void check_printr( $variables )
! 16: Description: use print_r() to print variables */
! 17: function check_printr( $variables ) {
! 18: $counter = 1;
! 19: foreach( $variables as $variable ) {
! 20: echo "\n-- Iteration $counter --\n";
! 21: //default = false, prints output to screen
! 22: print_r($variable);
! 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);
! 28: $counter++;
! 29: }
! 30: }
! 31:
! 32: echo "\n*** Testing print_r() on integer variables ***\n";
! 33: $integers = array (
! 34: 0, // zero as argument
! 35: 000000123, //octal value of 83
! 36: 123000000,
! 37: -00000123, //octal value of 83
! 38: -12300000,
! 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
! 49: );
! 50: /* calling check_printr() to display contents of integer variables
! 51: using print_r() */
! 52: check_printr($integers);
! 53:
! 54: echo "\n*** Testing print_r() on float variables ***\n";
! 55: $floats = array (
! 56: -0.0,
! 57: +0.0,
! 58: 1.234,
! 59: -1.234,
! 60: -2.000000,
! 61: 000002.00,
! 62: -.5,
! 63: .567,
! 64: -.6700000e-3,
! 65: -.6700000E+3,
! 66: .6700000E+3,
! 67: .6700000e+3,
! 68: -4.10003e-3,
! 69: -4.10003E+3,
! 70: 4.100003e-3,
! 71: 4.100003E+3,
! 72: 1e5,
! 73: -1e5,
! 74: 1e-5,
! 75: -1e-5,
! 76: 1e+5,
! 77: -1e+5,
! 78: 1E5,
! 79: -1E5,
! 80: 1E+5,
! 81: -1E+5,
! 82: 1E-5,
! 83: -1E-5,
! 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
! 88: );
! 89: /* calling check_printr() to display contents of float variables
! 90: using print_r() */
! 91: check_printr($floats);
! 92:
! 93: echo "\n*** Testing print_r() on string variables ***\n";
! 94: $strings = array (
! 95: "",
! 96: '',
! 97: " ",
! 98: ' ',
! 99: "0",
! 100: "\0",
! 101: '\0',
! 102: "\t",
! 103: '\t',
! 104: "PHP",
! 105: 'PHP',
! 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\rabcda" // strings with escape characters
! 109: );
! 110: /* calling check_printr() to display contents of strings using print_r() */
! 111: check_printr($strings);
! 112:
! 113: echo "\n*** Testing print_r() on boolean variables ***\n";
! 114: $booleans = array (
! 115: TRUE,
! 116: FALSE,
! 117: true,
! 118: false
! 119: );
! 120: /* calling check_printr() to display boolean variables using print_r() */
! 121: check_printr($booleans);
! 122: var_dump( reset($booleans) );
! 123: echo "\n";
! 124: var_dump( current($booleans) );
! 125:
! 126: echo "\n*** Testing print_r() on array variables ***\n";
! 127: $arrays = array (
! 128: array(),
! 129: array(NULL),
! 130: array(null),
! 131: array(true),
! 132: array(""),
! 133: array(''),
! 134: array(array(), array()),
! 135: array(array(1, 2), array('a', 'b')),
! 136: array(1 => 'One'),
! 137: array("test" => "is_array"),
! 138: array(0),
! 139: array(-1),
! 140: array(10.5, 5.6),
! 141: array("string", "test"),
! 142: array('string', 'test'),
! 143: );
! 144: /* calling check_printr() to display contents of $arrays */
! 145: check_printr($arrays);
! 146:
! 147: echo "\n*** Testing print_r() on object variables ***\n";
! 148: class object_class
! 149: {
! 150: var $value;
! 151: public $public_var1 = 10;
! 152: private $private_var1 = 20;
! 153: private $private_var2;
! 154: protected $protected_var1 = "string_1";
! 155: protected $protected_var2;
! 156:
! 157: function object_class ( ) {
! 158: $this->value = 50;
! 159: $this->public_var2 = 11;
! 160: $this->private_var2 = 21;
! 161: $this->protected_var2 = "string_2";
! 162: }
! 163:
! 164: public function foo1() {
! 165: echo "foo1() is called\n";
! 166: }
! 167: protected function foo2() {
! 168: echo "foo2() is called\n";
! 169: }
! 170: private function foo3() {
! 171: echo "foo3() is called\n";
! 172: }
! 173: }
! 174: /* class with no member */
! 175: class no_member_class {
! 176: // no members
! 177: }
! 178:
! 179: /* class with member as object of other class */
! 180: class contains_object_class
! 181: {
! 182: var $p = 30;
! 183: var $class_object1;
! 184: public $class_object2;
! 185: private $class_object3;
! 186: protected $class_object4;
! 187: var $no_member_class_object;
! 188:
! 189: public function func() {
! 190: echo "func() is called \n";
! 191: }
! 192:
! 193: function contains_object_class () {
! 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
! 200: }
! 201: }
! 202:
! 203: /* objects of different classes */
! 204: $obj = new contains_object_class;
! 205: $temp_class_obj = new object_class();
! 206:
! 207: /* object which is unset */
! 208: $unset_obj = new object_class();
! 209: unset($unset_obj);
! 210:
! 211: $objects = array (
! 212: new object_class,
! 213: new no_member_class,
! 214: new contains_object_class,
! 215: $obj,
! 216: $obj->class_object1,
! 217: $obj->class_object2,
! 218: $obj->no_member_class_object,
! 219: $temp_class_obj,
! 220: @$unset_obj
! 221: );
! 222: /* calling check_printr() to display contents of the objects using print_r() */
! 223: check_printr($objects);
! 224:
! 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);
! 231:
! 232: echo "\n*** Testing print_r() on resources ***\n";
! 233: /* file type resource */
! 234: $file_handle = fopen(__FILE__, "r");
! 235:
! 236: /* directory type resource */
! 237: $dir_handle = opendir( dirname(__FILE__) );
! 238:
! 239: $resources = array (
! 240: $file_handle,
! 241: $dir_handle
! 242: );
! 243: /* calling check_printr() to display the resource content type
! 244: using print_r() */
! 245: check_printr($resources);
! 246:
! 247: echo "\n*** Testing print_r() on different combinations of scalar
! 248: and non-scalar variables ***\n";
! 249: /* a variable which is unset */
! 250: $unset_var = 10.5;
! 251: unset($unset_var);
! 252:
! 253: /* unset file type resource */
! 254: unset($file_handle);
! 255:
! 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')
! 263: );
! 264: /* calling check_printr() to display combinations of scalar and
! 265: non-scalar variables using print_r() */
! 266: check_printr($variations);
! 267:
! 268: echo "\n*** Testing print_r() on miscelleneous input arguments ***\n";
! 269: $misc_values = array (
! 270: @$unset_var,
! 271: NULL, // NULL argument
! 272: @$undef_variable, //undefined variable
! 273: null
! 274: );
! 275: /* calling check_printr() to display miscelleneous data using print_r() */
! 276: check_printr($misc_values);
! 277:
! 278: /* checking print_r() on functions */
! 279: echo "\n*** Testing print_r() on anonymous functions ***\n";
! 280: $newfunc = create_function('$a,$b', 'return "$a * $b = " . ($a * $b);');
! 281: echo "New anonymous function: $newfunc\n";
! 282: print_r( $newfunc(2, 3) );
! 283: /* creating anonymous function dynamically */
! 284: print_r( create_function('$a', 'return "$a * $a = " . ($a * $b);') );
! 285:
! 286: echo "\n\n*** Testing error conditions ***\n";
! 287: //passing zero argument
! 288: var_dump( print_r() );
! 289:
! 290: //passing more than required no. of arguments
! 291: var_dump( print_r(123, true, "abc") );
! 292:
! 293: // check when second arg is given other than boolean TRUE
! 294: var_dump( print_r ($value, "string") );
! 295:
! 296: /* closing resource handle used */
! 297: closedir($dir_handle);
! 298:
! 299: echo "Done\n";
! 300: ?>
! 301: --EXPECTF--
! 302: *** Testing print_r() on integer variables ***
! 303:
! 304: -- Iteration 1 --
! 305: 0
! 306: 0
! 307: 0
! 308: -- Iteration 2 --
! 309: 83
! 310: 83
! 311: 83
! 312: -- Iteration 3 --
! 313: 123000000
! 314: 123000000
! 315: 123000000
! 316: -- Iteration 4 --
! 317: -83
! 318: -83
! 319: -83
! 320: -- Iteration 5 --
! 321: -12300000
! 322: -12300000
! 323: -12300000
! 324: -- Iteration 6 --
! 325: Array
! 326: (
! 327: [0] => 1
! 328: [1] => 2
! 329: [2] => 3
! 330: [3] => 4
! 331: [4] => 5
! 332: [5] => 6
! 333: [6] => 7
! 334: [7] => 8
! 335: [8] => 9
! 336: [9] => 10
! 337: )
! 338:
! 339: Array
! 340: (
! 341: [0] => 1
! 342: [1] => 2
! 343: [2] => 3
! 344: [3] => 4
! 345: [4] => 5
! 346: [5] => 6
! 347: [6] => 7
! 348: [7] => 8
! 349: [8] => 9
! 350: [9] => 10
! 351: )
! 352:
! 353: Array
! 354: (
! 355: [0] => 1
! 356: [1] => 2
! 357: [2] => 3
! 358: [3] => 4
! 359: [4] => 5
! 360: [5] => 6
! 361: [6] => 7
! 362: [7] => 8
! 363: [8] => 9
! 364: [9] => 10
! 365: )
! 366:
! 367: -- Iteration 7 --
! 368: Array
! 369: (
! 370: [0] => -1
! 371: [1] => -2
! 372: [2] => -3
! 373: [3] => -4
! 374: [4] => -5
! 375: [5] => -6
! 376: [6] => -7
! 377: [7] => -8
! 378: [8] => -9
! 379: [9] => -10
! 380: )
! 381:
! 382: Array
! 383: (
! 384: [0] => -1
! 385: [1] => -2
! 386: [2] => -3
! 387: [3] => -4
! 388: [4] => -5
! 389: [5] => -6
! 390: [6] => -7
! 391: [7] => -8
! 392: [8] => -9
! 393: [9] => -10
! 394: )
! 395:
! 396: Array
! 397: (
! 398: [0] => -1
! 399: [1] => -2
! 400: [2] => -3
! 401: [3] => -4
! 402: [4] => -5
! 403: [5] => -6
! 404: [6] => -7
! 405: [7] => -8
! 406: [8] => -9
! 407: [9] => -10
! 408: )
! 409:
! 410: -- Iteration 8 --
! 411: 2147483647
! 412: 2147483647
! 413: 2147483647
! 414: -- Iteration 9 --
! 415: 2147483648
! 416: 2147483648
! 417: 2147483648
! 418: -- Iteration 10 --
! 419: -2147483648
! 420: -2147483648
! 421: -2147483648
! 422: -- Iteration 11 --
! 423: -2147483647
! 424: -2147483647
! 425: -2147483647
! 426: -- Iteration 12 --
! 427: 2147483647
! 428: 2147483647
! 429: 2147483647
! 430: -- Iteration 13 --
! 431: -2147483648
! 432: -2147483648
! 433: -2147483648
! 434: -- Iteration 14 --
! 435: 2147483647
! 436: 2147483647
! 437: 2147483647
! 438: -- Iteration 15 --
! 439: -2147483648
! 440: -2147483648
! 441: -2147483648
! 442: *** Testing print_r() on float variables ***
! 443:
! 444: -- Iteration 1 --
! 445: 0
! 446: 0
! 447: 0
! 448: -- Iteration 2 --
! 449: 0
! 450: 0
! 451: 0
! 452: -- Iteration 3 --
! 453: 1.234
! 454: 1.234
! 455: 1.234
! 456: -- Iteration 4 --
! 457: -1.234
! 458: -1.234
! 459: -1.234
! 460: -- Iteration 5 --
! 461: -2
! 462: -2
! 463: -2
! 464: -- Iteration 6 --
! 465: 2
! 466: 2
! 467: 2
! 468: -- Iteration 7 --
! 469: -0.5
! 470: -0.5
! 471: -0.5
! 472: -- Iteration 8 --
! 473: 0.567
! 474: 0.567
! 475: 0.567
! 476: -- Iteration 9 --
! 477: -0.00067
! 478: -0.00067
! 479: -0.00067
! 480: -- Iteration 10 --
! 481: -670
! 482: -670
! 483: -670
! 484: -- Iteration 11 --
! 485: 670
! 486: 670
! 487: 670
! 488: -- Iteration 12 --
! 489: 670
! 490: 670
! 491: 670
! 492: -- Iteration 13 --
! 493: -0.00410003
! 494: -0.00410003
! 495: -0.00410003
! 496: -- Iteration 14 --
! 497: -4100.03
! 498: -4100.03
! 499: -4100.03
! 500: -- Iteration 15 --
! 501: 0.004100003
! 502: 0.004100003
! 503: 0.004100003
! 504: -- Iteration 16 --
! 505: 4100.003
! 506: 4100.003
! 507: 4100.003
! 508: -- Iteration 17 --
! 509: 100000
! 510: 100000
! 511: 100000
! 512: -- Iteration 18 --
! 513: -100000
! 514: -100000
! 515: -100000
! 516: -- Iteration 19 --
! 517: 1.0E-5
! 518: 1.0E-5
! 519: 1.0E-5
! 520: -- Iteration 20 --
! 521: -1.0E-5
! 522: -1.0E-5
! 523: -1.0E-5
! 524: -- Iteration 21 --
! 525: 100000
! 526: 100000
! 527: 100000
! 528: -- Iteration 22 --
! 529: -100000
! 530: -100000
! 531: -100000
! 532: -- Iteration 23 --
! 533: 100000
! 534: 100000
! 535: 100000
! 536: -- Iteration 24 --
! 537: -100000
! 538: -100000
! 539: -100000
! 540: -- Iteration 25 --
! 541: 100000
! 542: 100000
! 543: 100000
! 544: -- Iteration 26 --
! 545: -100000
! 546: -100000
! 547: -100000
! 548: -- Iteration 27 --
! 549: 1.0E-5
! 550: 1.0E-5
! 551: 1.0E-5
! 552: -- Iteration 28 --
! 553: -1.0E-5
! 554: -1.0E-5
! 555: -1.0E-5
! 556: -- Iteration 29 --
! 557: -2147483649
! 558: -2147483649
! 559: -2147483649
! 560: -- Iteration 30 --
! 561: 2147483649
! 562: 2147483649
! 563: 2147483649
! 564: -- Iteration 31 --
! 565: 2147483649
! 566: 2147483649
! 567: 2147483649
! 568: -- Iteration 32 --
! 569: -2147483649
! 570: -2147483649
! 571: -2147483649
! 572: *** Testing print_r() on string variables ***
! 573:
! 574: -- Iteration 1 --
! 575:
! 576:
! 577:
! 578: -- Iteration 2 --
! 579:
! 580:
! 581:
! 582: -- Iteration 3 --
! 583:
! 584:
! 585:
! 586: -- Iteration 4 --
! 587:
! 588:
! 589:
! 590: -- Iteration 5 --
! 591: 0
! 592: 0
! 593: 0
! 594: -- Iteration 6 --
! 595: