1321941
1321941

Reputation: 2180

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

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?

Upvotes: 6

Views: 10498

Answers (12)

Duke
Duke

Reputation: 21

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.! :)

Upvotes: 0

matt
matt

Reputation: 11

Checks to see if $something is false.

Upvotes: 0

shoaib akhunzada
shoaib akhunzada

Reputation: 7

if($somethin == ""){
}

Or

if($somethin != ""){
}

Upvotes: -3

kapa
kapa

Reputation: 78751

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.

Upvotes: 15

Fabian
Fabian

Reputation: 3495

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.

Upvotes: 1

Praveen Kumar Purushothaman
Praveen Kumar Purushothaman

Reputation: 167250

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

Upvotes: 1

John David Ravenscroft
John David Ravenscroft

Reputation: 191

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

Upvotes: 0

user1453390
user1453390

Reputation: 34

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

Upvotes: 0

Artefact2
Artefact2

Reputation: 7654

It's the same as:

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

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

Upvotes: 6

Rick Kuipers
Rick Kuipers

Reputation: 6607

if (!$something) {

is an equivelent of

if ($something == false) {

Upvotes: 3

BenM
BenM

Reputation: 53246

Checks to see whether $something is falsy.

Upvotes: 2

Quentin
Quentin

Reputation: 944530

It just means "If not something".

if (!false) {
   this_happens_because_not_false_is_true();
}

Upvotes: 1

Related Questions