开发者

PHP: IF condition

Is there any difference between following two IF conditions?

if ($insert_id)
{
..
}



if($insert_id != NULL)
{
..
}

Actua开发者_StackOverflow中文版lly 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.


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

if ($insert_id)
{
    // do stuff
}


if (!empty($insert_id))
{

}

if($insert_id > 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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜