php equivalent to javascript eval()? (or just a solution =o)

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Axium
Forum Newbie
Posts: 1
Joined: Mon Nov 25, 2002 9:34 pm
Location: Blacksburg, VA
Contact:

php equivalent to javascript eval()? (or just a solution =o)

Post by Axium »

I'm using variables passed in through a form to generate pages of data dynamically (they're all on separate layers linked together, and I don't know how many pages beforehand). When I make the url for the link variables that represent different pages get numbers appended to them: &name0=name...&name1=name2...etc.

Anyway.

On the receiving end I'm running into the problem that I have to know the variable name before I try to output it, but I don't want to hardcode it.

I'd want something to the effect of:

Code: Select all

for ($i = 0; $i < $noofelements; $i++)
   echo ($name+$i);
But that will only give me the contents of $name + the integer 1, I want the contents of $name1.

In javascript you can use the eval() function to turn a string into an executable statement, I'm just wondering if there's an equivalent in php or someone knows how to help me with this problem.

Thanks ahead of time,
Justin
User avatar
mydimension
Moderator
Posts: 531
Joined: Tue Apr 23, 2002 6:00 pm
Location: Lowell, MA USA
Contact:

Post by mydimension »

its the eval() function. and works the same way i think. http://www.php.net/manual/en/function.eval.php
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

also note http://www.php.net/manual/en/language.v ... riable.php

Code: Select all

<?php
$name0 = 'test';
$name1 = 'value';
$noofelements = 2;

for ($i = 0; $i < $noofelements; $i++)
   echo ${'name'.$i}, '<br/>';
?>
Post Reply