PHP 0 vs False ambiguity [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 6 years ago.
I've been trying to make a system where certain flags in a MySQL database are checked with php. To see if a certain letter is in the string, I use the function strpos(). If the letter I'm checking for happens to be the first one in the string the function returns 0. However this result seems to be synonymous with the Boolean value for false. I tested this theory with the below code:
if(0 == false){
echo 'true';
}
low and behold this evaluates to true and echoes the output inside the block. So, is there a logical way to distinguish between a character being at index 0 or not being part of the string at all? One option that occurred to me was to basically prepend the flags string with some character like an underscore. This way no meaningful characters would ever be at index 0 of the function's return value. That seems like a very dirty patchy way to fix things though. If you have any other ideas I would greatly appreciate your help.
Thanks for reading.

Use ===:
if(0 === false){
// will never be true
echo 'true';
}

Related

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) {
// …
}

php equations in statements with double = problems [duplicate]

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)

Three equals signs in php comparison [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.
I was working on some code made by someone else and came across in if statement that looks like this
IF($res === true){
do_something();}
This code is called many times and seems to be working. What is it doing?
I know one equals is assignment and two is comparison. What does the three equals sign operator do?
The easiest way to see it, is that == checks equality and === checks identicality. Equality will check the value, but identicality will check the variable type too.
Examples:
var_dump('true' == true); // bool(true)
var_dump('true' === true); // bool(false)
This is the "identical" operator. They'll need to be exactly the same to pass the condition.
The 3 different equals
Three === means that result has to be identical. Worded it wrong first time :)

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

IF Statement always executed in PHP [duplicate]

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.

Categories