Jump to content

Is it possible to do something like this?


doogles

Recommended Posts

It was hard to explain with just the title, so I figured I'll explain what I mean here:

 

Say I have various variables with the same name and different endings, like $test1, $test2, $test3, $test4, etc...

 

Would it be possible to create a loop that will do something to each of those variables instead of me typing it for each and every one?

 

Something like:

 

for($x=1; $x <=4; $x++) {echo $test$x;} so it would print each test variable, 1-4? Obviously combining the two variables is impossible. Do I need to encase the $x in something like $test".$x." ?

 

Thanks !

 

;D

Link to comment
https://forums.phpfreaks.com/topic/54522-is-it-possible-to-do-something-like-this/
Share on other sites

Actually, I don't think Obsidians way will work. Though I haven't tested, I think you would need something more like....

 

<?php

  $test1 = 'one';
  $test2 = 'two';
  $test3 = 'three';
  $test4 = 'four';

  for ($x=1; $x <=4; $x++) {
    echo ${'test'.$x};
  }  

?>

 

Actually, I don't think Obsidians way will work. Though I haven't tested, I think you would need something more like....

 

<?php

  $test1 = 'one';
  $test2 = 'two';
  $test3 = 'three';
  $test4 = 'four';

  for ($x=1; $x <=4; $x++) {
    echo ${'test'.$x};
  }  

?>

 

 

Thorpe, you're exactly right. My bad. That's what I was intending... it's just way to late tonight.

 

Sorry, guys. Thanks, Thorpe.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.