41
if ($user_id == NULL || $user_name == NULL || $user_logged == NULL) {
    $user_id = '-1';
    $user_name = NULL;
    $user_logged = NULL;
}
if ($user_admin == NULL) {
    $user_admin = NULL;
}
  1. Is there any shortest way to do it ?
  2. And if i right, it should be tested with is_null?
  3. It's possible $user_id, $user_name and $user_logged write in one line (maybe array?) without repeating NULL ?
ZeroSuf3r
  • 1,961
  • 8
  • 25
  • 36
  • *(suggested)* http://stackoverflow.com/questions/381265/better-way-to-check-variable-for-null-or-empty-string –  Jan 08 '12 at 12:46
  • I think you can create a function for that... –  Jan 08 '12 at 12:50

12 Answers12

98

If you want to test whether a variable is really NULL, use the identity operator:

$user_id === NULL  // FALSE == NULL is true, FALSE === NULL is false
is_null($user_id)

If you want to check whether a variable is not set:

!isset($user_id)

Or if the variable is not set or has an "empty" value (an empty string, zero, etc., see this page for the full list):

empty($user_id)

If you want to test whether an existing variable contains a value that's considered "not empty" (non-empty string or array, non-zero number, etc..), ! will be sufficient:

!$user_id
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
Rob W
  • 341,306
  • 83
  • 791
  • 678
  • 1
    Note also that `isset()` accepts an unlimited number of arguments. – DaveRandom Jan 08 '12 at 12:49
  • Also note that because he is using `==` above and not `===`, all that is effectively being done is `if (!$var || !$var ...` – DaveRandom Jan 08 '12 at 12:51
  • 2
    Yep, documentation at: [`isset`](http://php.net/manual/en/function.isset.php), [`is_null`](http://php.net/manual/en/function.is-null.php), [`empty`](http://php.net/manual/en/function.empty.php). Judging by the code, I assume that the variables have to be a string. So, I recommend: **`if (!is_string($user_id) || $user_id === '')`**. – Rob W Jan 08 '12 at 12:52
10

You can check if it's not set (or empty) in a number of ways.

if (!$var){ }

Or:

if ($var === null){ } // This checks if the variable, by type, IS null.

Or:

if (empty($var)){ }

You can check if it's declared with:

if (!isset($var)){ }

Take note that PHP interprets 0 (integer) and "" (empty string) and false as "empty" - and dispite being different types, these specific values are by PHP considered the same. It doesn't matter if $var is never set/declared or if it's declared as $var = 0 or $var = "". So often you compare by using the === operator which compares with respect to data type. If $var is 0 (integer), $var == "" or $var == false will validate, but $var === "" or $var === false will not.

Jens
  • 1,302
  • 1
  • 13
  • 20
  • 1
    `!isset($var)` checks if the variable isn't defined and not if it is empty or not. – noob Jan 08 '12 at 12:48
2

here i have explained how the empty function and isset works please use the one that is appropriate also you can use is_null function also

<?php
    $val = 0;
    //evaluates to true because $var is empty
    if (empty($val)) {
        echo '$val is either 0, empty, or not set at all';
    }
    //evaluates to true because $VAR IS SET
    if (isset($val)) {
        echo '$val is set even though it is empty';
    }
    ?>
Manoj Rammoorthy
  • 1,384
  • 16
  • 16
1

To check for null values you can use is_null() as is demonstrated below.

if (is_null($value)) {
   $value = "MY TEXT"; //define to suit
}
Sᴀᴍ Onᴇᴌᴀ
  • 8,218
  • 8
  • 36
  • 58
1

empty() is a little shorter, as an alternative to checking !$user_id as suggested elsewhere:

if (empty($user_id) || empty($user_name) || empty($user_logged)) {
}
Michael Berkowski
  • 267,341
  • 46
  • 444
  • 390
0

The best and easiest way to check if a variable is empty in PHP is just to use the empty() function.

if empty($variable) then ....

user1445657
  • 109
  • 1
  • 3
0
<?php

$nothing = NULL;
$something = '';
$array = array(1,2,3);

// Create a function that checks if a variable is set or empty, and display "$variable_name is SET|EMPTY"
function  check($var) {
    if (isset($var)) {
        echo 'Variable is SET'. PHP_EOL;
  } elseif (empty($var)) { 
        echo 'Variable is empty' . PHP_EOL;

   } 

} 

check($nothing);
check($something);
check($array);
0

Its worth noting - and I only found this out after nearly 9 years of PHP coding that the best way of checking any variable exists is using the empty() function. This is because it doesn't generate errors and even though you turn them off - PHP still generates them! empty() however won't return errors if the variable doesn't exist. So I believe the correct answer is not to check if its null but to do the following

if (!empty($var) && is_null($var))

Note the PHP manual

variable is considered empty if it does not exist or if its value equals FALSE

As opposed to being null which is handy here!

Antony
  • 3,875
  • 30
  • 32
  • Its worth noting that `if (!empty($var) && is_null($var))` will never evaluate as `true` because `null` will never satisfy `!empty()`. – mickmackusa Feb 03 '23 at 08:11
0

Felt compelled to answer this because of the other responses. Use empty() if you can because it covers more bases. Future you will thank me.

For example you will have to do things like isset() && strlen() where instead you could use empty(). Think of it like this empty is like !isset($var) || $var==false

Mike Q
  • 6,716
  • 5
  • 55
  • 62
0

1.

if(!($user_id || $user_name || $user_logged)){
    //do your stuff
}

2 . No. I actually did not understand why you write such a construct.

3 . Put all values into array, for example $ar["user_id"], etc.

false
  • 10,264
  • 13
  • 101
  • 209
Dmitry Zaytsev
  • 23,650
  • 14
  • 92
  • 146
0

Please define what you mean by "empty".

The test I normally use is isset().

Alnitak
  • 334,560
  • 70
  • 407
  • 495
0

you can use isset() routine .

also additionaly you can refer an range of is_type () functions like

is_string(), is_float(),is_int() etc to further specificaly test

Vijeenrosh P.W
  • 359
  • 1
  • 3
  • 8