Reply to Re: eval()

Your name:

Reply:


Posted by Norman Peelman on 02/27/07 02:41

rog wrote:
> Thanks,
>
> It looks like it's all about the {} operator or whatever it is.
> Wish they were covered in the php docs.
>
> :)
> R
>
>
>
> "Norman Peelman" <npeelman@cfl.rr.com> wrote in message
> news:45e37722$0$24693$4c368faf@roadrunner.com...
>
>> rog wrote:
>>
>>> I have some variables, $unit1, $unit2 and $unit3, that I would like to
>>> read from a loop like this (or any other way):
>>>
>>> for($x=1;$x<=3;$x++)
>>> {
>>> $str='unit' . $x
>>> echo eval($str);
>>> }
>>>
>>> The debugger shows $str as '$unit1' but eval() gives a parse error.
>>> Can someone give a tip?
>>>
>>> Thanks,
>>> Roger
>>>
>>>
>>>
>> for($x=1;$x<=3;$x++)
>> { // create variabls unit1, unit2, unit3...
>> ${"unit$x"} = $x;
>> $str = "unit$x = ".${"unit$x"}.'<br>';
>> //echo "unit$x = ${"unit$x"}<br>"; // will work too...
>> }
>> echo $unit2;
>>
>>
>> output:
>>
>> unit1 = 1
>> unit2 = 2
>> unit3 = 3
>> 2
>>
>> Norm
>>
>
>
>
They're there somewhere... stuff between {...} gets evaluated as a
whole, some uses:

1) ${"unit$x"} = ${"unit1"} = $unit1 where $x = 1
2) echo "The value is: {$unit["level1]["level2"]["level3"]}", only way
to use multi-dimensional arrays inside a string (I think).
notice the $ is inside the curly brace on that one. Normally you can
do: echo "The value is: $unit['level1']"; without any problems... no
need for curly braces.

Norm

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация