php equations in statements with double = problems [duplicate] - php

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)

Related

PHP - why does php think that a random string is equal to true? [duplicate]

This question already has answers here:
The 3 different equals
(5 answers)
Closed last month.
Code:
$x = true;
if ($x == "continue") {
$x = "foo";
}
echo $x; // prints "foo"
I know I can use === instead to solve it, but I wonder why PHP thinks that a string is equal to true?
Because a non-empty string is considered true and an empty string is false.
Google for "truthiness" for more info. And yes, a lot of it is bonkers. Just wait until you hear about how JavaScript does it.

How does the if statement work with data returned from PHP methods? [duplicate]

This question already has answers here:
How exactly does if($variable) work? [duplicate]
(8 answers)
Closed 3 years ago.
Looking at this single if-statement, if a string is returned, then the condition of the if statement is false? and if null is returned the condition is true? That doesn't seem to make sense, though I know that, that is how this piece of code works. Does null always mean true? and does a stand alone string always get inturpruted as a false condition for an if statement?
BTW, this is just somthing ive been racking my head on, doesnt really matter, other than clarity sure would feel good.
if ($conn->connect_error){
die("Connection failed: " . $conn->connect_error);}
tl;dr: See the table here, particularly the "Boolean" (far right) column.
A string will only evaluate to false if it contains zero characters (an empty string) or the single character "0" (zero), in which way it behaves like the integer zero. Otherwise, it will always evaluate to true.
Null will always evaluate to false. If it seems to be evaluating as true, there must be something else happening there.
If you want to be sure, you can use the identity operator === and/or its negative compliment !== or your own casting to be sure.
if ($conn->connect_error !== null) {
// …
}

Difference between NULL, 0 ,False and ''? [duplicate]

This question already has answers here:
Null vs. False vs. 0 in PHP
(18 answers)
Closed 4 years ago.
What is the exact difference between null, 0, false, and an empty string in PHP? Is null similar to None in python?
Well, after massive comments and then poof deletion of them... may as well try to help you out.
null is the absence of value.
0 can be a numeric value, or a representation of a boolean FALSE, or a string. PHP doesn't really have variable typing, so depending on what you are checking for using ==0 or ===0 or ==false or ===false may be appropriate.
You may want to read over this - https://www.virendrachandak.com/techtalk/php-isset-vs-empty-vs-is_null/ as well as the PHP manual for isset() and empty()

Conditional in PHP [duplicate]

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.

PHP executing header:location regardless IF statement parameters [duplicate]

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

Categories