mb_strlen_variation1.phpt 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. --TEST--
  2. Test mb_strlen() function : usage variations - Pass different data types as $str arg
  3. --SKIPIF--
  4. <?php
  5. extension_loaded('mbstring') or die('skip');
  6. function_exists('mb_strlen') or die("skip mb_strlen() is not available in this build");
  7. ?>
  8. --FILE--
  9. <?php
  10. /* Prototype : int mb_strlen(string $str [, string $encoding])
  11. * Description: Get character numbers of a string
  12. * Source code: ext/mbstring/mbstring.c
  13. */
  14. /*
  15. * Test mb_strlen by passing different data types as $str argument
  16. */
  17. echo "*** Testing mb_strlen() : usage variations ***\n";
  18. // Initialise function arguments not being substituted
  19. $encoding = 'utf-8';
  20. //get an unset variable
  21. $unset_var = 10;
  22. unset ($unset_var);
  23. // get a class
  24. class classA
  25. {
  26. public function __toString() {
  27. return b"Class A object";
  28. }
  29. }
  30. // heredoc string
  31. $heredoc = b<<<EOT
  32. hello world
  33. EOT;
  34. // get a resource variable
  35. $fp = fopen(__FILE__, "r");
  36. // unexpected values to be passed to $str argument
  37. $inputs = array(
  38. // int data
  39. /*1*/ 0,
  40. 1,
  41. 12345,
  42. -2345,
  43. // float data
  44. /*5*/ 10.5,
  45. -10.5,
  46. 12.3456789000e10,
  47. 12.3456789000E-10,
  48. .5,
  49. // null data
  50. /*10*/ NULL,
  51. null,
  52. // boolean data
  53. /*12*/ true,
  54. false,
  55. TRUE,
  56. FALSE,
  57. // empty data
  58. /*16*/ "",
  59. '',
  60. // string data
  61. /*18*/ b"string",
  62. b'string',
  63. $heredoc,
  64. // object data
  65. /*21*/ new classA(),
  66. // undefined data
  67. /*22*/ @$undefined_var,
  68. // unset data
  69. /*23*/ @$unset_var,
  70. // resource variable
  71. /*24*/ $fp
  72. );
  73. // loop through each element of $inputs to check the behavior of mb_strlen()
  74. $iterator = 1;
  75. foreach($inputs as $input) {
  76. echo "\n-- Iteration $iterator --\n";
  77. var_dump( mb_strlen($input, $encoding));
  78. $iterator++;
  79. };
  80. fclose($fp);
  81. echo "Done";
  82. ?>
  83. --EXPECTF--
  84. *** Testing mb_strlen() : usage variations ***
  85. -- Iteration 1 --
  86. int(1)
  87. -- Iteration 2 --
  88. int(1)
  89. -- Iteration 3 --
  90. int(5)
  91. -- Iteration 4 --
  92. int(5)
  93. -- Iteration 5 --
  94. int(4)
  95. -- Iteration 6 --
  96. int(5)
  97. -- Iteration 7 --
  98. int(12)
  99. -- Iteration 8 --
  100. int(13)
  101. -- Iteration 9 --
  102. int(3)
  103. -- Iteration 10 --
  104. int(0)
  105. -- Iteration 11 --
  106. int(0)
  107. -- Iteration 12 --
  108. int(1)
  109. -- Iteration 13 --
  110. int(0)
  111. -- Iteration 14 --
  112. int(1)
  113. -- Iteration 15 --
  114. int(0)
  115. -- Iteration 16 --
  116. int(0)
  117. -- Iteration 17 --
  118. int(0)
  119. -- Iteration 18 --
  120. int(6)
  121. -- Iteration 19 --
  122. int(6)
  123. -- Iteration 20 --
  124. int(11)
  125. -- Iteration 21 --
  126. int(14)
  127. -- Iteration 22 --
  128. int(0)
  129. -- Iteration 23 --
  130. int(0)
  131. -- Iteration 24 --
  132. Warning: mb_strlen() expects parameter 1 to be string, resource given in %s on line %d
  133. bool(false)
  134. Done