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?
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?
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.
It's the same as:
if((bool)$something != true) {
if (!$something) {
is an equivelent of
if ($something == false) {
!"0"
is true, but "0" === false
is not. –
Silvester ==
is what happens here. The variable will be converted with !
to Boolean. So the strict equality will not return the same result. –
Gelinas It just means "If not something".
if (!false) {
this_happens_because_not_false_is_true();
}
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.
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! :)
empty()
. –
Gelinas empty()
and there are some differences (haven't tried myself). –
Gelinas Checks to see if $something
is false.
$variable === false
. –
Gelinas 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
!$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.
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.! :)
return !!$this->error;
–
Judaism © 2022 - 2024 — McMap. All rights reserved.