This question already has answers here:
How do the PHP equality (== double equals) and identity (=== triple equals) comparison operators differ?
(13 answers)
Closed 9 years ago.
In my case should I use != as below, or is !== more appropriate, what is the difference.
private function authenticateApi($ip,$sentKey) {
$mediaServerIp = '62.80.198.226';
$mediaServerKey = '45d6ft7y8u8rf';
if ($ip != $mediaServerIp ) {
return false
}
elseif ($sentKey != $mediaServerKey ) {
return false
}
else {
return true;
}
}
public function setVideoDeletedAction(Request $request)
{
//Authenticate sender
if ( $this->authenticateApi($request->server->get("REMOTE_ADDR"),$request->headers->get('keyFile')) != true ) {
new response("Din IP [$ip] eller nyckel [********] är inte godkänd för denna åtgärd.");
}
!= checks value
if($a != 'true')
!== checks value and type both
if($a !== 'true')
http://www.php.net/manual/en/language.operators.comparison.php
As the manual says, one compares type as well.
!== is more strict. '1'==1 returns true, but '1'===1 - false, because of different types.
=== and !== are used to compare objects that might be of different type. === will return TRUE iff the types AND values are equal; !== will return TRUE if the types OR values differ. It is sometimes known as the 'congruency' operator.
If you are comparing two things that you know will always be the same type, use !=.
Here's an example of using !==. Suppose you have a function that will return either an integer or the value FALSE in the event of a failure. 0 == FALSE, but 0 !== FALSE because they are different types. So you can test for FALSE and know an error happened, but test for 0 and know you got a zero but no error.
Related
This question already has answers here:
Is there a difference between !== and != in PHP?
(7 answers)
difference between != and !== [duplicate]
(4 answers)
The 3 different equals
(5 answers)
Closed 3 years ago.
I have two statements like this:
$ready = true;
if($ready === true) {
echo "it's ready!";
}
And this:
$ready = true;
if($ready !== false) {
echo "it's ready!";
}
I know those mean the same thing as === true and !== false is the same thing. I see people use the negation type (is not false or is not {something}). like this: https://stackoverflow.com/a/4366748/4357238
But what are the pros and cons of using one or the other? Are there any performance benefits? Any coding benefits? I would think it is confusing to use the negation type rather than the straight forward === true. Any input would be appreciated! thank you
These do not mean the same thing, and you can see this demonstrated below:
$ready = undefined;
if($ready === true) {
console.log("it's ready 1");
}
if($ready !== false) {
console.log("it's ready 2");
}
When in doubt, stick with ===. You're comparing to exactly what it should be. "This should exactly be equal to true" vs "This should be equal to anything but exactly false" are 2 separate statements, the second including a lot of values that would be true (As demonstrated above, undefined is not exactly false, so this would be true).
Most PHP functions return false as failure, such as strpos, from the manual it says:
Returns the position of where the needle exists relative to the
beginning of the haystack string (independent of offset). Also note
that string positions start at 0, and not 1.
Returns FALSE if the needle was not found.
So it returns an integer or false, then it is wrong to use strpos() === true
strpos() !== false is the right way to check the result.
It is not about performance and they are not the same. It helps us in reducing unnecessary if else statements.
Consider this: there is a variable foobarbaz whose value can be anything among foo/ bar/ baz/ foobar/ barbaz. You need to execute a statement if the value is not foo. So instead of writing like this:
if(foobarbaz === "bar" || foobarbaz === "baz" || foobarbaz === "foobar" || foobarbaz === "barbaz") {
//some statement
}
else if(foobarbaz === "foo") {
//do nothing
}
You can write something like this:
if(foobarbaz !== "foo") {
//some statement
}
You can see we were able to eliminate the unnecessary else statement.
In php == compare only the values are equal. For and example
$ready = 'true';
if($ready == true) {
echo "true";
}
else{
echo "false";
}
It print true.But if it is compare with === it not only check the values but also it check the datatype. So it compares with === it print the false.
Talking about !== it's work as the same. So it's not a problem to output if you use
if($ready === true)
or
if($ready !== false)
Can't compare these two because both are same.
This question already has answers here:
How do the PHP equality (== double equals) and identity (=== triple equals) comparison operators differ?
(13 answers)
Closed 5 years ago.
I'm new to PHP and unfamiliar with the !== and === operators. Are Logic #1 and #2 below equivalent? And if not, why so? Basically, the code reads values from a csv row in a file, and if the value is empty it disregards empty or null values
//Logic #1
$value = readValue();
//skip empty values
if ( !$value || $value === '') {
print "empty value found";
}else{
doSomething();
}
//Logic #2
$value = readValue();
if ( $value && $value !== '' ) {
doSomething();
}else{ //skip empty values
print "empty value found";
}
To answer your question about the == and === operators, those should be identical.
== is the opposite of !=
=== is the opposite of !==
See this previous answer for more information on the difference between == and ===.
To improve your code a bit, I would suggest using the empty() function which will check for nulls and empty strings.
Something like this:
if (empty($value)) echo "nothing to see here";
else doSomething();
I have a variable which always contains one of these two cases:
false
A number (it can be also 0)
My variable in reality is this:
$index = array_search(1, array_column($var, 'box'));
//=> the output could be something like these: false, 0, 1, 2, ...
Now I want to detect $index is false or anything else. How can I implement such a condition?
It should be noted none of these doesn't work:
if ( empty($index) ) { }
if ( !isset($index) ) { }
if ( is_null($index) ) { }
And I want this condition:
if ( ? ) { // $index is false } else { // $index is a number (even 0) }
You can use the PHP identical operator ===:
if (false === 0)
This will return false if they are of the same type. Unlike == where type juggling occurs, false is not identical to 0.
if ($index === false) {
// $index is false
} else {
// $index is a number (even 0)
}
More information on the difference between the identical operator === and the comparison operator ==: How do the PHP equality (== double equals) and identity (=== triple equals) comparison operators differ?
Use the === operator...
if(false == 0)
That will cast 0 to false and you get false == false
if(false === 0)
That does not cast and you get false compared to zero, which is not the same.
For not equal to, use !== instead of ===
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How do the equality (== double equals) and identity (=== triple equals) comparison operators differ?
if (filter_input(INPUT_GET, "email", FILTER_VALIDATE_EMAIL) === 0)
or
if (filter_input(INPUT_GET, "email", FILTER_VALIDATE_EMAIL) == 0)
Should I use == or === here?
Where can I use ===?
=== is used to make a strict comparison, that is, compare if the values are equal and of the same type.
Look at #Ergec's answer for an example.
In your case you should do just:
if (filter_input(INPUT_GET, "email", FILTER_VALIDATE_EMAIL) == false)
or simply
if (filter_input(INPUT_GET, "email", FILTER_VALIDATE_EMAIL))
because filter_input() returns:
Value of the requested variable on success, FALSE if the filter fails,
or NULL if the variable_name variable is not set. If the flag
FILTER_NULL_ON_FAILURE is used, it returns FALSE if the variable is
not set and NULL if the filter fails.
In PHP double equals matches only VALUE
if (2 == "2") this returns TRUE
Triple equals matches VALUE AND TYPE
if (2 === "2") this returns FALSE
You are looking for !== FALSE in that case.
filter_input will either return the filtered variable or FALSE.
=== checks if both sides are equal and of the same type.
!== checks if both sides are not equal and of the same type.
This question already exists:
Closed 13 years ago.
Possible Duplicate:
php == vs === operator
What's the difference between !== and != in PHP?
!== is strict not equal and which does not do type conversion
!= is not equal which does type conversion before checking
=== AND !== checks if the values compared have the same type (eg: int, string, etc.) and have the same values
While...
== AND != only compares the values
"1" != 1 // False
"1" !== 1 // True
It's a type thing. !== takes into account the types of its operands, while != does not (the implicit conversion makes the first conditional false).
== is only true if the values are equal.
=== is only true if the values and types are equal.
the triple equal also make sure the two variable are from the same type
1 == `1` // is ok
1 === `1` // is not same.
Both are comparion operators
$a !== $b Return TRUE if $a is not equal to $b, or they are not of the same type.
$a != $b Return TRUE if $a is not equal to $b.