This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to check if mysql returns null/empty
I have SQL command (LEFT JOIN between 2 tables) with result 'NULL' or '0'. And now I want to create PHP condition like this :
if ($rowSearch['Approved'] == NULL)
but it failed... then I tried this one
if ($rowSearch['Approved'] == 'NULL')
also failed... PHP always skip that statement. how to write PHP IF statement for this? thanks before.
If you need to distinguish between NULL, "", False, and 0, then you need to use a strict comparison ===. Or you can use is_null(). The table on this page may be relevant.
if ( empty($rowSearch['Approved']) && $rowSearch['Approved'] !== 0 )
You need to compare with ===
if ($rowSearch['Approved'] === null)
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:
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()
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:
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 11 years ago.
Possible Duplicate:
Javascript === vs == : Does it matter which “equal” operator I use?
Not really relative to any type of code, just in general?
usually === also represents equality of type.
so:
1 == "1" //true
1 === "1" //false
=== is the identical operator; it returns true when both the value and the type of the two operands are the same. == is the equal operator; it does not check types, just values.
Read more here.
=== compares the value as well as type of variable
== doesn't compare type