NYCPHP Meetup

NYPHP.org

[nycphp-talk] ${$variable}

Chris Shiflett shiflett at php.net
Wed Aug 27 15:20:12 EDT 2003


I'd like to make a small note regarding this topic. I think it's much more
readable (and correct) to use the empty() function, especially if that is what
you are wanting to check.

Consider this:

<?
$foo = 'bar';
$bar = 0;

if (!$$foo)
{
     echo '<p>$bar is empty</p>';
}
?>

That echo statement isn't exactly telling the whole truth, is it? $bar isn't
empty; it is the integer 0. Conditional statements are evaluated as boolean,
and 0 evaluates to false.

Consider this as an alternative:

<?
$foo = 'bar';
$bar = 0;

if (empty($$foo))
{
     echo '<p>$bar is empty</p>';
}
?>

To me, it is much easier to read, and it might be more valid, depending on
whether you are really wanting to test whether something is empty and not
whether it is false.

Hope that helps.

Chris

=====
Become a better Web developer with the HTTP Developer's Handbook
http://httphandbook.org/



More information about the talk mailing list