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
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};
  }  

?>

 

Link to comment
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};
  }  

?>

 

 

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

 

Sorry, guys. Thanks, Thorpe.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.