Dear all,

I'm new learner PHP. I beg help to everybody. Please check out the following code.

<?php 

$abc1="ABC1";
$abc2="ABC2";
$abc3="ABC3";

for($i=1; $i<=3; $i++)
    {
        $abc.=$i;
        echo "$".$abc;
    }
?>

Here, printed [ $abc1 $abc2 $abc3 ] but i want to print [ ABC1 ABC2 ABC3 ].

please advice me how can i solve this problem.

Thanks

Edited 3 Years Ago by Dani: Formatting fixed

Do you mean like:

for($i=1; $i<=3; $i++)
{
$abc="ABC";
$abc .=$i;
$abc .="  ";
echo $abc;
}

Outputs: ABC1 ABC2 ABC3

Do you mean like:

for($i=1; $i<=3; $i++)
{
$abc="ABC";
$abc .=$i;
$abc .="  ";
echo $abc;
}

Outputs: ABC1 ABC2 ABC3

Thanks a lot....... for your kind solution.

But ....... I have huge data with different content, e.g.

$abc1="qwe";
$abc2="XYZ";
$abc3="5sdf56";
$abc4="vcbvb";
$abc5="rty5";
$abc6="tbgfgh gfdh ";
$abc7="qdfg55";
$abc8="fgfd";
$abc9="fderrrfgfd";
...
....
........
$abc100="EFG";

Now i faced problem in that manner, Please advice me how can i print that content stroed in variable.

Thanks & best regards.
Monirul

This should do the trick:

<?php

	$abc1="ABC1";
	$abc2="ABC2";
	$abc3="ABC3";
	
	for($i=1; $i<=3; $i++)
	{
	$abc = ${'abc' . $i};
	echo $abc." ";
	}
	
?>

Many Many thanks.....................that i can't express to you.

Again thanks...............a lot.

Previously I solved this problem using arrya method by following code but i can't solve using for.

$abc[1]="ABC1"; 
$abc[2]="3435345";  
$abc[3]="A012"; 

for($i=1; $i<=3; $i++)  {
     $abc = $abc[ $i];
    echo $abc." ";  
}

Thanks all the best.

Edited 3 Years Ago by Dani: Formatting fixed

not sure if it would make a difference but get rid of the space after "[" and maybe change $abc = to $SOMEOTHERNAME =

I think this would make things easier for both you and the server. You have to use foreach:

<?php
$abc[1]="ABC1";
$abc[2]="3435345";
$abc[3]="A012";

foreach ($abc as $val)
{
print $val . " ";
}
?>

Previously I solved this problem using arrya method by following code but i can't solve using for .

$abc[1]="ABC1";
$abc[2]="3435345";
$abc[3]="A012";

for($i=1; $i<=3; $i++) {
$abc = $abc[ $i];
echo $abc." ";
}

Thanks all the best.

you were close here. Just a small change:

for($i=1; $i<=3; $i++) 
{
     echo $abc[$i];
}

or for unknown array size, use:

for($i=1; $i<count($abc); $i++) 
{
     echo $abc[$i];
}

In your code, you were assigning the current element of the array to the array itself, which resulted in some strange output.

As buddylee17 stated, the foreach works well also.

Keep in mind that arrays in PHP start with the index of zero. Your abc array actually has an element 0 that you are not using, which you can see with the nice debug function:

print_r($abc);

Hi,

You can simply use

where $n = maxno

for($i=1;$i<=$n;$i++)
{
       $aa = $abc.$i;
        echo $aa;
}

This should solve your problem

Hi,

You can simply use

where $n = maxno

for($i=1;$i<=$n;$i++)
{
       $aa = $abc.$i;
        echo $aa;
}

This should solve your problem

actually, this will just print the value of $abc (nothing) with the value of $i appended to the end. If you have variables $abc1, $abc2, etc. , you will not get the value of those variables, you'll just get the value of $i. The code above would output 1..2..3...etc. until $n+1 was reached.

I think this would make things easier for both you and the server. You have to use foreach:

<?php
$abc[1]="ABC1";
$abc[2]="3435345";
$abc[3]="A012";

foreach ($abc as $val)
{
print $val . " ";
}
?>

=================================
Thanks dear. I'm new learner of PHP. Now i feel n hope that you can make easy to me by advising and solving my ideas.

Thanks ......a lot.
Wish you all the very best.

=================================
Thanks dear. I'm new learner of PHP. Now i feel n hope that you can make easy to me by advising and solving my ideas.

Thanks ......a lot.
Wish you all the very best.

Hi,

I think this is what you are looking for.

<?php 
$abc1 = "aaaa";
$abc2 = "bbbb";
$abc3 = "cccc";

for($i=1;$i<=3;$i++)
{
   $c = "$"."abc".$i;	
   eval("\$c = \"$c\";");
   echo $c;	

}
?>
This article has been dead for over six months. Start a new discussion instead.