Re: [PHP] Re: isset($a->b) even if $a->b = null



the solution has been found. array_key_exists() can actually be used
on objects, and yields the correct result.

http://no.php.net/array_key_exists

thanks to dordea cosmin for pointing this out.

On 17/08/07, Olav Mørkrid <olav.morkrid@xxxxxxxxx> wrote:
the test i need should give the following results:

- FALSE when $a->b does not exist at all
- TRUE when $a->b = null
- TRUE when $a->b = <any value>

empty() gives true for both $a->b = null and not setting any value, so
that's no good.

borokovs suggestion seems to miss the purpose.

anyone else?

On 17/08/07, Colin Guthrie <gmane@xxxxxxxxxxxxxx> wrote:
Olav Mørkrid wrote:
how do i test if a property of a stdclass object is set, even if its
value is null, similar to how array_key_exists() works for arrays.

the following method fails:

$a->b = null;
if(isset($a->b))
echo "yes";

and property_exists() seems only to work for defined objects.

hope someone can help. thanks!

You can try:
unset($a-b)

Or change isset() to empty().

empty() catches more than isset() e.g. '' (empty string), false, 0 etc.
are considered "empty". Depending on your logic it can still be very
useful. It is a language construct rather than a function so it's also
efficient.

Col

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



.



Relevant Pages

  • Re: [PHP] Re: isset($a->b) even if $a->b = null
    ... echo "yes"; ... Or change isset() to empty. ... are considered "empty". ... Depending on your logic it can still be very ...
    (php.general)
  • Application::CompanyName property
    ... simple form app, yet the corresponding properties in the global ... Application object remain empty. ... yields an empty string in the Application::CompanyName property. ... Do I have to acces the Application ...
    (microsoft.public.dotnet.languages.vc)
  • Re: Possible ways to link existing user table into SQLMembershipProvider?
    ... Got a problem now where 1 out of 2 login attempts yields an empty ... Same with normal SQLMembership. ...
    (microsoft.public.dotnet.framework.aspnet.security)
  • Re: Dsum return 0 instead of Null?
    ... I don't think Joan meant DSum* 1, ... This is because Nz) converts Null to Empty ... and Empty * 1 yields 0. ... This makes use of the Nz function's optional second argument. ...
    (microsoft.public.access.queries)
  • Wordpress <=2.0.2 cache shell injection
    ... if user registration is enabled, ... php files. ... DB_PASSWORD costant could be empty, ... echo 'No response from '.$host.':'.$port; die; ...
    (Bugtraq)