Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I do have variables like

<?php
$num1 = 'txt1';
$num2 = 'txt2';
$num3 = 'txt3';   
...

and I do have a loop

for ($i=1; $i<100; $i++){  
   echo 'This is textNr'.$i.':'   .$num.$i  ;
}

I need to produce a result like:

This is textNr1: txt1


Of course with this code i would get something like:

This is textNr1: 'undefined'


,because in $num.$i the $num is not defined;

so $num.$i should be parsed once and become $num1 and then $num1 should be parsed a second time like:

 echo 'This is textNr'.$i.':'   .$num1  ;

Does anybody know how to handle this, or could give me a link about this problem and maybe the technical term of this problem (solved :) variable variables)?

Please feel free to edit my question, into something which will fit this case better!

share|improve this question
Why not just use an array and loop that? – elclanrs 14 hours ago
I don't get what you're trying to do inside the loop. – Lance 14 hours ago
@Lance he thought using $num.$i can get $num1 if $i=1 – pinkpanther 14 hours ago
He's trying to loop the variables with $i like $num.$i == $num1 etc. But this is the wrong approach... – elclanrs 14 hours ago
Ohhh. I see. Well, for starters define $num inside the loop. – Lance 14 hours ago
show 1 more comment

6 Answers

up vote 1 down vote accepted

Using an array (Antoine's answer) is better practice. But if you want/need to use your solution

for ($i=1; $i<100; $i++){  
   echo 'This is textNr'.$i.':' . ${"num$i"}  ;
}

PHP does first evaluate "num$i" and use that result to find a variable with that name,
thanks to ${ }.

Note that you can use a deeper level of variable names

$a = "b";
$b = "c";
${${$a}}  = 7; 
echo $c; // echoes 7

Via an array: you could also declare an array

$array = ('text1', 'text2', 'text3', ... , 'textN');
echo count ( $array ); // echoes N, size of array

To display its items

// Loop on array
foreach ($array as $text) {
  echo "$text\n";
}

displays text1, text2, ... textN (one per line)

You could also use a for loop

$n = count($array);
for($i=0 ; $i<$n ; $i++) echo $array[$i] . "\n";

that produces the same output.

share|improve this answer
thx for the detailed answer – AddingColor 27 mins ago

Try this:

<?php
$nums = array();
  $nums[0] = 'txt1';
  $nums[1] = 'txt2';

  for ($i=0; $i<count($nums); $i++){  
    echo 'This is textNr'.$i.':'   .$nums[$i]  ;
  }
?>

Is an improved version of the answer you have choosed. It will only show the values you have set to the array.

share|improve this answer

You can use an array , like this:

$nums = array('txt1','txt2','txt3');

for ($i=0; $i<sizeof($nums); $i++){  
    echo 'This is textNr'.$i.':'   .$nums[$i]  ;
}

The upper bound of array should be the size of the array, otherwise you may get empty values at $nums[x]

share|improve this answer
thx for the sizeof($nums) this comes quite handy , know this from javascript as array.length – AddingColor 13 hours ago
Sure, no problem :) – Jacob Amerz 13 hours ago

The php term you look for is "variable variable". php its one of very few languages that support it, and for a good reason - the approach smells wrong.

share|improve this answer

php variable can be constructed with curly brackets like example below:

echo 'This is textNr'.$i.':'   .${'num'.$i}  . "\r\n" ;

you can put anything that returnrs string between brackets

share|improve this answer

You should do something like:

  $nums = array();
  $nums[0] = 'txt1';
  $nums[1] = 'txt2';

  for ($i=0; $i<100; $i++){  
    echo 'This is textNr'.$i.':'   .$nums[$i]  ;
  }

Or start the index at 1 in the array, if you want to start it at one in the loop,

share|improve this answer
Array seems to be the magic here. Thx, this should work. I'm quite new to PHP, but I guess arrays are often quite handy. – AddingColor 14 hours ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.