NYCPHP Meetup

[nycphp-talk] ${$variable}

Analysis & Solutions danielc at analysisandsolutions.com
Wed Aug 27 15:07:04 EDT 2003


On Wed, Aug 27, 2003 at 02:46:25PM -0400, Michael Southwell wrote:
> A line of code says this:
>   if (!(${$foo})) do something if the value is empty
> 
> If $foo="bar" and $bar="something" then will ${$foo} evaluate to 
> "something"--which I assume is the purpose of the curly brackets?  But it 
> doesn't seem to work when the value is empty, though I would swear it used 
> to.

What?  Your code works as I expected it to.  Perhaps you have different 
expectations.

Code:
   $foo="bar";
   $bar="";
   if (!(${$foo})) echo 'something if the value is empty';

Results:
   something if the value is empty

But, if I set
   $bar="blah"; 
then no output is generated, just as the code requests.

In english, the code says if the variable variable "foo" -- which refers
in this case to the value of the real variable "bar" -- is empty, equals 
0 or equals false, then print the message out.

Is that what you thought it means?

By the way, the curleys aren't needed in this context, nor are the inner 
set of parentheses.  Personally, for clarity, I'd write it like this:

   $foo = 'bar';
   $bar = '';
   if (!$$foo) {
       echo 'something if the value is empty';
   }

--Dan

-- 
     FREE scripts that make web and database programming easier
           http://www.analysisandsolutions.com/software/
 T H E   A N A L Y S I S   A N D   S O L U T I O N S   C O M P A N Y
 4015 7th Ave #4AJ, Brooklyn NY    v: 718-854-0335   f: 718-854-0409



More information about the talk mailing list