What does exclamation mark mean in PHP?

Possible duplicate:
Link - what does this symbol mean in PHP?
Confuse with empty, isset ,! empty ,! isset

In PHP what's the difference between:

if(!isset) 
if(isset)

      

Same with if(!empty)

and if(empty)

?

What is he doing "!" symbol means?

+3


source to share


3 answers


!

is the logical negation operator or NOT

. This changes the meaning of the logic test.

I.e:

  • if(isset)

    does something if it isset

    is logical True

    .
  • if(!isset)

    does something if it isset

    is logical . False



Learn more about operators (boolean and other types) in the PHP documentation . Take a look !

to solidify your understanding of what he does. While you're there, check out the other boolean operators:

  • &&

    logical AND
  • ||

    logical OR
  • xor

    logical EXCLUSIVE-OR

which are also commonly used in logical operators.

+20


source


The symbol !

is the logical "not" operator. It inverts the boolean value of the expression.

If you have an expression that evaluates to TRUE

, prefixing it with !

makes it evaluate to the value, FALSE

and vice versa.

$test = 'value';

var_dump(isset($test));  // TRUE
var_dump(!isset($test)); // FALSE

      


isset()

returns TRUE

if the specified variable is defined in the current scope with a nonzero value.

empty()

returns TRUE

if the given variable is not defined in the current scope or if it is defined with a value that is considered "empty". These values ​​are:



NULL    // NULL value
0       // Integer/float zero
''      // Empty string
'0'     // String '0'
FALSE   // Boolean FALSE
array() // empty array

      

Depending on the PHP version, an object with no properties can also be considered empty.

As a result, they isset()

both empty()

almost complement each other (they return opposite results), but not quite, since it empty()

performs an additional check on the value of the variable isset()

just checks if it is defined.

Consider the following example:

var_dump(isset($test)); // FALSE
var_dump(empty($test)); // TRUE

$test = '';

var_dump(isset($test)); // TRUE
var_dump(empty($test)); // TRUE

$test = 'value';

var_dump(isset($test)); // TRUE
var_dump(empty($test)); // FALSE

      

+8


source


$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';
}

      

Edit:

here's a test case for you:

$p = false;
echo isset($p) ? '$p is setted : ' : '$p is not setted : ';
echo empty($p) ? '$p is empty' : '$p is not empty';
echo "<BR>";

      

$ p: $ p is empty

+3


source







All Articles