Jump to content

Identical operator and equals operator


kdsxchris

Recommended Posts

For designing a large php application, when it comes to speed and overall efficiency, which should be used, the identical === or equals == operator? I like the === operator, because it is type safe and I'm real careful about type safety after using Java and becoming certified in it with all of the type stuff in Java...

Link to comment
https://forums.phpfreaks.com/topic/54027-identical-operator-and-equals-operator/
Share on other sites

both are equally as fast... and both are quite safe... but personally... i only use === when its type specific... more or less... bool... if($var!==false)... php will automatically translate int/string to see if theres a common ground if you use ==... both are just as secure :-) === will save you alot of troubles with bool tho :-)

Well they're two different things...

== is to match the value regardless of its type.

=== matches the type aswell as the value.

 

 

Basically

If you wanted to match the types:

$num = 1;
if ($num === "1")
{
//$num is a string with the value 1
}

but if you just want to match values:

$num = 1;
if ($num == "1")
{
//it doesnt matter if it is a string or not, as long as it is 1
}

 

So as Orio said, use what ever fits the situation

It matters in cases like strpos

 

<?php
$str = 'abc';

$p1 = strpos ($str, 'a');

if ($p1 != false) 
    echo "Found";
else
    echo "Not found";
?>

 

The above gives "Not found" since 'a' is at position 0, whereas

 

<?php
$str = 'abc';

$p1 = strpos ($str, 'a');

if ($p1 !== false) 
    echo "Found";
else
    echo "Not found";
?>

 

correctly gives "Found"

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.