Is it possible to generate PHP with PHP within PHP

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
impulse()
Forum Regular
Posts: 748
Joined: Wed Aug 09, 2006 8:36 am
Location: Staffordshire, UK
Contact:

Is it possible to generate PHP with PHP within PHP

Post 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,
someberry
Forum Contributor
Posts: 172
Joined: Mon Apr 11, 2005 5:16 am

Post by someberry »

Code: Select all

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

print_r($myArray);
User avatar
Maugrim_The_Reaper
DevNet Master
Posts: 2704
Joined: Tue Nov 02, 2004 5:43 am
Location: Ireland

Post 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.
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post 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;
}
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

Code: Select all

$card = range(1,52);
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

thanks volka :oops: forgot about range()
Post Reply