string_offset.phpt 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. --TEST--
  2. Using different sorts of numerical strings as a string offset
  3. --FILE--
  4. <?php
  5. $str = "The world is fun";
  6. $keys = [
  7. "7",
  8. "7.5",
  9. " 7",
  10. " 7.5",
  11. " 7 ",
  12. " 7.5 ",
  13. "7 ",
  14. "7.5 ",
  15. "7str",
  16. "7.5str",
  17. " 7str",
  18. " 7.5str",
  19. " 7 str",
  20. " 7.5 str",
  21. "7 str",
  22. "7.5 str",
  23. "0xC",
  24. "0b10",
  25. "07",
  26. ];
  27. foreach ($keys as $key) {
  28. try {
  29. var_dump($str[$key]);
  30. } catch (\TypeError $e) {
  31. echo $e->getMessage() . \PHP_EOL;
  32. }
  33. }
  34. echo "Done\n";
  35. ?>
  36. --EXPECTF--
  37. string(1) "l"
  38. Cannot access offset of type string on string
  39. string(1) "l"
  40. Cannot access offset of type string on string
  41. string(1) "l"
  42. Cannot access offset of type string on string
  43. string(1) "l"
  44. Cannot access offset of type string on string
  45. Warning: Illegal string offset "7str" in %s on line %d
  46. string(1) "l"
  47. Cannot access offset of type string on string
  48. Warning: Illegal string offset " 7str" in %s on line %d
  49. string(1) "l"
  50. Cannot access offset of type string on string
  51. Warning: Illegal string offset " 7 str" in %s on line %d
  52. string(1) "l"
  53. Cannot access offset of type string on string
  54. Warning: Illegal string offset "7 str" in %s on line %d
  55. string(1) "l"
  56. Cannot access offset of type string on string
  57. Warning: Illegal string offset "0xC" in %s on line %d
  58. string(1) "T"
  59. Warning: Illegal string offset "0b10" in %s on line %d
  60. string(1) "T"
  61. string(1) "l"
  62. Done