how to fix php for loop output displaying 1? - php

Here is my php code given below when I run it in my browser its display 1 continuously. My question is why its show like this? After execution its displays a fatal error : maximum execution time is exceeded. what is that?
<?php
for ($i=1; $i<=5; $i+1) {
echo $i." ";
}
?>
Give me proper answer. Make me sure that what is the execution time of php code?
TIA

$i+1 does not increment the value of $i. It only adds 1 to what is in $i but it does not assign it back to $i. Your loop does this:
$i = 1
while ($i<=5) {
echo $i." ";
$i+1;
}
$i+1 on it's own doesn't do anything.
You need something like $i = $i + 1. Or for short $i += 1. Or even shorter and better: $i++.
for ($i=1; $i<=5; $i++) {
echo $i . " ";
}

It is because of $i+1 in for loop. This is basically an expression and it produces a result but you never assign this result to $i. Therefore you would rather do something like $i = $i + 1 or, in real life, use incrementation $i++. So the final code will looks like:
for ($i = 1; $i <= 5; $i++) {
echo $i." ";
}

use $i++ not $i+1, $i++ is $i=$i+1

Change your code to
<?php
for ($i=1; $i<=5; $i++) {
echo $i." ";
}
?>
because first you must set value for $i

You need to do
<?php
for( $i = 1; $i <= 5; $i++) {
echo $i." ";
}
?>
Now you just say 1 + 1 but you don't assign it to anything. You could use $i = $i + 1 but it's the same as $i++

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

in a for loop, every turn you need to increase value of $i. but you forgot to increase value of $i. You wrote $i +1 but it needs to be assigned with new value of $i.
In short, you should change $i +1 to $i = $i +1 or $i ++
the right code:
<?php
for ($i=1; $i<=5; $i = $i+1) {
echo $i." ";
}
?>

You are not changing the value of $i in the loop. Either $i =$i +1 or $i++ instead of $i + 1 will do.

You've problem in printing the line. It should look like
<?php
for ($i=1; $i<=5; $i++) {
echo $i;
}
?>

Related

How I can make pattern Pyramid number php like this

How I can make pyramid number like this
1
212
32123
Code:
<?php
for($i=2; $i<=5; $i++){
for($x=$i; $x>=2; $x--){
echo "$x ";
}
echo "<br/>";
}
for($i=1; $i<=5; $i++){
for($x=1; $x<=$i; $x++){
echo "$x &nbsp";
}
echo "<br/>";
}
You seem to printing numbers in descending order, adding a new line and then go to print numbers in ascending order in another line. This makes it too late to do so since you already lost the current line.
Instead, for every row, go from row number till 1 and print numbers and repeat the same in the same line from 2 till row number and then add a new line like below:
<?php
function printPyramid($rows){
for($i = 1; $i <= $rows; ++$i){
echo str_repeat(" ", $rows - $i);
for($j = $i; $j >= 1; --$j){
echo $j;
}
for($j = 2; $j <= $i; ++$j){
echo $j;
}
echo PHP_EOL; // or echo "<br/>";
}
}
printPyramid(7);
Online Demo (view the output in HTML format by clicking on the eye icon)

Is my second for loop able to iterate using the increment from my first loop?

What I want is the first loop iterating from 1 to 4 and the second loop from 5 to 6.
Here is my code:
<?php
for ($i = 1 ; $i <= 4 ; $i++)
{
echo $i . "<br>";
}
?>
<hr>
<?php
for ($i = 1 ; $i <= 2 ; $i++)
{
echo $i . "<br>";
}
?>
The loops you've given are:
1st loop: from 1 to 4
2nd loop: from 1 to 2
First loop is ok, but seconds needs to be modified. Use $i<=6 and don't initialize $i variable.
This will give you:
1st loop: from 1 to 4
2nd loop: from (value that 1st loop have ended)+1 to 6, so (4+1) to 6, 5 to 6
<?php
$i = 0; // be sure 'i' is visible in both loops
for ($i=1; $i<=4; $i++) // form 1 to 4
{
echo $i . "<br>";
}
?>
<hr>
<?php
$i++; // start from 5, not 4
for (; $i<=6; $i++) // from the previous value to 6
{
echo $i . "<br>";
}
?>
Your problem
The second for loop resets your $i variable to 1:
for ($i = 1 ; $i <= 2 ; $i++)
Solution
You can use a while loop instead of your second for loop:
<?php
for ($i = 1; $i <= 4; $i++)
{
echo $i . "<br>";
}
?>
<hr>
<?php
while ($i <= 6) // `<= 6` instead of `<= 2`, since we keep $1's value
{
echo $i . "<br>";
$i++;
}
?>
Rather than using two loops for this, why not just output the <hr> tag at the appropriate point within the same one? If you carry on with adding extra loops, first of all you'll run into confusing problems like this about (re-)initialising variables, and you'll also quickly end up with a lot of unnecessary duplicated code.
You can use the PHP modulo operator (%) to output the <hr> tag after every fourth element, which will both reduce the complexity and be a lot more extensible if you later add more elements:
for ($i=1; $i<=6; $i++) {
echo $i . "<br>";
if ($i % 4 === 0) {
echo "<hr>";
}
}
See https://eval.in/976102

PHP loop X amount of times

I have a string called $columns which dynamically gets a value from 1 to 7. I want to create a loop of <td></td> for however many times the value of $columns is. Any idea how I can do this?
for ($k = 0 ; $k < $columns; $k++){ echo '<td></td>'; }
Here's a more readable way to achieve this:
foreach(range(1,$columns) as $index) {
//do your magic here
}
If you just need to use number of repeat count:
for ($i = 0; $i < 5; $i++){
// code to repeat here
}
just repeat $n times? ... if dont mind that $n goes backwards...
the advantage is that you can see/config "times" at the beginning
$n = 5;
while (--$n >= 0)
{
// do something, remember that $n goes backwards;
}
I like this way:
while( $i++ < $columns ) echo $i;
Just bear in mind if $columns is 5, this will run 5 times (not 4).
Edit: There seems to be some confusion around the initial state of $i here. You are welcome to initialise $i=0 beforehand if you wish. This is not required however as PHP is a very helpful engine and will do it for you automatically (tho, it will throw a notice if you happen to have those enabled).
There is a str_repeat() function in PHP, which repeats a string a number of times. The solution for your problem would be:
str_repeat( '<td></td>', $columns );
If $columns is a string you can cast to int and use a simple for loop
for ($i=1; $i<(int)$columns; $i++) {
echo '<td></td>';
}
A for loop will work:
for ($i = 0; $i < $columns; $i++) {
...
}
You can run it through a for loop easily to achieve this
$myData = array('val1', 'val2', ...);
for( $i = 0; $i < intval($columns); $i++)
{
echo "<td>" . $myData[$i] . "</td>";
}
Why use logic at all, don't waste those CPU cycles!
<td colspan="<?php echo $columns; ?>"></td>

php itertation loop in hidden input not iterating

I've got one nagging little bug in this script. I'm going through my cart items and passing them into hidden inputs. The cart_id ($obj->id) is working fine into the value="" but my iteration loop that gives each value a unique name="" (cart_id_1, cart_id_2 etc) is NOT iterating.
<?php
$pass_cart_q = "SELECT c.id FROM carts AS c WHERE c.user_session_id='$sid'";
$result = $mysqli->query($pass_cart_q);
$i = 1;
while ($obj = $result->fetch_object()) {
echo "<input type=\"hidden\" name=\"cart_id_".$i."\" value=\" .$obj->id. \"><br>";
$i = $i++;
}
mysqli_close();?>
Each name field is coming through as cart_id_1
$i=$i++;
That's the problem just do:
$i++
Please replace $i = $i++; with just $i++.
$i = 1;
$i = $i++;
echo $i, "\n"; // 1
$i = 1;
$i = ++$i;
echo $i, "\n"; // 2
$i = 1;
$i++;
echo $i, "\n"; // 2
$i = 1;
++$i;
echo $i, "\n"; // 2
What $i = $i++ will cause it literally this: "make $i equal to $i and then increase it by one", but the $i will still remain the same. To solve this, simply replace $i = $i++; with $i++.
Manual Entry
you are assigning the incremented value to $i variable. and hence it is not able to iterate. instead you should remove that assignment variable $i and it should only be $i++

Simple PHP counter - how to print numbers with loop?

How can I print out the numbers from 0 to 20 with a loop in PHP?
I hope some one can help.
Thanks!
You want a for loop:
http://php.net/manual/en/control-structures.for.php
Use a for loop:
for ($i = 0; $i <= 20; $i++)
{
echo $i . "\n";
}

Categories