foreach

I'm new to php and often discover that what looks like an error to me in a
piece of code is in fact functionality that I was unaware of.
In another php forum today I came across the snip below.
I don't think it's appropriate to ask in that forum while the OP's question
had not yet been addressed.
The usage of the curly brackets is screwing with my head, most especialy the
last line.

foreach (array($section, 'newImage', "${section}_width", "$
{section}_height") as $val)
$this->$val =& ${$val};

I would be most grateful if someone could shed some light on this.
TIA
Vince
Vince Morgan [ So, 15 April 2007 03:03 ] [ ID #1688250 ]

Re: foreach

Vince Morgan wrote:
> I'm new to php and often discover that what looks like an error to me in a
> piece of code is in fact functionality that I was unaware of.
> In another php forum today I came across the snip below.
> I don't think it's appropriate to ask in that forum while the OP's question
> had not yet been addressed.
> The usage of the curly brackets is screwing with my head, most especialy the
> last line.
>
> foreach (array($section, 'newImage', "${section}_width", "$
> {section}_height") as $val)
> $this->$val =& ${$val};

It's mainly used to tell what is a variable in a string, say you have a
variable called $hell and you have the following

echo "$hellohm's";

this would try to use the variable $hellohm, which most likely would be an
empty variable and result in a string: 's

echo "{$hell}ohm's";

this would use the value of $hell and add ohm's after the value.

The brackets works outside string too, even if it's not that nice looking IMHO.

In the last line ${$val} is the same as $$val, which makes that the value of
$val is used as a variable name, say you

$hello=5;
$val="hello";
echo $$val; //will echo the $hello value


--

//Aho
Shion [ So, 15 April 2007 07:04 ] [ ID #1688253 ]

Re: foreach

"J.O. Aho" <user [at] example.net> wrote in message
news:58dq4bF2gg80nU1 [at] mid.individual.net...
> It's mainly used to tell what is a variable in a string, say you have a
> variable called $hell and you have the following
>
> echo "$hellohm's";
>
> this would try to use the variable $hellohm, which most likely would be an
> empty variable and result in a string: 's
>
> echo "{$hell}ohm's";
>
> this would use the value of $hell and add ohm's after the value.
>
> The brackets works outside string too, even if it's not that nice looking
IMHO.
>
> In the last line ${$val} is the same as $$val, which makes that the value
of
> $val is used as a variable name, say you
>
> $hello=5;
> $val="hello";
> echo $$val; //will echo the $hello value
>
>
> --
>
> //Aho
Thank you very much indeed.
I think your IMHO is a very valid HO ;)
The revs have reduced a little, but my head is still spinning.
Regards,
Vince
Vince Morgan [ So, 15 April 2007 09:26 ] [ ID #1688255 ]
PHP » alt.php » foreach

Vorheriges Thema: php equivalent to XMLHTTPobj??
Nächstes Thema: PHP & Paypal solution to sell digital goods?