• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 882
  • Last Modified:

How to check if a variable is undefined, null, or empty in php?

I am wondering how do I check if a variable is undefined, null, or empty in php?

In javascript I would do the following:

if (type of variable === 'undefined')  // for undefined
if (variable === null) // for null
if (variable === '') // for empty

Open in new window


but what about in php? Is it the same?
0
FairyBusiness
Asked:
FairyBusiness
  • 5
  • 4
  • 2
  • +4
3 Solutions
 
GaryCommented:
if(isset($myvar)){
// ....
}

Can be used for any var, $_POST, $_GET etc...
0
 
thehagmanCommented:
lookup the differences between
isset($x), empty($x), is_null($x), !$x, $x === FALSE, $x === NULL

Open in new window

I think not all cases can be readily verified.
I suggest you evaluate al of the above expressions with $undfefinedvar, NULL, 0, FALSE, and '' to get an overview
0
 
Dave BaldwinFixer of ProblemsCommented:
PHP also has is_null() and a few other is_... you can use to check your variables with.  http://us2.php.net/manual/en/function.is-null.php   There is empty() also.  http://us2.php.net/manual/en/function.empty.php
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
GaryCommented:
True missed the empy in your question, isset will check if its null or non existant (undefined)
0
 
Barry62Commented:
to check if a variable is defined, use defined()
to check if a variable is null, use is_null()
to check if a variable is empty, use empty()

All of these are boolean values.

Oh, and isset() can only be used to check if constants exists.
0
 
Kusala WijayasenaSoftware EngineerCommented:
0
 
Ray PaseurCommented:
PHP is a loosely typed language, so variables and expressions are evaluated as Truthy or Falsy.

If you want an all-inclusive test for undefined, null, and empty you may be out of luck.  But if you're willing to accept Zero as part of the picture you can usually do something like...

if ($var) // TRUTHY
if (!$var) // FALSY

You might want a close reading of the online man pages, because some definitions around this subject are changing.
http://php.net/manual/en/function.empty.php

See also the PHP type comparison tables.
http://php.net/manual/en/types.comparisons.php

HTH, ~Ray
0
 
FairyBusinessAuthor Commented:
defined() and empty() are not working as expected.... here is my code:

    foreach ($_POST as $key => $value) {
    if(is_null($value)) echo "null, ";
    if (!defined($value)) echo "undefined, ";
    if (empty($data)) echo "empty, ";
    }

Open in new window


everything is coming up as undefined or empty when I do this, and I am able to echo data from my form so I know not everything is... why would everything come up as undefined or empty?  Am I using them wrong?
0
 
Dave BaldwinFixer of ProblemsCommented:
I would at least echo the $value to see what is being compared.
0
 
Barry62Commented:
Are you trying to check form fields?  If so, you can't use these methods to check those directly.  You said the word 'variables'.  So what you need to do is put your $_POST values into variables and check those.  Like this:

$name = $_POST['name'];
if(is_null($name)) echo "null, ";

Open in new window

0
 
FairyBusinessAuthor Commented:
this is what I have:

    $output = '';
    foreach ($_POST as $key => $value) {
        if(is_null($value)) echo "null, ";
        if (!defined($value)) echo "undefined, ";
        if (empty($value)) echo "empty, ";
        $output .= $key . ': ' . $value . ', ';

    }
    echo $output;  

Open in new window


and this is what is returned:

undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, title: hi, description: hello, action: saveData, primary: green3.png, other1: aqua_hole2.png, other2: undefined, other3: undefined, other4: undefined,

there should not be this many undefineds...
0
 
FairyBusinessAuthor Commented:
@Barry62 would the $value in my foreach not work?
0
 
Dave BaldwinFixer of ProblemsCommented:
That's probably not true, I've never had PHP lie to me.  You need to see what it is calling 'undefined'.  Try this and post the results.

    $output = '';
    foreach ($_POST as $key => $value) {
        echo $key . ': ' . $value . '  ';
        if(is_null($value)) echo "null, ";
        if (!defined($value)) echo "undefined, ";
        if (empty($value)) echo "empty, ";
        $output .= $key . ': ' . $value . ', ';
        echo "<br>";
    }
    echo $output;  
 

Open in new window

0
 
FairyBusinessAuthor Commented:
@DaveBaldwin ok I used your code snippet instead of mine and these were the results that I got back (mind you I'm just logging them in the console for now so the br tags won't help here)

title: hi  undefined, <br>description: hello  undefined, <br>function: saveData  undefined, <br>primary: aqua_hole3.png  undefined, <br>other1: undefined  undefined, <br>other2: undefined  undefined, <br>other3: undefined  undefined, <br>other4: undefined  undefined, <br>title: hi, description: hello, function: saveData, primary: aqua_hole3.png, other1: undefined, other2: undefined, other3: undefined, other4: undefined,
0
 
Dave BaldwinFixer of ProblemsCommented:
Ok, I just looked up 'defined' and it doesn't apply here because none of these are constants.  http://us3.php.net/manual/en/function.defined.php  And 'empty' doesn't apply because you are checking the values, not the variables.  http://us3.php.net/manual/en/function.empty.php  is_null is the only one that applies but it should never report 'null' because in a 'foreach' your $value will always exist.

Obviously, you need a different plan.
0
 
Ray PaseurCommented:
Try making the test with isset() instead of defined().

This will tell you something you need to know to evaluate the quality of your tests:
var_dump($_POST);

All of the PHP functions are documented with user-contributed notes in the online man pages.  Example:
http://php.net/manual/en/function.defined.php

Checkboxes and radio buttons that are omitted from the POST request (unchecked) are NOT SET in the request.  Inputs of type=text and type=hidden are always present but may be empty().  PHP provides some online learning materials about this sort of thing.  Check this page and the links in the left sidebar:
http://php.net/manual/en/tutorial.forms.php
0
 
FairyBusinessAuthor Commented:
thanks
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 5
  • 4
  • 2
  • +4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now