What does if (! $ Variablename) do in PHP?

I know that != "Not equal", but what does it mean when you have this:

 if(!$something) 

My first guess is related to exceptions, but the look of google returns nothing.

So what does that mean?

+4
source share
11 answers

Regardless of what is in the variable, it is converted to a logical value (the variable itself, of course, remains intact), and then the NOT ( ! ) Operation is performed as a result of the Boolean. The conversion will happen because ! is a logical operator and works only with boolean values.

When converting to boolean, the following values ​​are considered FALSE:

  • logical false itself
  • integer 0 (zero)
  • float 0.0 (zero)
  • empty string and string "0"
  • array with zero elements
  • object with null member variables (PHP 4 only)
  • special type NULL (including undefined variables)
  • SimpleXML objects created from empty tags

Tip. If the variable does not have to be logical, you can use something more specific, for example isset($variable) , empty($variable) , $variable === '' , etc. depending on what you want to check. See the manual for more details.

+10
source

This is the same as:

 if((bool)$something != true) { 

See: http://www.php.net/manual/en/control-structures.if.php

+6
source
 if (!$something) { 

is an equivalent function

 if ($something == false) { 
+3
source

Checks if something is fake.

+2
source

It just means "If Not Something."

 if (!false) { this_happens_because_not_false_is_true(); } 
+1
source

if(!$variable) same as if($variable == false) , so it checks if the $ false variable is false Look at @ baΕΎmegakapa's answer to find out which values ​​are considered false.

+1
source

Checks if $something false.

0
source

it checks if !$something false, or you can understand it as (if not $ something), then {// it will execute}, and if $ something is present, then it will not be entered in if

0
source

! $ variable - logical operator "Not" http://uk3.php.net/manual/en/language.operators.logical.php

it takes a logical meaning and flips it. True becomes false, and false becomes true.

0
source

It converts the variable to the boolean equivalent of the variable. This can be done in several cases:

 <?php // Case 1: $variable is boolean $variable = true; $variable = !$variable; // Changes to false; var_dump($variable); // bool(false) // Case 2a: $variable is a positive integer $variable = 5; $variable = !$variable; // Changes to false; var_dump($variable); // bool(false) // Case 2b: $variable is an integer other than 0 $variable = 0; $variable = !$variable; // Changes to false; var_dump($variable); // bool(true) // Case 2c: $variable is a negative integer $variable = -5; $variable = !$variable; // Changes to false; var_dump($variable); // bool(false) // Case 3a: $variable is string $variable = "Hello"; $variable = !$variable; // Changes to false; var_dump($variable); // bool(false) // Case 3b: $variable is empty string $variable = ""; $variable = !$variable; // Changes to false; var_dump($variable); // bool(true) ?> 

In short, this does the opposite of the empty() function! :)

Hope this helps! :)

0
source
 if($somethin == ""){ } Or if($somethin != ""){ } 
-2
source

All Articles