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++;
}
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.
I want to show my output 0 to 50 after two digits interval but it's produce an infinity loop and therefore, crash my browser. How can I solve my issue?
<?php
for ($x = 0; $x < 50; $x+2) {
echo "The number is: $x <br>";
}
?>
You need to set $x:
<?php
for ($x = 0; $x < 50; $x = $x+2) {
echo "The number is: $x <br>";
}
?>
you could also use $x+=2 as a shorthand instead of $x = $x+2
Change the incremet to $x+=2 like:`
for($x=0; $x<50; $x+=2){
//do any thing... Echo
}
Let's try with do while loop.
$x = 0;
do {
echo "The number is: $x <br />";
$x = $x + 2;
} while($x < 50);
How would I group 5 numbers in an array into each line? I've tried this code below but it results in something I'm not expecting it to be.
$arrayCount = count($result_data);
for ($x = 0; $x < $arrayCount; $x++)
{
for ($i=0; $i<5; $i++)
{
echo ($result_data[$i]);
}
echo ("\n");
}
Result:
239298246244268
239298246244268
239298246244268
239298246244268
This loop keep repeating the first 5 numbers in my array. How do I make it to loop for every 5 numbers instead in my whole array of numbers? Thank you!
$x should be your index for the echo. Try this instead:
<?php
$result_data = array(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20);
for ($x = 0; $x < count($result_data); $x++)
{
echo ($result_data[$x]);
if(($x+1)%5==0)
{
echo ("\n");
}
}
use this $result_data[$x]
try this
$arrayCount = count($result_data);
for ($x = 0; $x < $arrayCount; $x++)
{
if($x%5==0)
{
echo ("\n");
}
echo ($result_data[$x]);
}
Don't know much about your $result_data Array, but probably it should go like this:
$arrayCount = count($result_data);
for ($x = 0; $x < $arrayCount; $x++)
{
for ($i=0; $i<5; $i++)
{
echo ($result_data[$x][$i]);
}
echo ("\n");
}
I think you want to do something like this
$i = 0;
foreach($result_data as $result) {
echo $result;
if($i < 5) {
echo ",";
} else {
echo "<br/>\n";
$i = 0;
}
$i++;
}
Something like this?
$chunks = array_chunk($result_data, 5);
foreach($chunks as $chunk) {
echo implode('', $chunk);
echo "\n";
}
See http://uk3.php.net/manual/en/function.array-chunk.php
Try this several line code:
$valuesDelimiter = ', ';
$lineDelimiter = "\n";
$input_array = array(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20);
$slited_array = array_chunk($input_array, 5);
array_walk($slited_array, function(&$arr) {$arr = implode($valuesDelimiter, $arr);});
$result = implode($lineDelimiter, $slited_array);
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.