Ken
Ken

Reputation: 1091

Double equals and tripple equals in php

I searched on StackOverflow and Google and I can't find the answer to this question:

Should we always use the triple equal in PHP for validation?

For example, I have a variable:

$x = '1';

if($x == 1)  // will work
if($x === 1) // will not

Now, my point is if we need to validate numeric fields like:

if(is_numeric($x) && $x == '1') { will be the equivalent to if($x === 1)

Since === also validate the type, will it be better if we always use the ===?

Upvotes: 7

Views: 1641

Answers (7)

Alex
Alex

Reputation: 694

From http://me.veekun.com/blog/2012/04/09/php-a-fractal-of-bad-design/

== is useless.

‣ It’s not transitive. "foo" == TRUE, and "foo" == 0… but, of course, TRUE != 0.

‣ == converts to numbers when possible, which means it converts to floats when possible. So large hex strings (like, say, password hashes) may occasionally compare true when they’re not. Even JavaScript doesn’t do this.

‣ For the same reason, "6" == " 6", "4.2" == "4.20", and "133" == "0133". But note that 133 != 0133, because 0133 is octal.

‣ === compares values and type… except with objects, where === is only true if both operands are actually the same object! For objects, == compares both value (of every attribute) and type, which is what === does for every other type. What.

See http://habnab.it/php-table.html

And http://phpsadness.com/sad/47

And http://developers.slashdot.org/comments.pl?sid=204433&cid=16703529

That being said, when you are absolutely sure type is not an issue when you are creating simple expressions, == works well enough in my experience. Just be vigilant.

Upvotes: 6

clyfe
clyfe

Reputation: 23770

It depends on what you want to do.
Given that from forms, data comes as strings, == is handy because it can compare, for example, strings that represent numbers with numbers with no additional type casting.

if ($_GET['amount'] == 10) {
    //...
}

So no, it's not better to always use ===.

Upvotes: 3

Drahcir
Drahcir

Reputation: 11972

It depends entirely on the script you're writing, there's not one correct answer for this. Having said that, there aren't many situations where you don't already know the type of the variable (except perhaps user input).

This is the reason I stick to using == and only use === when there could be more than one type of the variable.

The == is fine most of the time, it wouldn't have been invented if you weren't supposed to use it :)

Upvotes: 5

Charles Sprayberry
Charles Sprayberry

Reputation: 7853

if (is_numeric($x) && $x == '1') { ...

This looks redundant to me. Why do we need to check if $x is_numeric AND the value '1'? We know '1' is numeric so if it is equal to '1' then it must be a number.


You could use === comparison:

If you're fine with interpreting it as a string:

if ($x === '1') { ...

or

If you must interpret the value as an int

if ((int) $x === 1) { ...

or

If you don't care about the actual type:

if ($x == '1') { ...

Upvotes: 1

Ken Wayne VanderLinde
Ken Wayne VanderLinde

Reputation: 19339

If you want such a strong validation, then the answer is: yes, definitely use ===.

The == also does some very weird things, like comparing completely different string as equal, just because they are numerically equivalent. So === will probably be a better tool for you in most situations.

Upvotes: -1

vertazzar
vertazzar

Reputation: 1053

if you're expecting that variable that you're passing will (and must) be integer than you should use triple equal, if not than you should avoid that.

Although, if you really want to use === than you should be doing conversion of variables to the type that you want along the way, like on this example:

if ((int) $var === 1)
{

    // will return true if the $var is 1
}

Upvotes: 0

Fox
Fox

Reputation: 2368

I would say it is better to always use === and remove one = in cases you can justify.

And yes it's equal, though weird. Better way to write it is if(is_numeric($x) && $x == 1)

Upvotes: 0

Related Questions