+ Reply to Thread
Results 1 to 6 of 6

Thread: simple way to check if a variable is null?

  1. #1
    Laurent is offline Junior Member
    Join Date
    Mar 2009
    Posts
    14

    Default simple way to check if a variable is null?

    Hello,

    I'm used to JavaScript where it's quite simple to test if the property of an object is not null:

    Code:
    if (object.prop) doSomething();
    However in PHP, I can't find any simple way to do it. Each time I want to check if a variable is not null, I first need to check that it is set (to avoid the "undefined index" notice) and then if it is null. i.e.

    [PHP]if (isset($object['prop']) && $object['prop']) doSomething();[/PHP]

    Is there any way in PHP to do the above in a less verbose way? For example, is there any function that test if a variable is set and non-null?

    Thanks,

    Laurent

  2. #2
    Rhino's Avatar
    Rhino is offline Senior Member
    Join Date
    Feb 2009
    Location
    Lost Angeles, CA
    Posts
    105

    Default

    [PHP]
    if(isset($object['prop']) && !is_null($object['prop'])) doSomething();
    [/PHP]
    OR - the Zend way:
    [PHP]
    if(isset($object['prop']) && null !== $object['prop']) doSomething();
    [/PHP]

  3. #3
    Laurent is offline Junior Member
    Join Date
    Mar 2009
    Posts
    14

    Default

    Actually, I was looking for a less verbose way to do that As I mentioned above, in javascript, it's as simple as "if (object.prop)" while in PHP I have to write a very lengthy statement, which makes the code very heavy for not real benefit.

    So is it possible to do something like "if ($object['prop'])" in PHP? or maybe is there a function such as "isNotSetOrNull()"?

  4. #4
    Rhino's Avatar
    Rhino is offline Senior Member
    Join Date
    Feb 2009
    Location
    Lost Angeles, CA
    Posts
    105

    Default

    if ($object['prop']) will only tell you if $object['prop'] === true or false. if the index 'prop' is not set then that statement will throw an 'undefined index' exception. there is no function "isNotSetOrNull()" that I know of, although I guess you could write one yourself if it's really that much of a concern...

  5. #5
    SirAdrian's Avatar
    SirAdrian is offline Member
    Join Date
    Apr 2008
    Posts
    87

    Default

    Quick correction, if ($object['prop']) will tell you if $object['prop'] == true. The expression ($object['prop']) will be evaluated as a boolean.
    Quote Originally Posted by PHP.net
    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

    Every other value is considered TRUE (including any resource).
    If you want to bypass the E_NOTICE for an undefined variable or index, you can use !empty() to see if the variable is 1) set, and 2) evaluates to true -- empty would be false, but we negate that with the !

    99% of the time we know (or SHOULD know, validate input!) what types we are dealing with, so IMO it's fine to use lazy typing like this. The code you have now checks to see that the value is declared and is null. That's a rare condition to have to check for, honestly.
    Last edited by SirAdrian; 04-16-2009 at 04:54 PM.

  6. #6
    Rhino's Avatar
    Rhino is offline Senior Member
    Join Date
    Feb 2009
    Location
    Lost Angeles, CA
    Posts
    105

    Default

    yea that makes sense. I thought empty() only worked on arrays? But it turns out that you are absolutely correct:
    Quote Originally Posted by http://us3.php.net/empty
    Returns FALSE if var has a non-empty and non-zero value.
    The following things are considered to be empty:
    * "" (an empty string)
    * 0 (0 as an integer)
    * "0" (0 as a string)
    * NULL
    * FALSE
    * array() (an empty array)
    * var $var; (a variable declared, but without a value in a class)
    thank you for pointing that out. the only problem would be - what if you are running empty() on a variable that is set to 0 or '0' and it is legitimately supposed to be set to 0? according to the docs it would consider that to be empty. this has caused me problems before when a null value is invalid input but 0 is not, which is why i had originally began using isset() and null ===
    Last edited by Rhino; 04-16-2009 at 05:10 PM.

+ Reply to Thread

Similar Threads

  1. Zend_Db_Table update, set column to null
    By NewICrash in forum Databases
    Replies: 2
    Last Post: 03-26-2010, 06:08 PM
  2. How to check if a controller exists
    By sasquatch in forum Model-View-Controller (MVC)
    Replies: 0
    Last Post: 03-11-2010, 08:42 PM
  3. hidden form value null problem
    By aniketto in forum General Q&A on Zend Framework
    Replies: 1
    Last Post: 12-25-2008, 12:00 AM
  4. call setOptions method, results reading null value from Session
    By cheyo in forum Authentication & Authorization
    Replies: 0
    Last Post: 06-14-2008, 03:00 AM
  5. Database check if exist
    By ivotrompert in forum Databases
    Replies: 0
    Last Post: 08-19-2007, 01:09 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts