Which one is true while making kontrol with php [duplicate] - php

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.

Related

PHP why is 0=='all' true? [duplicate]

This question already has answers here:
How do the PHP equality (== double equals) and identity (=== triple equals) comparison operators differ?
(13 answers)
Closed 4 years ago.
I am reading the PHP documentation for boolean.
One of the comments says 0=='all' is true.
http://php.net/manual/en/language.types.boolean.php#86809
I want to know how it becomes true.
The documentation says all non-empty strings are true except '0'.
So 'all' is true
and
0 is false.
false == true should be false.
But:
if(0=='all'){
echo 'hello';
}else{
echo 'how are you ';
}
prints 'hello'.
In PHP, operators == and != do not compare the type. Therefore PHP automatically converts 'all' to an integer which is 0.
echo intval('all');
You can use === operator to check type:
if(0 === 'all'){
echo 'hello';
}else{
echo 'how are you ';
}
See the Loose comparisons table.
As you have as left operand an integer, php tries to cast the second one to integer. So as integer representation of a string is zero, then you have a true back.
If you switch operators you obtain the same result.
As Bhumi says, if you need this kind of comparison, use ===.
If you put a string as condition in a IF steatment it is checked to be not empty or '0', but if you compare it with an integer (==, <, >, ...) it is converted to 0 int value.
if('all')
echo 'this happens!';
if('all'>0 || 'all'<0)
echo 'this never happens!';

Why (false==0) is TRUE? [duplicate]

This question already has answers here:
How do the PHP equality (== double equals) and identity (=== triple equals) comparison operators differ?
(13 answers)
Closed 6 years ago.
I just var_dump(false==0) it outputs bool(true) Why false== 0 is true.I know true==1 is true Because if i echo true; It will output 1 so numeric value of true is 1, But numeric value of false is not 0, because when i echo false; It display nothing(empty), So how can false has same value as 0 AS we know == operator compares the values , if they same it will return true, and if their values is not same it will return false , so in the case of false==0 It should be false. Any idea ?
A boolean TRUE value is converted to the string "1". Boolean FALSE is
converted to "" (the empty string). This allows conversion back and
forth between boolean and string values.
So both false == "" and false == 0 are true. Remember, "0" is not the same as 0.
You can have a check here.
PHP Type Comparison
In short, == is the loose comparison operator which invokes type conversion before comparison. Maybe you should use the strict comparison operator === instead.
The same story goes in JavaScript.
false has the the same value as 0, it is just another way of writing it
So, false == 0 will be the same thing as saying
0 == 0
which returns true because 0 = 0

difference between != and !== [duplicate]

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.

Is there a difference between !== and != in PHP?

Is there a difference between !== and != in PHP?
The != operator compares value, while the !== operator compares type as well.
That means this:
var_dump(5!="5"); // bool(false)
var_dump(5!=="5"); // bool(true), because "5" and 5 are of different types
!= is the inverse of the == operator, which checks equality across types
!== is the inverse of the === operator, which checks equality only for things of the same type.
!= is for "not equal", while !== is for "not identical". For example:
'1' != 1 # evaluates to false, because '1' equals 1
'1' !== 1 # evaluates to true, because '1' is of a different type than 1
!== checks type as well as value, != only checks value
$num = 5
if ($num == "5") // true, since both contain 5
if ($num === "5") // false, since "5" is not the same type as 5, (string vs int)
=== is called the Identity Operator. And is discussed in length in other question's responses.
Others' responses here are also correct.
Operator != returns true, if its two operands have different values.
Operator !== returns true, if its two operands have different values or they are of different types.
cheers
See the PHP type comparison tables on what values are equal (==) and what identical (===).

What's the difference between !== and != in PHP? [duplicate]

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.

Categories