can === and != be used interchangably? - php

Can the relational operator === (used for identical) be used interchangeably with the != operator" and get the same results? Or will I eventually run into issues later down the road when I do larger programs?
I know I will get the same results in the example below, will this always be true?
//example 1
<?php
$a = 1; //integer
$b = '1'; //string
if ($a === $b) {
echo 'Values and types are same';
}
else {
echo 'Values and types are not same';
}
?>
// example 2
<?php
$a = 1; //integer
$b = '1'; //string
if ($a != $b) {
echo 'Values and types are not same';
}
else {
echo 'Values and types are same';
}
?>

Short answer is, no, you can't interchange them because they check for different things. They are not equivalent operators.
You'll want to use !==
It basically means both values being compared must be of the same type.
When you use ==, the values being compared are typecast if needed.
As you know, === checks the types also.
When you use !=, the values are also typecast, whereas !== checks the values and type, strictly.

You're essentially asking whether !($a != $b) will always be identical to $a === $b. The simple answer is: no. !($a != $b) can be boiled down to $a == $b, which is obviously not the same as $a === $b:
php > var_dump(!('0' != 0));
bool(true)
php > var_dump('0' === 0);
bool(false)
!== is obviously the opposite of ===, so !($a !== $b) will always be identical to $a === $b.

Related

Negation (!) does not change the outcome of preg_match in PHP [duplicate]

In PHP, is
if(!$foo)
equivalent with
if($foo != true)
or with
if($foo !== true)
or is it even something completly different of both?
Note that,
== OR != compares the values of variables for equality, type casting as necessary. === OR !== checks if the two variables are of the same type AND have the same value.
This answer will give you better explanation of this concept:
https://stackoverflow.com/a/80649/3067928
if(!$foo)
is the equivalent to
if($foo != true)
so
$foo = null;
if(!$foo){
echo "asd";
}
will ouptut "asd"
Its not the same
!= is No equal (Returns true if is not equal)
!== is Not identical (Returns true if is not equal , or they are not of the same type)
$a != $b
TRUE if $a is not equal to $b after type juggling.
$a !== $b
TRUE if $a is not equal to $b, or they are not of the same type.
See type juggling in PHP for more info on type juggling.
Sources : php.net

Wrong PHP string equality

For some reason, PHP decided that if:
$a = "3.14159265358979326666666666"
$b = "3.14159265358979323846264338"
$a == $b is true.
Why is that, and how can I fix that?
It ruins my code.
The Problem
PHP converts strings (if possible) to numbers (source). Floating points have a limited precision (source). So $a == $b because of rounding errors.
The Fix
Use === or !==.
Try it
<?php
$a = "3.14159265358979326666666666";
$b = "3.14159265358979323846264338";
if ($a == $b) {
echo "'$a' and '$b' are equal with ==.<br/>";
} else {
echo "'$a' and '$b' are NOT equal with ==.<br/>";
}
if ($a === $b) {
echo "'$a' and '$b' are equal with ===.<br/>";
} else {
echo "'$a' and '$b' are NOT equal with ===.<br/>";
}
?>
Results in
'3.14159265358979326666666666' and '3.14159265358979323846264338' are equal with ==.
'3.14159265358979326666666666' and '3.14159265358979323846264338' are NOT equal with ===.
Note
When you want to do high precision mathematics, you should take a look at BC Math.
You could use === in the equality test.
$a = "3.14159265358979326666666666";
$b = "3.14159265358979323846264338";
if($a===$b)
{
echo "ok";
}
else
{
echo "nope";
}
This code will echo nope.
Comparing with == is a loose comparison, and both strings will be converted to numbers, and not compared right away.
Using === will perform a string comparison, without type conversion, and will give you the wanted result.
You can find more explanations in the PHP manual:
PHP type comparison tables
Comparison Operators
Try using $a === $b instead; you should never use == for string comparison.
Read PHP: Comparison Operators
If you compare a number with a string or the comparison involves
numerical strings, then each string is converted to a number and the
comparison performed numerically. These rules also apply to the switch
statement. The type conversion does not take place when the comparison
is === or !== as this involves comparing the type as well as the
value.
Others have recommended BC Math, but if you are doing floating point comparisons, the traditional way of comparing numbers is to see if they are the same to a reasonable error level
$epsilon = 1.0e-10;
if (abs($a - $b) < $epsilon) then {
// they're the same for all practical purposes
}
You should not to compare a float variable like that.
Try this:
bccomp($a, $b, 26)

"AND &" operator work fine but "OR |" and Either-OR "^" always true

I am using logical operators to test variables but AND & operator work fine but OR | and Either-OR ^ always true.
Why?
$a = 6;
$b = 6;
if ($a OR $b == 3) {
echo 'true <br />';
}
else {
echo 'false <br />';
}
The issue is with your syntax.
You need to look at the expression separately.
if($a) OR if($b == 3)
is what you're doing.
What you want is:
if($a == 3 || $b == 3)
If you look at $a by itself, any value except for 0 will return true making the entire equation true thanks to the OR
Because you have to OR to Boolean results - you're reading it too much like English.
if ($a == 3 || $b == 3)
rather than
if ($a OR $b == 3)
It's a matter of precedence - see http://php.net/manual/en/language.operators.precedence.php for more details here.
Both the other answers give you the code you need.
$a = true
$b = true
if($a and $b) TRUE if both $a and $b are TRUE.
Reference: http://php.net/manual/en/language.operators.logical.php

What does !== comparison operator in PHP mean?

I saw
if($output !== false){
}
It's an exclamation mark with two equals signs.
It almost works like not equal. Does it has any extra significance?
They are the strict equality operators ( ===, !==) , the two operands must have the same type and value in order the result to be true.
For example:
var_dump(0 == "0"); // true
var_dump("1" == "01"); // true
var_dump("1" == true); // true
var_dump(0 === "0"); // false
var_dump("1" === "01"); // false
var_dump("1" === true); // false
More information:
PHP Comparison Operators
PHP’s === Operator enables you to compare or test variables for both equality and type.
So !== is (not ===)
!== checks the type of the variable as well as the value. So for example,
$a = 1;
$b = '1';
if ($a != $b) echo 'hello';
if ($a !== $b) echo 'world';
will output just 'world', as $a is an integer and $b is a string.
You should check out the manual page on PHP operators, it's got some good explanations.
See this question: How do the equality (==) and identity (===) comparison operators differ?.
'!==' is the strict version of not equal. I.e. it will also check type.
yes, it also checks that the two values are the same type. If $output is 0, then !== will return false, because they are not both numbers or booleans.

What does === do in PHP

I have been programming in PHP for a while but I still dont understand the difference between == and ===. I know that = is assignment. And == is equals to. So what is the purpose of ===?
It compares both value and type equality.
if("45" === 45) //false
if(45 === 45) //true
if(0 === false)//false
It has an analog: !== which compares type and value inequality
if("45" !== 45) //true
if(45 !== 45) //false
if(0 !== false)//true
It's especially useful for functions like strpos - which can return 0 validly.
strpos("hello world", "hello") //0 is the position of "hello"
//now you try and test if "hello" is in the string...
if(strpos("hello world", "hello"))
//evaluates to false, even though hello is in the string
if(strpos("hello world", "hello") !== false)
//correctly evaluates to true: 0 is not value- and type-equal to false
Here's a good wikipedia table listing other languages that have an analogy to triple-equals.
It is true that === compares both value and type, but there is one case which hasn't been mentioned yet and that is when you compare objects with == and ===.
Given the following code:
class TestClass {
public $value;
public function __construct($value) {
$this->value = $value;
}
}
$a = new TestClass("a");
$b = new TestClass("a");
var_dump($a == $b); // true
var_dump($a === $b); // false
In case of objects === compares reference, not type and value (as $a and $b are of both equal type and value).
The PHP manual has a couple of very nice tables ("Loose comparisons with ==" and "Strict comparisons with ===") that show what result == and === will give when comparing various variable types.
It will check if the datatype is the same as well as the value
if ("21" == 21) // true
if ("21" === 21) // false
=== compares value and type.
== doesn't compare types, === does.
0 == false
evaluates to true but
0 === false
does not
Minimally, === is faster than == because theres no automagic casting/coersion going on, but its so minimal its hardly worth mentioning. (of course, I just mentioned it...)
It's a true equality comparison.
"" == False for instance is true.
"" === False is false

Categories