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
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 :-)

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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"

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.