So I have a list with variables (auto-generated), something like:
$won3 = 1;
$time3 = 4;
$won6 = 0;
$time6 = 5;
$won4 = 0;
$time4 = 5;
(...)
but with many more variables. Now I want to make a table with all the variables, so I used a for-loop, but $won1 has to be the first in the table, then $won2 etc...
But how can I recall this $won1 in a for-loop? I tried:
for ($X = 0, $X < $Y, $X++){
echo '$won'.$X;
}
but this does not do the job. Anyone knows how I can solve this?
Thanks in advance.
First... In for loops you can't use ,
for ($X = 0, $X < $Y, $X++){
Try this:
for ($X = 0; $X < $Y; $X++){
.
And you have to choices...
for ($X = 0; $X < $Y; $X++){
$var = 'won' . $X;
echo $$var;
echo ${'won' . $X};
}
Related
I am trying to write a code that is capturing number 3. using while loop as the condition, so that the for statement will be the evaluation condition of $x, of the while loop statement, and by the same time, using if statement to evaluate the value of $x=3 and so it can echo 'three..'; . please enlighten me. thank you
<?php
$x = 0;
$y = 5;
while ($x <= $y) {
for ($z = 0; $z < 3; $z++) {
if ($x = 3) {
echo 'three..' . "\n";
}
}
$y++;
}
In while loop, you should increment x variable.
If you increment y variable, it will always "true". So it will be endless loop.
<?php
$x = 0;
$y = 5;
while ($x <= $y) {
// code
$x++;
}
<?php
$x = 0;
$y = 5;
while ($x <= $y) {
for ($z = 0; $z < 3; $z++) {
echo $z;
}
if ($x == 3) {
echo 'three..' . "\n";
}
$x++;
}
I need x (0) to increase with a value for 10 times, then I need to add a value to y and let x increase again from 0.
Basically I'm creating a grid.
Right now I manually do one "row", loop 10 times through that, then manually change y, loop again, repeat. I want to automatize this.
$int = 0;
$x = 0;
$y = 0;
$z = 0;
while($int < 10) {
echo 'posX="'. $x .'" posY="'. $y .'" posZ="'. $z .'<br>';
$int++;
$x+=20;
}
What I'd manually do now is change the value of y to 20, then let the loop run again, I have to manually change this 10 times.
Any suggestions?
You could try something like this. I've used variables $x_inc and $y_inc to define how much to increment $x and $y in each pass of the loop:
$x = $y = $z = 0;
$x_inc = 20;
$y_inc = 20;
for ($i = 0; $i < 10; $i++) {
for ($j = 0; $j < 10; $j++) {
echo 'posX="'. $x .'" posY="'. $y .'" posZ="'. $z .'<br>';
$x += $x_inc;
}
$y += $y_inc;
$x = 0;
}
Demo on 3v4l.org
I would use the modulus personally it always feels like the best way to handle these sort of "timed" increments. You can read about the modulo operator here. This also means you dont have to have nested loops
$int = 11;
$x = 0;
$y = 0;
$z = 0;
while($int < 110) {
echo '"posX="'. $x .'" posY="'. $y .'" posZ="'. $z .'"<br />"';
$int ++; //you probably want to do this last unless you need int to increment before we evaluate it
$x+=20;
if(($int % 10) == 0) { //basically if int is a multiple of 10 you want to add to y and reset x
$y += 10; //your value
$x = 0; // reset x to 0 for the next 10 iterations
}
}
====== OR =======
$int = 0;
$x = 0;
$y = 0;
$z = 0;
while($int < 110) {
echo '"posX="'. $x .'" posY="'. $y .'" posZ="'. $z .'"<br />"';
$int ++; //you probably want to do this last unless you need int to increment before we evaluate it
$x+=20;
if(($int / 10) == 1) { //basically if int is a multiple of 10 you want to add to y and reset x
$y += 10; //your value
$x = 0; // reset x to 0 for the next 10 iterations
}
}
Hello I would like to know if there is away and if this is valid programming to increment the value of array inside echo. Example code is:
for ($x = 1; $x <= $number; $x++) {
echo"<td>".(round(($row['day_1'])/3600))."</td>";
}
Where I would like to display $row['day_1'], $row['day_2'], $row['day_3'] etc. if there is no way this to be achieved, is there a way to increment the same predefined array with the results of $row like
$time_01[0] = $row['day_1'];
$time_01[1] = $row['day_2'];
So after that to loop through the time_01[] array ?
Least amount of changes: Use the $x you already have:
for ($x = 1; $x <= $number; $x++) {
echo"<td>".(round(($row["day_$x"])/3600))."</td>";
}
or
for ($x = 1; $x <= $number; $x++) {
echo"<td>".(round(($row['day_' . $x])/3600))."</td>";
}
or if you want to put it in another array first, for whatever reason:
for ($x = 1; $x <= $number; $x++) {
$time_01[] = $row['day_' . $x];
}
This really is basic stuff by the way. I do recommend that you follow some basic tutorials about variables, expressions, string concatenation and more.
echo $xml->SLOT1->Effect;
echo $xml->SLOT2->Effect;
echo $xml->SLOT3->Effect;
Is there a way to simplify this by using a for loop? I tried this but it echos nothing:
for ($x = 1; $x <= 3; $x++) {
echo $xml->SLOT[$x]->Effect;
}
You can use
$xml->{"SLOT".$x}->Effect;
for ($x = 1; $x <= 3; $x++) {
echo $xml->{SLOT.$x}->Effect;
}
I tried and tried and tried to get this code to work and kept coming up with zilch. So I decided to try it using "for loops" instead and it worked first try. Could somebody tell me why this code is no good?
<?php
$x = $y = 10;
while ($x < 100) {
while ($y < 100) {
$num = $x * $y;
$numstr = strval($num);
if ($numstr == strrev($numstr)) {
$pals[] = $numstr;
}
$y++;
}
$x++;
}
?>
you should reset y=10 inside the first while.
$x = 10;
while ($x < 100) {
$y = 10;
while ($y < 100) {
$num = $x * $y;
$numstr = strval($num);
if ($numstr == strrev($numstr)) {
$pals[] = $numstr;
}
$y++;
}
$x++;
}
You need to reset y before the y loop begins.
While($x < 100){
$y=10; //... rest of code
For loops which loop over an integer that is incremented I would prefer the for-loop:
for ($x=0; $x < 100; $x++) {
for ($y=10; $y<100; $y++) {
$num = $x * $y;
$numstr = strval($num);
if ($numstr == strrev($numstr)) {
$pals[] = $numstr;
}
}
}
IMHO this is much more readable and it's shorter, too.