Zend Framework Forum

Go Back   Zend Framework Forum > Zend Framework Forum > General Talks

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 04-15-2009, 03:10 PM
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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 04-15-2009, 04:48 PM
Rhino's Avatar
Senior Member
 
Join Date: Feb 2009
Location: Lost Angeles, CA
Posts: 105
Send a message via AIM to Rhino
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]
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 04-16-2009, 08:59 AM
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()"?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 04-16-2009, 04:40 PM
Rhino's Avatar
Senior Member
 
Join Date: Feb 2009
Location: Lost Angeles, CA
Posts: 105
Send a message via AIM to Rhino
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...
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 04-16-2009, 04:51 PM
SirAdrian's Avatar
Member
 
Join Date: Apr 2008
Posts: 83
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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 04-16-2009, 05:04 PM
Rhino's Avatar
Senior Member
 
Join Date: Feb 2009
Location: Lost Angeles, CA
Posts: 105
Send a message via AIM to Rhino
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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT. The time now is 12:26 PM.


Designed by: Miner Skinz Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.1.0