0

Not sure if the Title is the correct title but I'm not sure how to call what I'm trying to accomplish. On the surface it looks fairly simple...

I wrote the following code:

$var1 = "55";
$var2 = "66";
$var3 = "22";
$var4 = "22";
$x = 1;
$result = "null";

while($x < 5) {
$result = "$" . "var" . "$x";
echo $result;
echo " | ";
$x = $x +1;
}

Was hoping to get: 55 | 66 | 22 |22

Instead I'm getting: $var1 | $var2 | $var3 | $var4 |

I tried many things but unable to find a working solution, any help would be much approached.

Thank you

3
  • Just use an array. Commented Dec 21, 2016 at 20:10
  • you are building a string called $var1 when you mean to evaluate that string to the value of $var1. I guess you don't know what variable names you will handle beforehand. However, using eval on an unknown input stream is a bad approach. Use arrays and iterate over them as @PeeHaa said. Commented Dec 21, 2016 at 20:14
  • Array is a great idea but in this case I really would like to find a way to make it work without an array. The code above is a simplified version of what I'm trying to do which does contain four different arrays. I figured it would be easier to post this strip down code that emulate the problem. Commented Dec 21, 2016 at 20:20

3 Answers 3

5

See this statement here,

$result = "$" . "var" . "$x";

$result variable is actually a string. To get it's value, use variable variables. Also, with your approach you'll end up getting an extra | at the end. So to achieve your desired result, change your while() loop in the following way,

while (true) { 
    $result = "var" . $x; 
    echo $$result;
    if($x++ == 4) break;
    echo " | ";
}

Here's a demo


Furthermore, do look into @freginold's solution as well, which I slightly modified to suit your needs. Initialize $x with 5 and change your while() loop in the following way,

$output = "";
while (--$x) { 
    $result = "var" . $x; 
    $output = $$result . " | " . $output; 
}
echo rtrim($output, " | "); 

In this solution, while coming out of the loop you'll end up getting an extra | at the end, which you need to trim down using rtrim() function.

Here's a demo

Sign up to request clarification or add additional context in comments.

5 Comments

This is the best solution so far. You could even condense your code further by including $output=""; in your initializations and then changing your while loop to this: while (--$x) { $result = "var" . "$x"; $output = $$result . " | " . $output; } with echo $output; at the end.
@freginold Good attempt,;-) but there's one flaw in your code, which I noticed now. In your method the elements will be printed in reverse order, given that we initialize $x with 5.
Of course you would also have to change $x = 1; to $x = 5;
they are processed in reverse order, but printed in correct order, via adding the new values to the beginning of the output string :)
@freginold Ah, you're right, I didn't think it through. Nice solution! I'll add it in my answer as well. Cheers! :-)
1

Another solution without a while loop:

$var1 = "55";
$var2 = "66";
$var3 = "22";
$var4 = "22";

$vars = array($var1, $var2, $var3, $var4);
echo implode(' | ', $vars);

Comments

0

Would an array work better?

<?php 

  $vars = array( 
    'var1' => 55, 
    'var2' => 66, 
    'var3' => 22, 
    'var4' => 22
    'x'    => 1
  );

  foreach($vars as $key => $value){
    print $value;
  }

?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.