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

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!';

Related

php why does 0 == 'somestr' evaluate to true [duplicate]

This question already has answers here:
How do the PHP equality (== double equals) and identity (=== triple equals) comparison operators differ?
(13 answers)
Why does PHP consider 0 to be equal to a string?
(9 answers)
Closed 4 years ago.
I have this code:
echo ( $arr['process_refunds'] == 'storecredit' ) ? 'true' : 'false'
The value in $arr['process_refunds'] is 0 type int(0).
It tried in php -a and it turns out that if we compare any string with
== with int(0) it evaluates to true.
Why is that?
This is because == is a loosely-typed comparison operator. It is converting your string to a number, therefore it evaluates to true. Try using ===.
Your string is getting converted to a zero because it starts with a letter.
I think it's pretty obvious that the loose comparison is not returning expected values. You can see the comparison table here (2nd table)
http://php.net/manual/en/types.comparisons.php
you can see that this is expected behaviour. This is because php converts the string "php" or "somestr"to a match the number type, making it equal to 0, before making the assessment.
Unless there are other types/conditions you're looking to match with a loose comparison, to get around this, you should use === that will assure you have the matching type.
Firstly:
php -r "echo ( $arr['process_refunds'] == 'storecredit' ) ? 'true' : 'false';"
prints:
false
It is obvious because $arr is undefined. But if it has value 0 then
php -r "echo ( 0 == 'storecredit' ) ? 'true' : 'false';"
prints:
true
because both 0 and 'storecredit' are converted to integers.
Value of (int) 'storecredit' is 0 because of it does not contain any number on start of string. For example string '4ss' would be converted to 4.
You need no use === because in that way you compare both values are the same type and the same content; example
echo 0 === 'somstr'; // returns false
echo 0 == 'somstr'; //return true

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

why in PHP (-1 > false) returns true? [duplicate]

This question already has answers here:
Type-juggling and (strict) greater/lesser-than comparisons in PHP
(3 answers)
Closed 8 years ago.
as I know false is a 0
if(-1 > false)
print "Here";
in this code if returns a true and here is printed
WHY ?
Please see PHP Comparision Operators - table Comparison with Various Types
Type of Operand 1 Type of Operand 2 Result
bool or null anything Convert both sides to bool, FALSE < TRUE
So if you compare bool to anything else, then the second operand is casted to boolean true.
Also we have information here, that FALSE < TRUE, what exactly happens in your example.
In this case it's -1 that is converted into boolean (true, as only 0 is treated as false). So the final comparison is
if (true > false) {
...
}
Type Juggling can be very unintuitive, so always try to avoid situations where you compare variables of two different types. In case of equality comparison always try to use identity operator (===), in case of inequality all you can do is to add a manual cast.
See also: http://us3.php.net/manual/en/types.comparisons.php
< is a numerical comparison operator, the code does loose comparison converting -1 to true and hence the result.
WARNING: -1 is considered TRUE, like any other non-zero
(whether negative or positive) number!
Check with this two code. You can get the difference
if(-1 > false)
print "Here"; //This will print the `Here`
if(-1 > 0)
print "Here"; // Not print `Here`

== in php causing false positives when variable is zero [duplicate]

This question already has answers here:
How do the PHP equality (== double equals) and identity (=== triple equals) comparison operators differ?
(13 answers)
php == vs === operator [duplicate]
Why does 1234 == '1234 test' evaluate to true? [duplicate]
(6 answers)
Closed 9 years ago.
I have the strangest php behavior I've never noticed before:
$array =array(0,1,2, 'parent');
foreach ($array as $value)
{
if ($value=='parent')
{
echo $value;
echo '<br>';
continue;
}
}
exit;
Will return
0
parent
I was wondering if anyone could explain to me why it matches the 0 to the string 'parent'?
what Mark said but to expound, 0 is a number so the == operator casts the 'parent' string to a number, which gives 0. The == operator does not care about type. So they match.
The "==" operator in php does not compare the type of the objects, it converts the objects to another type. So in your case you are comparing a numerical object with a string object. So it changes the type of your string 'parent'. Since that string does not contain any numerical value it probably changes it to 0.
See
http://il.php.net/manual/en/language.operators.comparison.php and
http://il.php.net/manual/en/language.types.string.php#language.types.string.conversion
for more explanations

PHP empty var == 0?

In php 5
$my_var = "";
if ($my_var == 0) {
echo "my_var equals 0";
}
Why it evaluates true? Is there some reference in php.net about it?
PHP is a weakly typed language. Empty strings and boolean false will evaluate to 0 when tested with the equal operator ==. On the other hand, you can force it to check the type by using the identical operator === as such:
$my_var = "";
if ($my_var === 0) {
echo "my_var equals 0";
} else {
echo "my_var does not equal 0";
}
This should give you a ton of information on the subject: How do the PHP equality (== double equals) and identity (=== triple equals) comparison operators differ?
A string and an integer are not directly comparable with ==. So PHP performs type juggling to see if there is another sensible comparison available.
When a string is compared with an integer, the string first gets converted to an integer. You can find the details of the conversion here. Basically, since "" is not a valid number, the result of the conversion is 0. Thus, the comparison becomes 0 == 0 which is clearly true.
You'll probably want to use the identity comparison === for most (if not all) your comparisons.
This is due to the type coercion that comes from the equality operator you are using.
The PHP manual has the Type Comparison tables to shed a light on this.
Its generally considered a good practice to utilize the identical operator === instead, as to avoid such corner(?) cases.
In your first line, you define $my_var as string.
Inside the comparison you compare that variable with a constant integer.
If you want exact comparison (I don't know why you need to compare a string with an integer without any cast), you should use the ===:
if ($my_var === 0) {
echo "my_var equals 0";
}
That will never echo the message.
The PHP manual defines in Comparison Operators section, the operator == as:
TRUE if $a is equal to $b after type juggling.
So, the important thing here is type juggling.
As a matter of fact, in PHP Manual: types comparisons, the second table tell you exactly that integer 0 equals string "".
here is the reference in the php manual about boolean values
http://www.php.net/manual/en/language.types.boolean.php
and here is the reference for the NULL value
http://www.php.net/manual/en/language.types.null.php
$my_var = '';
if ($my_var == 0) {
echo "my_var equals 0"
}
evaluates to true because "" is the same as NULL which evaluates to false or 0

Categories