Home > Backend Development > PHP Tutorial > To compare two strings for equality in PHP, which one is more efficient, == or strcmp()?

To compare two strings for equality in PHP, which one is more efficient, == or strcmp()?

WBOY
Release: 2016-08-18 09:16:31
Original
2311 people have browsed it

To compare whether two strings are equal in PHP, which one is more efficient, == or strcmp()?

Reply content:

To compare two strings for equality in PHP, which one is more efficient, == or strcmp()?

I think you still need to practice more on your own.

<code><?php
$time1 = microtime(true);
$a = $b = 'hello,world!';
$i = $j = 100000000;
while($i>0){
    $i--;
    if($a==$b) ;
}
$time2 = microtime(true);
while($j>0){
    $j--;
    if(strcmp($a,$b));
}
$time3 = microtime(true);

echo ($time2 - $time1)."\n";
echo ($time3 - $time2);

//result
//$ php demo.php
//1.9449820518494
//7.9897949695587
</code>
Copy after login

It is definitely == that is more efficient, because the operator saves memory than function calls.

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template