Author Topic: Variable Variables  (Read 223 times)

0 Members and 1 Guest are viewing this topic.

Offline sstangle73Topic starter

  • Enthusiast
  • Posts: 285
    • View Profile
Variable Variables
« on: March 14, 2010, 09:54:13 PM »
i am trying to make a script that will make a varible 18 times here is what i have something is wrong

for($i=1$i<=18$i++){
${
"player" $i "_number"} = ${$array[player .  $i _number]};
}

it is something with the second half the
Code: [Select]
${$array[player .  $i . _number]};i am trying to get it so it will do:
Code: [Select]
$player1_number = $array['player1_number'];
$player2_number = $array['player2_number'];
$player3_number = $array['player3_number'];
ect

Thanks!

EDIT
the error i am getting is
Code: [Select]
Notice: Use of undefined constant player - assumed 'player' in /homepages/27/d193007783/htdocs/maddogmania/application/roster.php on line 22

Notice: Use of undefined constant _number - assumed '_number' in /homepages/27/d193007783/htdocs/maddogmania/application/roster.php on line 22

Notice: Undefined variable: 07 in /homepages/27/d193007783/htdocs/maddogmania/application/roster.php on line 22
the third one where it has the variable 07, 07 is what the $player1_number should be set to if it worked right
« Last Edit: March 14, 2010, 09:56:29 PM by sstangle73 »

Offline thorpe

  • Administrator
  • 'Mind Boggling!'
  • *
  • Posts: 29,255
    • View Profile
Re: Variable Variables
« Reply #1 on: March 14, 2010, 09:58:20 PM »
Assuming $array is an array.

${"player" $i "_number"} = $array['player' .  $i '_number'];

You would be much better off using arrays instead of variable variables though. They are slow, and make your code much harder to read.

Offline sstangle73Topic starter

  • Enthusiast
  • Posts: 285
    • View Profile
Re: Variable Variables
« Reply #2 on: March 14, 2010, 10:01:53 PM »
Thanks thorpe!!

Offline Andy-H

  • Addict
  • Posts: 1,690
  • Gender: Male
    • View Profile
    • Phantom Ltd - Vehicle tracking and accessories
Re: Variable Variables
« Reply #3 on: March 14, 2010, 10:04:12 PM »
if you already have the array keys set as your desired variable names, as in thorpe's example; look into extract().