You are here: Re: eval() « PHP Language « IT news, forums, messages
Re: eval()

Posted by shimmyshack on 02/27/07 01:21

On 27 Feb, 01:03, "shimmyshack" <matt.fa...@gmail.com> wrote:
> On 27 Feb, 00:22, "rog" <jk!ttop5@mnpX$.net> wrote:
>
>
>
> > Hello Matt,
> > Excuse me, but that's the weirdest looking PHP syntax I've ever seen!
> > How do you know that???
> > I use the { } occasionally and was looking for it in the PHP docs to see if
> > I could use it here. I can't find it in the docs at all. Is {} an
> > operator?
> > Anyway...Thanks!!
> > Roger
>
> > "shimmyshack" <matt.fa...@gmail.com> wrote in message
>
> >news:1172523882.320703.317550@8g2000cwh.googlegroups.com...
>
> > > On 26 Feb, 20:55, "rog" <jk!ttop5@mnpX$.net> wrote:
> > >> Sorry, that should read:
>
> > >> for($x=1;$x<=3;$x++)
> > >> {
> > >> $str='$unit' . $x
> > >> echo eval($str);
>
> > >> }
>
> > >> Still doesn't work.
>
> > >> Roger
>
> > >> "rog" <jk!ttop5@mnpX$.net> wrote in message
>
> > >>news:b56dnVxMKbsi1X7YnZ2dnUVZ_vmqnZ2d@comcast.com...
>
> > >> >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
>
> > > you could do this:
>
> > > <?php
> > > $unit1 = '123';
> > > $unit2 = '231';
> > > $unit3 = '321';
>
> > > for($x=1;$x<=3;$x++)
> > > {
> > > echo ${'unit' . $x} . "\n<br />";
> > > }
> > > ?>
>
> norms way is nicer because it forces the String type,
> the syntax is interesting isnt it, and has different uses based on
> contxt.
> for instance
> $string = 'rog';
> echo $string{2} would output g
> whereas
> echo {$string} would
>
> What you are doing is called a "variable variable" $$, you can find it
> under that in the php docs. There's an example more or less equal to
> your problem entered at 18-Oct-2002 in the manual.
> It isnt limited to one layer so it has a multitude of uses, (if you
> can figure them out!)
> $confusing = 'hi';
> $hi = 'there';
> $there = 'this';
> $this = 'is';
> $is = 'confusing!';
> echo $$$$$confusing; //confusing!
> (I made that more confusing by making it ciruclar I could have had $$$$
> $start with $start='there')
>
> Also check the manual under complex (curly) syntax
>
> Don't worry about seeing things for the first time, I admit after
> nearly 5 years of php programming I only realised the other day that
> you could have multiple arguments in an echo statement,
> echo 'hi! :' , ' like' , ', this';
> I felt a bit humbled for that gap! (this forum I think)

whoops sorry for the half posting! To take Norms eg. and play with it,
look at this, (its in the manual in the "complex (curly) syntax"
section

for($x=1;$x<=3;$x++)
{
${"unit$x"} = $x;
echo "\$unit${x} = ${"unit$x"}";
echo ' <em>';
echo "\$unit${x} = $unit$x";
echo '</em>' . "<br />\n";
}

Your colouriser might have difficult with this, it is actually the
lines (because of the multiple consecutive " without concatenating
symbol . - the test of a decent colouriser!)

echo "\$unit${x} = ${"unit$x"}";
echo "\$unit${x} = $unit$x";

which it is having a problem with, they produce identical output, the
backslash is the part that stops the variable's value being output.
The " around the $unit$x mean the variable is parsed, and the output
taken as a string.

echo '\$unit${x} = $unit$x';

wouldnt work (it would lead to the last $unit$x being output as the
string $unit$x.

Have you come across this type of string?

$string = 'rog';
$text = <<<SOMETHING
Here is some \$string: $string
<we can put anything we like "here" or "here"
'quotes' etc...
SOMETHING;

it is very useful when combined with curly syntax, it is a bit like
the <pre> for php.

boring lecture over.

 

Navigation:

[Reply to this 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

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