1

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.

3
  • mysql function return boolean FALSE on error, and other thigns on success. They should never be returning null. Commented Jul 8, 2011 at 21:14
  • @Marc B, so if theres some mysql error and it doesnt return the insert_id, will the first condition true even then? Which method i should use to make sure that condition is true only if an integer (insert_id) is returned. In all other cases it should be false. Commented Jul 8, 2011 at 21:19
  • No, it'd still be false, because the failure turned a false. you can explicitly test with if ($result_from_mysql === FALSE) { ... failure ... }. Commented Jul 8, 2011 at 22:12

6 Answers 6

1

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
Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

0

If the $insert_id belongs to a resultset returned by a mysql_query, then use the first one:

if ($insert_id)
{
    // do stuff
}

Comments

0
if (!empty($insert_id))
{

}

if($insert_id > 0)
{

}

Comments

0

In the php language,you not need return an expression booleana(what is correct). any something different of: 0,false,null and ""(empty string) is parsed as true

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.