Is there any difference between following two IF conditions?
if ($insert_id)
{
..
}
if($insert_id != NULL)
{
..
}
Actually a function returns the mysql insert id, and i want to check if the function returned id or not.
NULL is loosely equal to FALSE, Zero, and an empty string.
Study this table to learn about comparisons.
Basically,
$value = NULL;
if($value == FALSE) // True
if($value == '') // True
if($value == 0) // True
while...
if($value === FALSE) // False
if($value === '') // False
if($value === 0) // False
No, there's no difference.
There would be if you used !== NULL. If it's just != NULL it will also evaluate against false, '' (empty string) or the integer value 0.
The same goes for the first statement, which is short for $insert_id == true which is basically the same as != false or != NULL.
For what you're trying to do there is no difference.
Basically what you're trying to do is see if the variable is set. Since any non-zero expression is essentially true, if there is a value it will evaluate as such.
The only time it would matter is if you were specifically checking for a NULL value.
if ($result_from_mysql === FALSE) { ... failure ... }.