开发者

(isset($someValue) && ($someValue != '')) vs. !empty($someValue) - are they the same?

Are the below two s开发者_如何学JAVAtatements similar?

if (isset($someValue) && $someValue != '') 

and

if(!empty($someValue]))


No, they are not the same. Yes, they are similar. The first one checks if the 'someValue' is set and not equal to null plus whether it is not empty string. The second one is true if $_GET['act'] is not set or is on the list of variables treated as empty and listed on documentation page.

The following example from the same documentation page should fill any gaps:

$var = 0;

// Evaluates to true because $var is empty
if (empty($var)) {
    echo '$var is either 0, empty, or not set at all';
}

// Evaluates as true because $var is set
if (isset($var)) {
    echo '$var is set even though it is empty';
}


Kind of similar except 0 value.

According to http://us3.php.net/empty

$var = "0";
(!empty($var)) === false;
(isset($var) && $var!='') === true;


As you write it, they're actually almost the opposite, but I'll just assume you mean isset($_GET[someValue]) and !empty.

In that case no, they're not the same. In the first line you are checking for an empty string, but for example a zero value would pass. However, a zero value counts as empty.


Similar but not the same. Empty will return true if $_GET['act'] is equal to any of the following:

  • "" (an empty string)
  • 0 (0 as an integer)
  • 0.0 (0 as a float)
  • "0" (0 as a string)
  • NULL
  • FALSE
  • array() (an empty array)
  • var $var; (a variable declared, but without a value in a class)

Where as the first one only checks to see if the variable has been created and isn't equal to an empty string.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜