0

I want to print the values of variable but it prints 12 only

$str1="abhishek";
$str2="ashish";
for($i=1;$i<=2;$i++)
{
echo $str.$i;
}
Abhishek Mishra
  • 340
  • 4
  • 14
  • 2
    Possible duplicate of [Dynamic variable names in PHP](https://stackoverflow.com/questions/9257505/dynamic-variable-names-in-php) – bugwheels94 Jul 04 '17 at 18:01
  • You're concatenating `$str` with `$i`. You really are only echoing `$i` (`1`, then `2`) and you should be getting undefined variable `$str` notices. – chris85 Jul 04 '17 at 18:04

2 Answers2

4

This is a better practice:

<?php 
$str = array("abhishek","ashish");

for($i=0; $i <= 2; $i++)
{
echo $str[$i].'<br>';
}
?>
Lekens
  • 1,823
  • 17
  • 31
1

Do this:

$str1="abhishek";
$str2="ashish";
for($i=1;$i<=2;$i++){
    echo ${"str".$i};
}
Rajdeep Paul
  • 16,887
  • 3
  • 18
  • 37