![]() |
|
|||
|
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(); [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 |
|
|||
|
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()"? |
|
||||
|
Quick correction, if ($object['prop']) will tell you if $object['prop'] == true. The expression ($object['prop']) will be evaluated as a boolean.
Quote:
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. |
|
||||
|
yea that makes sense. I thought empty() only worked on arrays? But it turns out that you are absolutely correct:
Quote:
Last edited by Rhino; 04-16-2009 at 05:10 PM. |
![]() |
| Thread Tools | Search this Thread |
| Display Modes | |
|
|
| 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 |
![]() |