简体   繁体   中英

!$var is different than isset()?

I'm using a PHP script that is showing me the following warning:

Undefined variable: _pagi_htaccess

The line is this one:

if (!$var){

I did the following change:

if (!isset($var)){

The warning is not there anymore, but the script is not working. Before the change, the code inside the if was executed, now with !isset, the code is executing the else. So, i don't understand the difference between !$var and !isset($var)

Try this:

if (isset($var) && !$var) {

This first checks to see if the variable exists and then, and only then, checks to see if it is FALSE.

Be aware: This is not good programming. It's better to find out why, in certain circumstances, $var does not exist. It's probably caused by some kind of coding error.

as @Halfstop and @Alex says. !$var is the negation of $var, $var is convert to boolean and then it is evaluate, if $var is equal true, !$var is equal false.

In other hand, isset($var) determines if a variable is set and is not NULL, then if you have if(!isset($var)) you are asking if not set and is NULL $var.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM