Search code examples
phpif-statementbooleanisset

!$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)


Solution

  • 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.