1--TEST-- 2Test mb_strlen() function : usage variations - Pass different data types as $str arg 3--SKIPIF-- 4<?php 5extension_loaded('mbstring') or die('skip'); 6function_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/* 16 * Test mb_strlen by passing different data types as $str argument 17 */ 18 19echo "*** Testing mb_strlen() : usage variations ***\n"; 20 21// Initialise function arguments not being substituted 22$encoding = 'utf-8'; 23 24//get an unset variable 25$unset_var = 10; 26unset ($unset_var); 27 28// get a class 29class classA 30{ 31 public function __toString() { 32 return b"Class A object"; 33 } 34} 35 36// heredoc string 37$heredoc = b<<<EOT 38hello world 39EOT; 40 41// get a resource variable 42$fp = fopen(__FILE__, "r"); 43 44// unexpected values to be passed to $str argument 45$inputs = array( 46 47 // int data 48/*1*/ 0, 49 1, 50 12345, 51 -2345, 52 53 // float data 54/*5*/ 10.5, 55 -10.5, 56 12.3456789000e10, 57 12.3456789000E-10, 58 .5, 59 60 // null data 61/*10*/ NULL, 62 null, 63 64 // boolean data 65/*12*/ true, 66 false, 67 TRUE, 68 FALSE, 69 70 // empty data 71/*16*/ "", 72 '', 73 74 // string data 75/*18*/ b"string", 76 b'string', 77 $heredoc, 78 79 // object data 80/*21*/ new classA(), 81 82 // undefined data 83/*22*/ @$undefined_var, 84 85 // unset data 86/*23*/ @$unset_var, 87 88 // resource variable 89/*24*/ $fp 90); 91 92// loop through each element of $inputs to check the behavior of mb_strlen() 93$iterator = 1; 94foreach($inputs as $input) { 95 echo "\n-- Iteration $iterator --\n"; 96 var_dump( mb_strlen($input, $encoding)); 97 $iterator++; 98}; 99 100fclose($fp); 101 102echo "Done"; 103?> 104--EXPECTF-- 105*** Testing mb_strlen() : usage variations *** 106 107-- Iteration 1 -- 108int(1) 109 110-- Iteration 2 -- 111int(1) 112 113-- Iteration 3 -- 114int(5) 115 116-- Iteration 4 -- 117int(5) 118 119-- Iteration 5 -- 120int(4) 121 122-- Iteration 6 -- 123int(5) 124 125-- Iteration 7 -- 126int(12) 127 128-- Iteration 8 -- 129int(13) 130 131-- Iteration 9 -- 132int(3) 133 134-- Iteration 10 -- 135int(0) 136 137-- Iteration 11 -- 138int(0) 139 140-- Iteration 12 -- 141int(1) 142 143-- Iteration 13 -- 144int(0) 145 146-- Iteration 14 -- 147int(1) 148 149-- Iteration 15 -- 150int(0) 151 152-- Iteration 16 -- 153int(0) 154 155-- Iteration 17 -- 156int(0) 157 158-- Iteration 18 -- 159int(6) 160 161-- Iteration 19 -- 162int(6) 163 164-- Iteration 20 -- 165int(11) 166 167-- Iteration 21 -- 168int(14) 169 170-- Iteration 22 -- 171int(0) 172 173-- Iteration 23 -- 174int(0) 175 176-- Iteration 24 -- 177 178Warning: mb_strlen() expects parameter 1 to be string, resource given in %s on line %d 179bool(false) 180Done