String is probably the most used data type in PHP as it is a templating language in nature. PHP string counts for a big part of your coding. Not to mention most of us use PHP to do what it does the best – serving web pages, involving a lot of strings all the way.
Okay, let’s cut the bullshit. To get or count the length of a php string, you need the strlen() function.
<?php
$str = 'Hi.';
$length = strlen($str); // $length equals to 3 as there are 3 characters in the string 'Hi.'
?>
A slightly better approach, is to examine the existence of the nth character of a string to determine the length of a string:
if (isset($str[5])) { // $str[5] is the 6th character of $str
echo 'This string is at least 6 bytes in size (6 characters long).';
}
The reasoning of this is that strlen is a function while isset is a language construct which should come faster. Not to mention that intuitively, counting the length of a string from the first byte to the last takes intrinsically more time than the determination of the existence of just one byte.
One reply on “PHP String Length function to Get Length of Strings in PHP”
function benchmark_function($fn,$args=null)
{
if(!is_callable($fn))
{
trigger_error(“Call to undefined function $fn()”,E_USER_ERROR);
}
$t = microtime(true);
$r = call_user_func_array($fn,$args);
return array(“time”=>(microtime(true)-$t),”returned”=>$r,”fn”=>$fn);
}
function bit_round($n,$u=false)
{
return ($n+(float)$u)>>1;
}
function get_len_loop($s)
{
while($s[$i++]){}
return $i-1;
}
$t = “kejhkhfkewkfhkwjfjrw”;
echo var_dump(benchmark_function(“strlen”,$t)).””;
echo var_dump(benchmark_function(“get_len_loop”,$t)).””;
$tm = microtime(true);
$r = isset($t[9]);
$a = array(“time”=>(microtime(true)-$tm),”returned”=>$r,”fn”=>$fn);
echo var_dump($a);
output:
run 1:
array(3) { [“time”]=> float(3.814697265625E-6) [“returned”]=> int(20) [“fn”]=> string(6) “strlen” }
array(3) { [“time”]=> float(1.5020370483398E-5) [“returned”]=> int(20) [“fn”]=> string(12) “get_len_loop” }
array(3) { [“time”]=> float(4.0531158447266E-6) [“returned”]=> bool(true) [“fn”]=> string(5) “isset” }
run 2:
array(3) { [“time”]=> float(3.0994415283203E-6) [“returned”]=> int(20) [“fn”]=> string(6) “strlen” }
array(3) { [“time”]=> float(1.5020370483398E-5) [“returned”]=> int(20) [“fn”]=> string(12) “get_len_loop” }
array(3) { [“time”]=> float(4.0531158447266E-6) [“returned”]=> bool(true) [“fn”]=> string(5) “isset” }
run 3:
array(3) { [“time”]=> float(4.0531158447266E-6) [“returned”]=> int(20) [“fn”]=> string(6) “strlen” }
array(3) { [“time”]=> float(1.1920928955078E-5) [“returned”]=> int(20) [“fn”]=> string(12) “get_len_loop” }
array(3) { [“time”]=> float(3.0994415283203E-6) [“returned”]=> bool(true) [“fn”]=> string(5) “isset” }
run 4:
array(3) { [“time”]=> float(3.814697265625E-6) [“returned”]=> int(20) [“fn”]=> string(6) “strlen” }
array(3) { [“time”]=> float(1.4066696166992E-5) [“returned”]=> int(20) [“fn”]=> string(12) “get_len_loop” }
array(3) { [“time”]=> float(4.0531158447266E-6) [“returned”]=> bool(true) [“fn”]=> string(5) “isset” }
run 5:
array(3) { [“time”]=> float(2.8610229492188E-6) [“returned”]=> int(20) [“fn”]=> string(6) “strlen” }
array(3) { [“time”]=> float(1.5020370483398E-5) [“returned”]=> int(20) [“fn”]=> string(12) “get_len_loop” }
array(3) { [“time”]=> float(2.8610229492188E-6) [“returned”]=> bool(true) [“fn”]=> string(5) “isset” }
it turns out that making a while inside a function is waaaaaaay faster!!!
tested on:
http://writecodeonline.com/php/