Forum Moderators: coopster

Message Too Old, No Replies

Very useful variable definition but very weird

         

NomikOS

1:42 am on Jun 30, 2009 (gmt 0)

10+ Year Member



I remember having used something like this:

$'selected'.$_GET['id] = ' selected';

OR

$"selected{$_GET['id]}" = ' selected';

How is it?

omoutop

7:29 am on Jun 30, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



sounds like a Variable variables issue

check on this [us2.php.net] if that's is the case

barns101

12:15 pm on Jun 30, 2009 (gmt 0)

10+ Year Member



Curly braces are also used to explicitly define your variables if there could be any ambiguity: [uk.php.net...]

jatar_k

1:37 pm on Jun 30, 2009 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



the links are god reading, the answer< i think, is like this, concatenate between braces

${'selected' . $_GET['id']} = ' selected';

NomikOS

2:51 am on Jul 1, 2009 (gmt 0)

10+ Year Member



&&... yes. it is:

${'selected' . $_GET['id']} = ' selected';

you can not say it's trivial but very useful, right?

"thanks for your help {$colegas}" && for jatar_k --the bible.

bkeep

5:26 am on Jul 1, 2009 (gmt 0)

10+ Year Member



I use something similar for setting the column names as my variable name so when I add a new column to the db it is automatically added to the variables list with the column contents

$env = getEnvSettings();
foreach ($env as $field => $value ) {
${$field} = $value;
}

jatar_k

1:05 pm on Jul 1, 2009 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



>> you can not say it's trivial but very useful, right?

I use variable variables for anything I can but it doesn't come up very often. Mostly I use them for working with variable amounts of data either in variables or in arrays.

NomikOS

8:07 am on Jul 11, 2009 (gmt 0)

10+ Year Member



I used forget all about this construction, but no more. Thanks.

Another use:


for ($i = 1; $i <= 20; $i++)
{
$img = $listing->{'images'.$i};
if ($img)
echo "Picture[$i] = '{$img}_lg.jpg';\n";
}