What does if (!$variablename) do in PHP?
Asked Answered
F

12

5

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

if(!$something)

My first guess is something to do with exceptions, but a look around google did not return anything.

So what does this do?

Fallfish answered 13/6, 2012 at 10:19 Comment(0)
G
14

Whatever is in the variable is converted to a Boolean (the variable itself of course remains intact), and then a NOT operation (!) is done on the resulting Boolean. The conversion will happen because ! is a Logical Operator and only works on Boolean values.

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

  • the boolean FALSE itself
  • the integer 0 (zero)
  • the float 0.0 (zero)
  • the empty string, and the string "0"
  • an array with zero elements
  • an object with zero member variables (PHP 4 only)
  • the special type NULL (including unset variables)
  • SimpleXML objects created from empty tags

Tip: If the variable is not expected to be Boolean, you might want to use something more specific like isset($variable), empty($variable), $variable === '', etc. depending on what you want to check for. Check the manual for details.

Gelinas answered 13/6, 2012 at 10:23 Comment(0)
S
6

It's the same as:

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

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

Silvester answered 13/6, 2012 at 10:21 Comment(1)
This one is the best answer, other forget to mention that the test is casting bool on $something to check it, which is very important depending on what is in $something. I'd recommand reading carefully the doc about casting to booleans in phpBarozzi
D
3
if (!$something) {

is an equivelent of

if ($something == false) {
Denominate answered 13/6, 2012 at 10:21 Comment(2)
False. For example !"0" is true, but "0" === false is not.Silvester
@Jeroen No, actually == is what happens here. The variable will be converted with ! to Boolean. So the strict equality will not return the same result.Gelinas
T
2

Checks to see whether $something is falsy.

Tachylyte answered 13/6, 2012 at 10:21 Comment(0)
I
1

It just means "If not something".

if (!false) {
   this_happens_because_not_false_is_true();
}
Iraq answered 13/6, 2012 at 10:21 Comment(0)
B
1

if(!$variable) is the same as if($variable == false) so it checks if $variable is false
Look at @bažmegakapa answer to see which values are considered false.

Beaman answered 13/6, 2012 at 10:24 Comment(0)
E
1

It converts the variable into boolean equivalent of the variable. This can be given in a few 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, it makes the opposite of the empty() function! :)

Hope this helps! :)

Emphysema answered 13/6, 2012 at 10:26 Comment(2)
Well, not exactly the opposite of empty().Gelinas
There is of course. I just checked the manual link I posted in my answer, and then the manual of empty() and there are some differences (haven't tried myself).Gelinas
A
0

Checks to see if $something is false.

Ahoufe answered 13/6, 2012 at 10:23 Comment(1)
Not really. That would be $variable === false.Gelinas
A
0

it check if !$something is false or you can understand it like (if not$something) then {//this will execute } and if $something is present then the this will not enter in the if

Altruism answered 13/6, 2012 at 10:23 Comment(0)
P
0

!$variable is the 'Not' logical operator https://www.php.net/manual/en/language.operators.logical.php

it takes a boolean value and flips it. True becomes false and false becomes true.

Phytohormone answered 13/6, 2012 at 10:24 Comment(0)
D
0

I met following code once

 if (!$this->error) {
      return false;
    } else {
    return true;
    }   

I thought "if no error , why false?? Thats wrong!" Because i thought "!" operator equals "NOT". And swaped returns. But everytime error condition codes ran. Then learned. "!" operator converts variables to boolean. Empty variables converted to "false". And "if" statements with "!" operator run like that "if this boolean is false, return false, else return true.! :)

Dariodariole answered 21/8, 2020 at 16:29 Comment(1)
When an if else statement is used to return true and false the statement can be simplified to return either the if condition or the reverse of the if condition depending on the order of the return statements in the if else. In this case return !!$this->error;Judaism
T
-3
if($somethin == ""){
}

Or

if($somethin != ""){
}
Tractable answered 13/6, 2012 at 11:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.