PHP Bug with Conditional Operator? - php

$i=0;
$j='00:15';
if ($i==$j)
echo "equal";
else
echo "not equal";
displays equal?

00:15 is cast into an integer first and then compared with $i. Since intval() (which does the casting) stops at the first non-numeric character, 00:15 is cast into zero, which is equal to zero. No bug here, this is how the == operator works.
If you want to avoid casting, use === instead.

Using the == the types will be converted before they are compared. In this case, both will be converted to an integer, and when you convert a non-numeric string to an integer, it will probably result in 0.
If you use the === operator instead, you will get the result that you expect.

string and int compares as int's, So $j converts to 0 here because starts with 00

Not bizarre at all - when comparing string with integer, the first one is being cast to integer then compared. Use === for type checking and you'll see the real result

Turns out the === is problematic
for example 10===10 will be false if one is integer and one is string even though they are identical as can happen from forms posting.
the solution i used was:
if ("$i"=="$j")
this way it just checks if they're identical regardless of type

Related

PHP : confused with "==" operator

Can somebody explain to me why this codes returns "TRUE".
I know that i should use the "===" rather "==" but I run to this code and wondering why it returns to true. Thanks in advance.
<?php
$s = "final";
$i = 0;
if($s == $i){
echo "TRUE";
}else{
echo "FALSE";
}
When you are trying to compare string and number, interpretator converts your string to int, so you got 0 == 0 at final. Thats why string == 0 is true.
Take a look at the PHP comparison tables.
You can see in the "Loose comparisons with ==" table that comparing the number 0 with a string containing text ("php" in the example) evaluates to TRUE.
This is just a property of the loose comparisons implemented in PHP. I wouldn't search for any more logic behind this than that this is a given.
As mentionned above, it is an issue with php's loose comparison. The accepted answer on php string comparasion to 0 integer returns true? Explains it well enough IMHO. in short "==" attempts to cast your string into an int, and since it fails, the resulting int has a value of 0
From 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.
And from PHP string conversion to numbers:
The value is given by the initial portion of the string. If the string starts with valid numeric data, this will be the value used. Otherwise, the value will be 0 (zero).
So when you compare integer and a string, PHP tries to convert string to integer first and as "final" doesn't contain any valid numeric data, it is converted to 0.
You can try:
var_dump( intval('12final') ); //int(12)
var_dump( floatval('1.2final') ); //float(1.2)
This is because of both 12final and 1.2final start with valid numeric data (12 and 1.2 respecrively), their converted value is not 0.

Why are strings "0e368798" and "00000000" equal, per PHP's equality operator?

Can someone explain why the following two statements return true?
if ('0e368798' == '00000000')
or
if ((string)'0e368798' == (string)'00000000')
Why do I have to use the strict operator to check the equality of these two strings?
Because XeY is X * 10^(Y), and 0 times anything is 0. 0000000 is also 0. And == in PHP very intuitively thinks that if it can be converted into a number, it should be.
EDIT: It was in a helpful comment that is now deleted, so with apologies to the commenter whose name I did not catch, I will repeat it here - from PHP docs on comparison:
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.

php string comparison unexpected type juggling

I encountered a strange and unexpected behavior in PHP while comparing some string values. The first two statements below return true when I would expect them to return false. The last statement returns false as expected. I'm aware of PHP's Type Juggling, but what I understand from the docs is that type juggling happens when you are comparing two different data types like a string and an integer. In the examples below though both literals are strings. Does this mean that when you are doing string comparison in PHP it inspects both strings to see if they look like integers and if so type casts the both of them to integers and then compares those integer values. So my question is under what conditions does this behavior happen, how exactly does string comparison work in PHP?
var_dump("10" == "10.0000");
var_dump("10" == "+10.");
var_dump("10" == "10 ");
#output
bool(true)
bool(true)
bool(false)
Updates
So baba's answer below comparison involves numerical strings really helped in getting me to understand what's going on. The function is_numeric will return to you whether or not a string is considered to be a numeric string. interestingly "10 " is not considered a numeric string but " 10" is. I dug around the PHP source code and I believe the implementation of is_numeric is in the is_numeric_string_ex function. From that one can tell exactly when PHP will treat a string as a numeric string.
You are getting error because of the position of the space this would return true
var_dump("10" == " 10"); // true
So if you RUN
var_dump("10" == "10 "); //false
What you are actually Running is because it would be treated as a string
var_dump("10" == 0); //false
This is because Type juggling would convert "10 " to 0 this is in the PHP Documentation
FROM PHP DOC
TRUE if $a is equal to $b after type juggling.
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.
If you want to trick type juggling
var_dump("10" == 0 + "10 "); // true
This is Because
An example of PHP's automatic type conversion is the addition operator '+'. If either operand is a float, then both operands are evaluated as floats, and the result will be a float. Otherwise, the operands will be interpreted as integers, and the result will also be an integer. Note that this does not change the types of the operands themselves; the only change is in how the operands are evaluated and what the type of the expression itself is.
I suppose the definite answer lies buried somewhere in the vastness of
compare_function
in php-src/Zend/zend_operators.c and the macros used in there.
In this case, all the 'strings' except the last one, are treated like floats or integers, and then compared. Thats why line 1 and 2 give a true.
In line 3 there is a space in the string, and that means for php that is is a 'real' string, and that line gets strings compared.
I do not like loose typed languages either, but for php this is the way it works.

Weird behaviour with if statement

The following appears to work, yet I really don't think it should:
if ("'True','False'" == 0)
{
echo 'Hello, World.';
}
Is it grepping the last False out of the string, and if so why, and how do you stop it?
This is due to loose typing. When you compare a string to a number, PHP has to cast one to the other's type. In this case, the string, when casted to an integer, is worth 0, your condition being True.
Any string evaluates to 0 when being compared against a number (unless it's a stringed number such as "5" or "4string"). That's why it'll always evaluate to 0.
See the Type Comparison Table
What's really happening is that PHP is trying to do an implicit type conversion, and for one reason or another it's deciding that string when converted to an integer looks like 0. You can prove this to yourself by doing:
echo ((int) "'True','False'");
If you want to do a type-checked comparison you should use the triple equals (===):
if("'True','False'" === 0)
...which will most certainly evaluate to false.
No; it's converting the string "'True','False'" to a number — and that number is 0. 0 == 0 is true. You can solve it by using !"'True','False'" if I understand your intent correctly, or by using strict equality, ===, which you should generally use.

why does this evaluate to true

Whey does this evaluate to true?
<?php
$val2=0;
//outputs that is an error123
if($val2=='error123'){
echo 'that is an error123<br />';
}else{
echo 'that is not an error123<br />';
}
You're comparing a string to an integer. To make the comparison the string is first converted to an integer. When 'error123' is converted to an integer it becomes 0.
echo intval("error123");
Result:
0
In the PHP manual there is an explanation for this behaviour.
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.
There is a quick reference page PHP type comparison tables that shows you the result of various comparions. See the table "Loose comparisons with ==". The interesting part with regard to this question is that 0 == "php" is shown as TRUE.
There is also a page on type juggling. A user comment on that page gives nearly the exact same example as this.
If you don't want the type juggling use === instead of ==.
Give this a try: $val2==='error123'
That will evaluate the value and the type of the variable. More here:
http://us.php.net/manual/en/language.operators.comparison.php

Categories