Page 1 of 1

Is it possible to generate PHP with PHP within PHP

Posted: Tue Sep 26, 2006 11:26 am
by impulse()
I've already tried to and I've had no sucess but I wondered if there's a way to possibly do the following. I want to create an array with every number between 0 and 52, just for a test. I'll explain in code because you'll understand what I mean easier :)

Code: Select all

$cards = array((for $i = 1; $i < 53; $i++) { echo $i; });
To try and get it to do:

Code: Select all

$cards = array ("1", "2", "3");
etc etc...

Stephen,

Posted: Tue Sep 26, 2006 11:30 am
by someberry

Code: Select all

for($i = 1; $i <= 52; $i++)
{
    $myArray[$i] = [$i];
}

print_r($myArray);

Posted: Tue Sep 26, 2006 11:30 am
by Maugrim_The_Reaper
Try first of all to generate a valid PHP array with all the numbers - i.e. just as a string. Then look up the eval() function and how it can be used to evaluate a string as PHP.

Posted: Tue Sep 26, 2006 11:31 am
by Luke
You can execute php from within php with eval() but in this case, you don't need to...

Code: Select all

$cards = array();
for($i = 1; $i < 53; $i++) {
    $cards[] = $i;
}

Posted: Tue Sep 26, 2006 12:02 pm
by volka

Code: Select all

$card = range(1,52);

Posted: Tue Sep 26, 2006 12:24 pm
by Luke
thanks volka :oops: forgot about range()