What's the difference between if(!Variable) and if(isset($variable))?

if(!Variable)和if(isset($ variable))之间有什么区别?


Well, the answer is pretty simple. isset( $var ) returns whether or not a variable exists and is not null, where !$var tells you if that variable is true, or anything that evaluates to true (such as a string). Also, using !$var will output a notice that you're using an undefined variable, whereas isset won't do that.

Mind you: they are two different things:

<?php
var_dump( isset( $foo ) ); // false.
var_dump( !$foo ); // true, but with a warning.

$foo = false;
var_dump( isset( $foo ) ); // true
var_dump( !$foo ); // true.

如果没有设置变量,那么在使用if(!Variable)时会发出警告。


They are two different statements

  • in the first you check if a variable is false hence the '!'
  • here you check if a variable is actually set to some value other then null.
  • 链接地址: http://www.djcxy.com/p/58884.html

    上一篇: 如果钥匙没有设置,GET ['钥匙']返回?

    下一篇: if(!Variable)和if(isset($ variable))之间有什么区别?