Preventing for loop from exceeding condition in PHP [duplicate] - php

This question already has answers here:
Can you 'exit' a loop in PHP?
(6 answers)
Closed 4 years ago.
How to prevent for loop from exceeding condition?
Loop below returns 25. I want it to return 20.
for ($i = 5; $sum < 23;)
{
echo $sum += $i;
}
This loop is just an example. There will be variables with any value in place of 5 and 23

You have to write your loop differently.
A for loop is designed to run until the conditions evaluates false. In your case, the loop at sum = 20 will still run, becuase sum < 23 evaluates to true. So if you want 20, simply write $sum < 20.
Or if I give it a second thought, you may want to do it like that:
<?PHP
$sum = 0;
for($i = 5; ($sum+$i) < 23;)
{
$sum += $i;
}
echo $sum;
?>

Related

how to break children loops only [duplicate]

This question already has answers here:
Can you 'exit' a loop in PHP?
(6 answers)
Closed 4 years ago.
I won't break a loop that is in another loop
these are my codes
for ($i = 0; $i < 20; $i++) {
// some codes
for ($i1 = 0; $i1 < 10; $i1++) {
if (a condition)
{
// i want break this loop not parent loop
}
}
// some codes
}
if I use break; parent loop will break too but I won't break only work for child loop
thanks for your answers
break only breaks out of the loop it's called in. If you want to break out of an outer control structure, you can use the optional integer argument to tell break the number of structures it should break out of (e.g., in this case, break 2 would break out of the outer loop).
I used this and its worked
for ($i = 0; $i < 20; $i++) {
// some codes
for ($i1 = 0; $i1 < 10; $i1++) {
if (a condition)
{
$i1=11;
}
}
// some codes
}

How does for loop works actually in php?

I'm a newbie to php and I've understood all other loops in php but the question is I cannot understand how the for loops works for ex:
Here is the code;
$a = 0;
$b = 0;
for ($i=0; $i < 5; $i++) {
$a += 10;
$b += 5;
}
echo("At the end of the loop a=$a and b=$b");
When I execute this script the value of a = 50 and b = 25!
Is it multiplying the a value with i's increment value? like 10 * 5 = 50.
You start with $i=0, then you do $a+10 and $b+5 as long as $i <5
$i=0, $a=10, $b=5
$i=1, $a=20, $b=10
$i=2, $a=30, $b=15
$i=3, $a=40, $b=20
$i=4, $a=50, $b=25
$i=5, now the loop stops because $i is no longer <5
Your loop runs five times. Each time through the loop you add 10 to the value of $a. Doing that five times gives you 50.
This is the way it works.
Let's say you have a no dollars. And I tell you that I will give you a dollar everytime you do 5 chores. However, I will only give you 5 dollars. At first you have no dollars and after one chore I give you 5 dollars. Now you have 5 dollars. You do another chore and I give you another 5, bringing you to ten. I have now given you two dollars. I give you another 5 - you have 15. I give you another - 20 - and one more; bringing you to 25 dollars. Now I have given you my limit of dollars, and our loop is complete.
In this story, my dollars are your $i value. Beginning at 0, working up to 5. Your chores are your $b values, which are added to every time.
Code example:
for ($dollars=0; $dollars < 5; $dollars++) {
$chores += 5;
}
+= is not increment its an assignment operator.
http://php.net/manual/en/language.operators.assignment.php
As I mentioned in the comments, and others in their answers. += will add the value to on the right to the value on the left. so in a loop its like this
step 0 $a = 5 ( 0+5)
step 1 $a = 10 (5[previous iteration exit value]+5 )
step 2 $a = 15 (10[previous iteration exit value] + 5)
so on....
Of note is you can also do -= *= etc. and .=or append all the same kind of operation.
The for statement is used when you know how many times you want to execute a statement or a block of statements.
Syntax:
for (initialization; condition; increment){
code to be executed;
}
The initializer is used to set the start value for the counter of the number of loop iterations. A variable may be declared here for this purpose and it is traditional to name it $i.
Example
The following example makes five iterations and changes the assigned value of two variables on each pass of the loop −
<html>
<body>
<?php
$a = 0;
$b = 0;
for( $i = 0; $i<5; $i++ ) {
$a += 10;
$b += 5;
}
echo ("At the end of the loop a = $a and b = $b" );
?>
</body>
</html>
This will produce the following result −
At the end of the loop a = 50 and b = 25
For loop is entry condition loop. It evaluate condition first, so the statement block associated with the loop won't run even once if the condition fails to meet
The statements inside this for loop block will run 5 times, the value of $i will be 0 to 4;
Imagine the loops are running separately.
$a=0;
$b=0;
for ($i = 0; $i < 5; $i++){
echo $a += 10;
echo '<br>';
}
And the output like this.
10
20
30
40
50
Now another Loop
for ($i=0; $i < 5; $i++) {
echo $b += 5;
echo '<br>';
}
And the output like this
5
10
15
20
25
In each iteration it adds 10 and 5 to previous number of iteration using the assignment operator x += y.

Incrementing numbers with double digits in php [duplicate]

This question already has answers here:
Formatting a number with leading zeros in PHP [duplicate]
(11 answers)
Closed 7 years ago.
I would like to Increment numbers with double digits if the number is less then 10
This is what i tried so far
$i = 1;
echo $i++;
results is 1,2,3,4,5,6 so on
Then i try adding a condition
$i = 1;
if ($i++<10){
echo "0".$i++;
}else{
echo $i++;
}
Work but skipping the numbers 2,4,6,8 so on.
Can anyone tell me the proper way to do this?
If the condition is only there for the leading zero you can do this much easier with this:
<?php
$i = 10;
printf("%02d", $i++);
?>
if you want prepend something to a string use:
echo str_pad($input, 2, "0", STR_PAD_LEFT); //see detailed information http://php.net/manual/en/function.str-pad.php
On the second fragment of code you are incrementing $i twice, that's why you get only even numbers.
Incrementing a number is one thing, rendering it using a specific format is another thing. Don't mix them.
Keep it simple:
// Increment $i
$i ++;
// Format it for display
if ($i < 10) {
$text = '0'.$i; // Prepend values smaller than 10 with a zero
} else {
$text = $i;
}
// Display it
echo($text);
<?php
$i = 1;
for($i=1;$i<15;){
if($i<10){
echo '0'.$i++."<br>";
}else{
echo $i++."<br>";
}
}
?>

How to work for loop increment value in PHP? [duplicate]

This question already has answers here:
Pre-incrementation vs. post-incrementation
(3 answers)
Closed 8 years ago.
I have checked for loop these two ways, but given same output:
1: Increment use ++$i
for($i=0; $i<10; ++$i){
echo $i;
}
2: Increment use $i++
for($i=0; $i<10; $i++){
echo $i;
}
These both code gave this output:
0 1 2 3 4 5 6 7 8 9
We learn ++$i mean pre increment, $i++ mean post increment,
why that post and pre increment not work? can someone please explain me? thank you.
Value of $i++ and ++$i will be same after evaluation.
As $i++ first evaluate value of $i then increment $i.
And
++$i first increment then evaluate value of $i.
Here in for loop, it follows step
initialization
test condition(if true then execute body/else exit)
increment/decrement
As again its working for a new line, either you use $i++ or ++$i it will be the same.
But if you use it in between the for loop you can see the difference.
Check Link for more details
for example
$i++; // Or ++$i;
echo $i;
It will give same value in both condition.
But if you use echo $i++; or echo ++$i then you will find difference.
Pre- and post- incrementing only make a difference when there are other operations happening in the same statement. For example:
$i = 0;
echo ++$i;
Will return 1, as opposed to this:
$i = 0;
echo $i++;
Which will return 0. In the latter case, the increment happens after the echo.
In your original example, the entire statement being executed is either ++$i or $i++. Either way, the order doesn't matter because nothing else is happening.

Echo out html set number of times - php [duplicate]

This question already has answers here:
make string of N characters
(5 answers)
Closed 2 years ago.
I'm creating quite a complex html <table> layout and at this early stage it quite time consuming for me to copy and paste each <tr> in order to generate dummy content.
My idea was to specify a dummy <tr> as a $var and then output that x number of times using a function as below:
$html = "<tr>//content</tr>";
function dummy_html($html, $times){
$i = 0;
for ($i <= $times) {
echo $html;
$i = $i++;
}
}
echo dummy_html($html, 5);
But this is returning a parse error on the for line any idea why that might be ?
PHP has a function already
echo str_repeat($html,5);
Your for loop is incorrect. It should be something like:
for( $i = 0; $i <= $times; $i++ ) {
echo $html;
}
Update
#Your Common Sense's solution is better: str_repeat (http://php.net/manual/en/function.str-repeat.php)
http://php.net/manual/en/control-structures.for.php
for should use the notation: for (set arguments, conditions, command to run at the end of the loop), therefor should be:
for($i = 0; $i <= $times; $i++)
Also, I would recommend using str_repeat (http://php.net/manual/en/function.str-repeat.php)

Categories