Skip to content

Instantly share code, notes, and snippets.

@iNem0o
Last active December 15, 2015 12:59
Show Gist options
  • Save iNem0o/5263718 to your computer and use it in GitHub Desktop.
Save iNem0o/5263718 to your computer and use it in GitHub Desktop.
Vérifier la longueur d'une chaine en PHP : strlen() vs isset()
<?php
$string = "hello world";
$taille = 5;
$t = microtime(true);
for($i = 0;$i<10000;$i++) {
if(strlen($string) >= $taille) {
}
}
echo '<br> strlen($string) > 5 : '.round((microtime(true)-$t)*1000,2)." ms";
$t = microtime(true);
for($i = 0;$i<10000;$i++) {
if(isset($string[$taille])) {
}
}
echo '<br> isset($string[5]) : '.round((microtime(true)-$t)*1000,2)." ms";
?>
Résultats :
strlen($string) > 5 : 25.85 ms
isset($string[5]) : 4.02 ms
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment