Page 1 of 1

Execute a string in PHP?

Posted: Sat Jul 24, 2004 10:39 pm
by tonic889
Here is my situation. I have some variables in a php program:

$foo1 = "stuff";
$foo2 = "more stuff";
...
$foo50 = "end of stuff";

What I would like to know is if there is a command in PHP to instruct PHP to execute a given string in PHP. So in other words,

<?

$i = 0;
while ($i < 50) {
execute_command("echo $foo" . $i);
i++;
}

?>

..would cause php to execute the commands "echo $foo1", "echo $foo2", etc... The end result of the program would then be:

stuff
more stuff
...
end of stuff.

My question is: Is there a command like "execute_command" (this was just a name I made up) that will do what I've described?

Hope this makes sense...

Posted: Sat Jul 24, 2004 11:00 pm
by feyd
[php_man]eval[/php_man](), however, what you want to do can be done without eval..

Code: Select all

<?php

echo ${'foo' . $i};

?>

Posted: Sat Jul 24, 2004 11:48 pm
by d3ad1ysp0rk
Am I missing something?

What's wrong with:

Code: Select all

$i = 0;
while ($i < 50) {
  echo $foo . $i;
  $i++;
}

Posted: Sun Jul 25, 2004 12:42 am
by tonic889
Lilpunk - When I tried your code, I got just the value of i outputed (ie. 0123456789....etc.)

Feyd - your code worked fine.

Thanks both of you for your replies.

Posted: Sun Jul 25, 2004 6:08 am
by d3ad1ysp0rk
ohhh, i just understood it, sorry it was kinda 3am :P