001.phpt 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. --TEST--
  2. throw expression
  3. --FILE--
  4. <?php
  5. try {
  6. $result = true && throw new Exception("true && throw");
  7. var_dump($result);
  8. } catch (Exception $e) {
  9. var_dump($e->getMessage());
  10. }
  11. try {
  12. $result = false && throw new Exception("false && throw");
  13. var_dump($result);
  14. } catch (Exception $e) {
  15. var_dump($e->getMessage());
  16. }
  17. try {
  18. $result = true and throw new Exception("true and throw");
  19. var_dump($result);
  20. } catch (Exception $e) {
  21. var_dump($e->getMessage());
  22. }
  23. try {
  24. $result = false and throw new Exception("false and throw");
  25. var_dump($result);
  26. } catch (Exception $e) {
  27. var_dump($e->getMessage());
  28. }
  29. try {
  30. $result = true || throw new Exception("true || throw");
  31. var_dump($result);
  32. } catch (Exception $e) {
  33. var_dump($e->getMessage());
  34. }
  35. try {
  36. $result = false || throw new Exception("false || throw");
  37. var_dump($result);
  38. } catch (Exception $e) {
  39. var_dump($e->getMessage());
  40. }
  41. try {
  42. $result = true or throw new Exception("true or throw");
  43. var_dump($result);
  44. } catch (Exception $e) {
  45. var_dump($e->getMessage());
  46. }
  47. try {
  48. $result = false or throw new Exception("false or throw");
  49. var_dump($result);
  50. } catch (Exception $e) {
  51. var_dump($e->getMessage());
  52. }
  53. try {
  54. $result = null ?? throw new Exception("null ?? throw");
  55. var_dump($result);
  56. } catch (Exception $e) {
  57. var_dump($e->getMessage());
  58. }
  59. try {
  60. $result = "foo" ?? throw new Exception('"foo" ?? throw');
  61. var_dump($result);
  62. } catch (Exception $e) {
  63. var_dump($e->getMessage());
  64. }
  65. try {
  66. $result = null ?: throw new Exception("null ?: throw");
  67. var_dump($result);
  68. } catch (Exception $e) {
  69. var_dump($e->getMessage());
  70. }
  71. try {
  72. $result = "foo" ?: throw new Exception('"foo" ?: throw');
  73. var_dump($result);
  74. } catch (Exception $e) {
  75. var_dump($e->getMessage());
  76. }
  77. try {
  78. $callable = fn() => throw new Exception("fn() => throw");
  79. var_dump("not yet");
  80. $callable();
  81. } catch (Exception $e) {
  82. var_dump($e->getMessage());
  83. }
  84. $result = "bar";
  85. try {
  86. $result = throw new Exception();
  87. } catch (Exception $e) {}
  88. var_dump($result);
  89. try {
  90. var_dump(
  91. throw new Exception("exception 1"),
  92. throw new Exception("exception 2")
  93. );
  94. } catch (Exception $e) {
  95. var_dump($e->getMessage());
  96. }
  97. try {
  98. $result = true ? true : throw new Exception("true ? true : throw");
  99. var_dump($result);
  100. } catch (Exception $e) {
  101. var_dump($e->getMessage());
  102. }
  103. try {
  104. $result = false ? true : throw new Exception("false ? true : throw");
  105. var_dump($result);
  106. } catch (Exception $e) {
  107. var_dump($e->getMessage());
  108. }
  109. try {
  110. throw new Exception() + 1;
  111. } catch (Throwable $e) {
  112. var_dump($e->getMessage());
  113. }
  114. try {
  115. throw $exception = new Exception('throw $exception = new Exception();');
  116. } catch (Exception $e) {}
  117. var_dump($exception->getMessage());
  118. try {
  119. $exception = null;
  120. throw $exception ??= new Exception('throw $exception ??= new Exception();');
  121. } catch (Exception $e) {}
  122. var_dump($exception->getMessage());
  123. try {
  124. throw null ?? new Exception('throw null ?? new Exception();');
  125. } catch (Exception $e) {
  126. var_dump($e->getMessage());
  127. }
  128. ?>
  129. --EXPECT--
  130. string(13) "true && throw"
  131. bool(false)
  132. string(14) "true and throw"
  133. bool(false)
  134. bool(true)
  135. string(14) "false || throw"
  136. bool(true)
  137. string(14) "false or throw"
  138. string(13) "null ?? throw"
  139. string(3) "foo"
  140. string(13) "null ?: throw"
  141. string(3) "foo"
  142. string(7) "not yet"
  143. string(13) "fn() => throw"
  144. string(3) "bar"
  145. string(11) "exception 1"
  146. bool(true)
  147. string(20) "false ? true : throw"
  148. string(42) "Unsupported operand types: Exception + int"
  149. string(35) "throw $exception = new Exception();"
  150. string(37) "throw $exception ??= new Exception();"
  151. string(30) "throw null ?? new Exception();"