Do you have a question? Post it now! No Registration Necessary. Now with pictures!
- Posted on
- Dave Monk
August 5, 2006, 8:48 pm
rate this thread
I'm reasonably proficient in PHP but have been asked how to do
something which has got me stumped. Hence, my posting:
I want to create a small number of variables, $pos1, $pos2 ... $pos5
within a loop and assign them values from an array, but creating the
variable names with incrementing numbers, well I've no idea how the PHP
syntax for it works.
========== START CODE ==========
$myarray = array('a','b','c','d','e');
$pos.$a = $myarray[$a]; //Create $pos0 here on first loop,
// $pos1 next loop and so forth..
//Test assigned variable
echo $pos3; // $pos3 should equal 'd'
========== END CODE ==========
Re: Technique for creating variables within a loop
You can do it the way Janwillem suggests, but I would ask why you want
to do this. Why not use an array ($pos, $pos, etc.)? That's what
Photos from 40 countries on 5 continents: http://travel.u.nu
Latest photos: Malaysia; Thailand; Singapore; Spain; Morocco
Airports of the world: http://airport.u.nu