/hphp/test/spec/tests/functions/passing_arguments.php

https://gitlab.com/iranjith4/hhvm · PHP · 145 lines · 80 code · 15 blank · 50 comment · 0 complexity · 75efa32cc48d18ed32524c70fda2f41b MD5 · raw file

  1. <?php
  2. /*
  3. +-------------------------------------------------------------+
  4. | Copyright (c) 2015 Facebook, Inc. (http://www.facebook.com) |
  5. +-------------------------------------------------------------+
  6. */
  7. error_reporting(-1);
  8. // a simple example of passing by value and by reference
  9. function f($p1, &$p2)
  10. {
  11. echo "f In: \$p1: $p1, \$p2: $p2\n";
  12. $p1 = 100; // only the local copy is changed
  13. $p2 = 200; // actual argument's value changed
  14. echo "f Out: \$p1: $p1, \$p2: $p2\n";
  15. }
  16. ///*
  17. $a1 = 10;
  18. $a2 = 20;
  19. var_dump($a1);
  20. var_dump($a2);
  21. f($a1, $a2); // variable $a2 is passed by reference
  22. var_dump($a1);
  23. var_dump($a2);
  24. $a2 = [10,20,30];
  25. var_dump($a2);
  26. f($a1, $a2[1]); // variable $a2[1] is passed by reference
  27. var_dump($a1);
  28. var_dump($a2);
  29. class C
  30. {
  31. public $member;
  32. }
  33. $o = new C;
  34. $o->member = "Red";
  35. var_dump($o);
  36. f($a1, $o->member); // variable $o->member is passed by reference
  37. var_dump($o);
  38. //*/
  39. // passing by reference explored further
  40. function g(&$p1)
  41. {
  42. echo "g In: \$p1: $p1\n";
  43. $p1 = 200; // actual argument's value changed
  44. echo "g Out: \$p1: $p1\n";
  45. }
  46. ///*
  47. $a2 = 0;
  48. // g(TRUE); // PHP5 32/64, Error: Only variables can be passed by reference
  49. // HHVM, Error: Cannot pass parameter 1 by reference
  50. g($a2 = TRUE); // Suspicious "behavior"
  51. // PHP5 32, allowed quietly, but does not change the original argument
  52. // PHP5 64 allowed with warning "PHP Strict standards: Only variables
  53. // should be passed by reference", but does not change the original argument
  54. // HHVM, allowed quietly, but does not change the original argument
  55. var_dump($a2);
  56. $a2 = TRUE;
  57. g($a2); // OK; passing a modifiable lvalue by reference
  58. var_dump($a2);
  59. // following tests have different values for $a2, and give results like the case above
  60. // g(-123);
  61. g($a2 = -123);
  62. var_dump($a2);
  63. $a2 = -123;
  64. g($a2);
  65. var_dump($a2);
  66. // g(1.23e3);
  67. g($a2 = 1.23e3);
  68. var_dump($a2);
  69. $a2 = 1.23e3;
  70. g($a2);
  71. var_dump($a2);
  72. // g(NULL);
  73. g($a2 = NULL);
  74. var_dump($a2);
  75. $a2 = NULL;
  76. g($a2);
  77. var_dump($a2);
  78. // g("abc");
  79. g($a2 = "abc");
  80. var_dump($a2);
  81. $a2 = "abc";
  82. g($a2);
  83. var_dump($a2);
  84. // g([1,2,3]);
  85. g($a2 = [1,2,3]);
  86. var_dump($a2);
  87. $a2 = [1,2,3];
  88. g($a2);
  89. var_dump($a2);
  90. //*/
  91. ///*
  92. // based on finding from above, further testing
  93. $z = 10;
  94. // ($z = 5) = 20; // All 3, Error: unexpected '='
  95. // ($z = 5)++; // All 3, Error: unexpected '++'
  96. // ($z += 5) = 20; // All 3, Error: unexpected '='
  97. // ($z += 5)++; // All 3, Error: unexpected '++'
  98. //*/
  99. ///*
  100. $z = 10;
  101. g($z); // create a reference to a modifiable lvalue
  102. var_dump($z);
  103. // g($z + 1); // PHP5 32/64, Error: Only variables can be passed by reference
  104. // HHVM, Error: Cannot pass parameter 1 by reference
  105. g($z -= 2); // PHP5 32, allowed quietly, but does not change the original argument
  106. // PHP5 64 allowed with warning "PHP Strict standards: Only variables
  107. // should be passed by reference", but does not change the original argument
  108. // HHVM, allowed quietly, but does not change the original argument
  109. var_dump($z);
  110. g($z = $z - 2); // same as above
  111. var_dump($z);
  112. // g($z++); // PHP5 32/64, Error: Only variables can be passed by reference
  113. // HHVM, Error: Cannot pass parameter 1 by reference
  114. // ($z++)++; // All 3, Error: unexpected '++'
  115. g(--$z); // PHP5 32, allowed quietly, but does not change the original argument
  116. // PHP5 64 allowed with warning "PHP Strict standards: Only variables
  117. // should be passed by reference", but does not change the original argument
  118. // HHVM, allowed quietly, but does not change the original argument
  119. // ----$z; // All 3, Error: unexpected '--'
  120. var_dump($z);
  121. //*/
  122. ///*
  123. function k() { return 10; }
  124. var_dump(k());
  125. g(k()); // PHP5 32, allowed quietly, but (presumably) does not change the original argument
  126. // PHP5 64 allowed with warning "PHP Strict standards: Only variables
  127. // should be passed by reference", but (presumably) does not change the original argument
  128. // k() = 10; // All 3, Error: Can't use function return value in write context
  129. // HHVM, allowed quietly, but (presumably) does not change the original argument
  130. //*/