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.
Related
This question already has answers here:
Does PHP have short-circuit evaluation?
(8 answers)
Closed 9 months ago.
I have always thought that if I want to check if a variable exists and has a certain value I have to use two if conditions:
if(isset($x)){
if($x->age==5){}
}
But I realized its also possible to do it in one line this way:
if(isset($x) && ($x->age==5)){}
Can someone tell me why the second variation will not result in an error if $x is null. Given that $x is null and doesn't have the property age? Would it be trying to access a property that doesn't exist?
$x=null;
Because $x is null, isset($x) is false. Then, because of the logical operator "AND" (&&), the condition cannot be fully validated, so, the test is stopped here and ($x->age==5) is not executed.
For a shorter code, as of PHP 8.0.1, you can use the NullSafe Operator (?->)
if ($x?->age == 5) { }
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.
This question already has answers here:
Does PHP have short-circuit evaluation?
(8 answers)
Closed 9 years ago.
Basically
Is this:
<?PHP if (false && crazyFunction()) : ?>
The same as:
<?PHP if (false) : ?>
<?PHP if (crazyFunction()) : ?>
If FALSE is evaluated in the first example will it still continue to evaluate "crazyFunction"?
The && operator is a shotcircuit operator, which means that it will stop as soon as it knows the outcome is going to be false.
This means that if the left part evaluates to false it stops and returns false. crazyFunction() will never be called in this example.
As soon as the value of boolean expression is known, it is no more executed.
Please note this (deprecated) example in old-fashioned mysql connections:
$db=mysql_connect_db('...') or die('Database error');
If after first part mysql_connect returned something that is not FALSE, 0, NULL etc., it does not execute this die().
(Regardless if this is or not correct to use mysql_* functions)
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.