PageRenderTime 44ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/hphp/test/zend/bad/ext/standard/tests/strings/strlen.php

http://github.com/facebook/hiphop-php
PHP | 145 lines | 114 code | 22 blank | 9 comment | 1 complexity | 02720e25dbb4b870a7b23f025d36b9f0 MD5 | raw file
Possible License(s): LGPL-2.1, BSD-2-Clause, BSD-3-Clause, MPL-2.0-no-copyleft-exception, MIT, LGPL-2.0, Apache-2.0
  1. <?php
  2. /* returns the length of a given string */
  3. echo "#### Basic operations and variations ####\n";
  4. $strings = array( "Hello, World",
  5. 'Hello, World',
  6. '!!Hello, World',
  7. "??Hello, World",
  8. "$@#%^&*!~,.:;?",
  9. "123",
  10. 123,
  11. "-1.2345",
  12. -1.2344,
  13. NULL,
  14. "",
  15. " ",
  16. "\0",
  17. "\x000", // len = 2
  18. "\xABC", // len = 2
  19. "\0000", // len = 2
  20. "0",
  21. 0,
  22. "\t", // len = 1
  23. '\t', // len = 2
  24. TRUE,
  25. FALSE,
  26. "Hello, World\0",
  27. "Hello\0World",
  28. 'Hello, World\0',
  29. "Hello, World\n",
  30. "Hello, World\r",
  31. "Hello, World\t",
  32. "Hello, World\\",
  33. " ",
  34. chr(128).chr(234).chr(65).chr(255).chr(256),
  35. "abcdefghijklmnopqrstuvwxyz0123456789~!@#$%^&*()_+=|?><-;:$
  36. []{}{{{}}}[[[[]][]]]***&&&^^%$###@@!!@#$%&^&**/////|\\\\\\
  37. abcdefghijklmnopqrstuvwxyz0123456789~!@#$%^&*()_+=|?><-;:$
  38. []{}{{{}}}[[[[]][]]]***&&&^^%$###@@!!@#$%&^&**/////|\\\\\\
  39. abcdefghijklmnopqrstuvwxyz0123456789~!@#$%^&*()_+=|?><-;:$
  40. []{}{{{}}}[[[[]][]]]***&&&^^%$###@@!!@#$%&^&**/////|\\\\\\
  41. abcdefghijklmnopqrstuvwxyz0123456789"
  42. );
  43. /* loop through to find the length of each string */
  44. for($i=0; $i<count($strings); $i++) {
  45. echo "String length of '$strings[$i]' is => ";
  46. var_dump( strlen($strings[$i]) );
  47. }
  48. echo "\n#### Testing Miscelleneous inputs ####\n";
  49. echo "--- Testing objects ---\n";
  50. /* we get "Catchable fatal error: saying Object of class could not be converted
  51. to string" by default when an object is passed instead of string:
  52. The error can be avoided by choosing the __toString magix method as follows: */
  53. class string {
  54. function __toString() {
  55. return "Hello, world";
  56. }
  57. }
  58. $obj_string = new string;
  59. var_dump(strlen("$obj_string"));
  60. echo "\n--- Testing arrays ---\n";
  61. $str_arr = array("hello", "?world", "!$%**()%**[][[[&@#~!", array());
  62. var_dump(strlen($str_arr));
  63. var_dump(strlen("$str_arr[1]"));
  64. var_dump(strlen("$str_arr[2]"));
  65. echo "\n--- Testing Resources ---\n";
  66. $filename1 = "dummy.txt";
  67. $file1 = fopen($filename1, "w"); // creating new file
  68. /* getting resource type for file handle */
  69. $string1 = get_resource_type($file1);
  70. $string2 = (int)get_resource_type($file1); // converting stream type to int
  71. /* $string1 is of "stream" type */
  72. var_dump(strlen($string1)); // int(6)
  73. /* $string2 holds a value of "int(0)" */
  74. var_dump(strlen($string2)); // int(1)
  75. fclose($file1); // closing the file "dummy.txt"
  76. unlink("$filename1"); // deletes "dummy.txt"
  77. echo "\n--- Testing a longer and heredoc string ---\n";
  78. $string = <<<EOD
  79. abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
  80. abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
  81. abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
  82. abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
  83. abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
  84. abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
  85. abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
  86. @#$%^&**&^%$#@!~:())))((((&&&**%$###@@@!!!~~~~@###$%^&*
  87. abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
  88. EOD;
  89. var_dump(strlen($string));
  90. echo "\n--- Testing a heredoc null string ---\n";
  91. $str = <<<EOD
  92. EOD;
  93. var_dump(strlen($str));
  94. echo "\n--- Testing simple and complex syntax strings ---\n";
  95. $str = 'world';
  96. /* Simple syntax */
  97. var_dump(strlen("$str"));
  98. var_dump(strlen("$str'S"));
  99. var_dump(strlen("$strS"));
  100. /* String with curly braces, complex syntax */
  101. var_dump(strlen("${str}S"));
  102. var_dump(strlen("{$str}S"));
  103. echo "\n--- strlen for long float values ---\n";
  104. /* Here two different outputs, which depends on the rounding value
  105. before converting to string. Here Precision = 12 */
  106. var_dump(strlen(10.55555555555555555555555555)); // len = 13
  107. var_dump(strlen(10.55555555595555555555555555)); // len = 12
  108. echo "\n--- Nested strlen() ---\n";
  109. var_dump(strlen(strlen("Hello"))); // len=1
  110. echo "\n#### error conditions ####";
  111. /* Zero arguments */
  112. strlen();
  113. /* Greater number of args than expected */
  114. strlen("string1", "string2");
  115. strlen("", "");
  116. echo "Done\n";
  117. ?>