This question already has answers here:
Why does PHP consider 0 to be equal to a string?
(9 answers)
Closed 8 years ago.
When I run the follow code in PHP
if('yes' == 0)
echo 'a';
else
echo 'b';
The output is a.
I don't understand what happen?
And can I convert the php code to C source code to have a look what real happening?
PHP is a dynamically typed language, and == is a loose comparison operator, meaning it will first cast values it compares to one type, int for that matter, and then compare them; strings are being cast to integers by taking numericals from the left part, so 1abc casts to 1. By that logic yes cast to 0, and 0 == 0 yields true.
Related
This question already has answers here:
Why does 1...1 evaluate to 10.1? [duplicate]
(4 answers)
Closed 3 years ago.
Just a second ago I was playing around with PHP, trying to figure out if there was a native range function (eventually finding range). However, one of the things I tried was the following:
echo 1...2;
which to my surprise returns the string "10.2". Can anyone tell me exactly what syntax is responsible for this? It doesn't seem like a valid place for a splat operator.
The statement consists of three parts: 1., . and .2. The first one evaluates to the number 1, the second one is the string concatenation operator, and the latter one evaluates to 0.2. Thus, you get 10.2.
Equivalent example code:
$a = 1.;
$b = .2;
echo "a = $a\n";
echo "b = $b\n";
echo "a.b = ".($a.$b)."\n";
outputs
a = 1
b = 0.2
a.b = 10.2
This question already has answers here:
Why does PHP consider 0 to be equal to a string?
(9 answers)
Closed 6 years ago.
I've got some weird actions from my php script and finally figured out that this following statement makes problems. Because 0 is equal some string in php.
if(0 == "whats Up?")
{
echo 42;
}
With triple "=" it do what I expected. It is possible for you to give me a briefly answer what is the reason and idea behind this behavior of php? Why did they implement php like this?
I mean I know that 1 == "1" is true and 1 === "1" is not. This is also in python. I also learn from somewhere that 0 could be understandable as false but this example above has no explication for me. But I am sure that you know it.
Thank you in advance
That's because of Type Juggling. The second operand gets converted to an integer and 0 == 0 is true.
var_dump((int) "whats Up?"); // int(0)
This question already has answers here:
How does true/false work in PHP?
(6 answers)
Closed 9 years ago.
Is it same in PHP:
if($x!=5)
{
//code
}
VS
$x=5;
if(!$x)
{
//code
}
What about if($x)? Expression in IF statement evaluates to either TRUE or FALSE unlike C where it is either 0 or anything other than 0 (say 1 or more). We can test the expression by using var_dump(!$x) in PHP. So,what about if($x)?
They are not the same.
The first block of code tests whether or not the variable x does not equal 5.
The 2nd block of code tests whether x is not true. Since you declared a value for $x, the statement will be evaluated as false and the content inside the brackets will not execute.
No,it is not same in PHP:
Logical Operator.
! $x Not TRUE if $x is not
TRUE.
Comparison Operators
$x!=5 Not equal TRUE if $x is not equal to 5
Source: PHP Documentation.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
The 3 different equals
Can anyone tell me why, when using the code below, I am getting redirected to elephant.com rather than seeing a 'giraffe!
<?php
$foo="giraffe";
if($foo="elephant"){
header("location:http://www.elephant.com");
exit();
}else{
echo $foo;}
?>
Thanks for looking
J
if($foo="elephant")
You're assigning $foo here, rather than comparing it; you should be doing:
if($foo=="elephant")
The result of an assignment operation is the value that's just been assigned; in this case, 'elephant' is evaluating to true.
Your if() statement has a single equal sign. This doesn't do a comparison in PHP; it sets the value and returns true.
In order to do a comparison, you need to use either a double-equal or a triple-equal sign:
if($foo == "elephant") { .... }
or
if($foo === "elephant") { .... }
The difference between the two is that double-equal doesn't care about the variable's data type, whereas triple-equal does. In this case, there's not much difference between them, but it's worth learning and understanding the differences because they can bite you if you don't know them. More info here: http://php.net/manual/en/language.operators.comparison.php
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Why does PHP consider 0 to be equal to a string?
php string comparasion to 0 integer returns true?
it seems that as one has in PHP an if-statement where a function some_function() returns zero
<?php
if( some_function() == "whatever_you_want" ) { ... }
the statement will always be executed since
<?php
echo some_function() == "whatever_you_want";
is then TRUE.
Why behaves PHP in such a counter intuitive way?
This is a defined behavior of PHP when you compare a number value and a string value:
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.
Use strict value comparison with === or !== and you’re getting the expected result.