offset_string.phpt 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. --TEST--
  2. using different variables to access string offsets
  3. --FILE--
  4. <?php
  5. $str = "Sitting on a corner all alone, staring from the bottom of his soul";
  6. var_dump($str[1]);
  7. var_dump($str[0.0836]);
  8. var_dump($str[NULL]);
  9. try {
  10. var_dump($str["run away"]);
  11. } catch (\TypeError $e) {
  12. echo $e->getMessage() . \PHP_EOL;
  13. }
  14. var_dump($str["13"]);
  15. try {
  16. var_dump($str["14.5"]);
  17. } catch (\TypeError $e) {
  18. echo $e->getMessage() . \PHP_EOL;
  19. }
  20. var_dump($str["15 and then some"]);
  21. var_dump($str[TRUE]);
  22. var_dump($str[FALSE]);
  23. $fp = fopen(__FILE__, "r");
  24. try {
  25. var_dump($str[$fp]);
  26. } catch (Error $e) {
  27. echo $e->getMessage(), "\n";
  28. }
  29. $obj = new stdClass;
  30. try {
  31. var_dump($str[$obj]);
  32. } catch (Error $e) {
  33. echo $e->getMessage(), "\n";
  34. }
  35. $arr = Array(1,2,3);
  36. try {
  37. var_dump($str[$arr]);
  38. } catch (Error $e) {
  39. echo $e->getMessage(), "\n";
  40. }
  41. echo "Done\n";
  42. ?>
  43. --EXPECTF--
  44. string(1) "i"
  45. Warning: String offset cast occurred in %s on line %d
  46. string(1) "S"
  47. Warning: String offset cast occurred in %s on line %d
  48. string(1) "S"
  49. Cannot access offset of type string on string
  50. string(1) "c"
  51. Cannot access offset of type string on string
  52. Warning: Illegal string offset "15 and then some" in %s on line %d
  53. string(1) "r"
  54. Warning: String offset cast occurred in %s on line %d
  55. string(1) "i"
  56. Warning: String offset cast occurred in %s on line %d
  57. string(1) "S"
  58. Cannot access offset of type resource on string
  59. Cannot access offset of type stdClass on string
  60. Cannot access offset of type array on string
  61. Done