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;
}
This is a better practice:
<?php
$str = array("abhishek","ashish");
for($i=0; $i <= 2; $i++)
{
echo $str[$i].'<br>';
}
?>
Do this:
$str1="abhishek";
$str2="ashish";
for($i=1;$i<=2;$i++){
echo ${"str".$i};
}
i need to call similar variable on shortest way.
$test = new stdClass();
$test->var0=0;
$test->var1=1;
$test->var2=2;
And now i need to echo all 3 variables in for cycle, not like this:
echo $test->var0;
echo $test->var1;
echo $test->var2;
Try this -
for($i = 0; $i < 3; $i++) {
echo $test->{'var' . $i};
}
Okay so i am trying to retrieve the object value iterating through a for loop.
Manually it looks like this:
echo $game->stats->item0;
echo $game->stats->item1;
...
I want to do it something like this:
for($i = 0; $i < 6; $i++) {
echo $game->stats->item.$i;
}
The above however just returns the value of $i. How can i return the actual object value?
Thanks
Basically, your statement should be like this:
echo $game->stats->{'item'.$i}
Regards,
Instead of item.$i, use {"item$i"}.
for($i = 0; $i < 6; $i++) {
echo $game->stats->{"item$i"};
}
Another way you can do it is to set a variable to be equal to 'item' . $i.
for($i = 0; $i < 6; $i++) {
$item = 'item' . $i;
echo $game->stats->$item;
}
<?php
$td = date("d");
for ($i = 1; $i <= $td; $i++)
{
$num18 = count($this->numbermonth18);
echo "['1'," . $num18 . "],";
}
The above code will display the output like, ['1',12],['1',12],['1',12],['1',12],['1',12],['1',12],['1',12],['1',12],['1',12],['1',12],['1',12],['1',12],['1',12],['1',12],['1',12],['1',12],['1',12],['1',12],['1',12],['1',12],['1',12],['1',12],['1',12],['1',12],['1',12],['1',12],['1',12],['1',12],['1',12],['1',12],.
I need to replace the 18 with $i value in the above code. How can I use $i instead of 18 in the for loop to display all the values of $this->numbermonth1, $this->numbermonth2, $this->numbermonth3 etc. and print the array?
You could do with:
for($i=1;$i<=$td;$i++)
{
$num=count($this->{'numbermonth'.$i});
echo "['1',".$num."],";
}
Here is my code
for ($i=0; $i<$Percentile["Parameter_length"]; $i++)
{
echo "Eqt_Param".$i."=".$Percentile["Eqt_Param".$i];
}
The above code will display
Eqt_Param0=2.00
Eqt_Param1=3.00
Eqt_Param2=1.00
Eqt_Param3=5.00
If I put echo() outside the for loop, I need the same result. Please help me to fix this...
How about this? Simply concatenating the result of the foreach into a variable which can be echoed.
$output = "";
for ($i = 0; $i < $Percentile["Parameter_length"]; $i++)
{
$output .= "Eqt_Param" . $i . "=" . $Percentile["Eqt_Param" . $i];
}
echo $output;
You need to store your values in a viarable that exists outside of the scope of the for
like:
$accumulatedString = '';
for ($i=0; $i<$Percentile["Parameter_length"]; $i++) {
echo "Eqt_Param".$i."=".$Percentile["Eqt_Param".$i];
$accumulatedString .= "Eqt_Param".$i."=".$Percentile["Eqt_Param".$i];
}
echo $accumulatedString;
Thats if you want it all as one string.