I am using PHP generator and can't explain this behavior.
This is the code I tried
<!-- language: PHP -->
<?php
function myfun($num1, $num2, $ctr = 1) {
for ($i = $num1; $i <= $num2; $i =+ $ctr) {
yield $i;
}
}
echo 'Odd numbers: ';
foreach(myfun(1, 7, 2) as $num) {echo $num;};
?>
Can someone explain me this behavior using PHP yield, entering a infinite loop?
result: Odd numbers: 122222222222222222222222222222222...............
Note: $i += $ctr works as expected
result: Odd numbers: 1357
The problem lies in the =+ operation, you probably meant to type +=, which would do the trick:
<?php
function myfun($num1, $num2, $ctr = 1) {
for ($i = $num1; $i <= $num2; $i += $ctr) {
yield $i;
}
}
echo 'Odd numbers: ';
foreach(myfun(1, 7, 2) as $num) {echo $num;};
Result: Odd numbers: 1357
$i =+ $ctr
=+ is not an operator. This will essentially do $i = $ctr.
The first time the loop occurs $i is set to $ctr, in this case this is 2. After this it is continually set to 2 and never goes higher. Hence the infinite loop. Use += instead.
Related
what I am trying to achieve here is to be able to loop from 0 to 100 (100, 98, 96, 94 ...) but has to stop at 0. What is doing right now is it passes 0 and -2 -4 which crashes the server. What am I doing wrong?
for ($i = 100; $i <= 100; $i--){
echo $i--;
echo "<br>";
}
Maybe a little explanation would be useful.
The middle part of the for loop $i <= 100 is what makes it infinite. That expression is checked before each iteration of the loop, and the loop will continue as long as that expression evaluates to true.
Since you set $ito 100 in the first section of the loop, and you're doing nothing except making it smaller, it will always be <= 100, forever.
The loop will work fine just the way you have it written if you change the continuation condition.
for ($i = 100; $i >= 0; $i--){
echo $i--;
echo "<br>";
}
That way it will continue until $i is reduced to less than zero, then $i >= 0 will be false, and the loop will end.
The third argument in for loop is what will be executed at the end of the loop. So:
for ($i = 100; $i >= 0; $i -= 2){
echo "$i<br>";
}
Will do the trick
As you can read here https://secure.php.net/manual/en/control-structures.for.php
At the end of each iteration, expr3 is evaluated (executed).
Alternatively:
<?php
foreach(range(100, 0, -2) as $n) {
echo $n;
}
for ($i = 100; $i >= 0; $i-=1){
echo $i--;
echo "<br>";
}
I figured it out somehow. Has been studying JavaScript for a year; Loop is still confusing to me
Recently, my exams got over. My last exam was based on PHP. I got the following question for my exam:
"Convert the following script using for loop without affecting the output:-"
<?php
//Convert into for loop
$x = 0;
$count = 10;
do
{
echo ($count. "<BR>");
$count = $count - 2;
$x = $x + $count;
}
while($count < 1)
echo ($x);
?>
Please help me as my computer sir is out of station and I am really puzzled by it.
Well, If I understand well, You have to use "for" loop, instead of "do...while", but the printed text must not change.
Try:
$count = 10;
$x = 0;
$firstRun = true;
for(; $count > 1 || $firstRun;) {
$firstRun = false;
echo ($count . "<BR>");
$count -= 2;
$x = $x + $count;
}
echo ($x);
By the way loop is unnecessary, because $count will be greater than 1 after the first loop, so the while will get false.
EDIT
$firstRun to avoid infiniteLoop
$count in loop
EDIT
Fixed code for new requirement
Removed unnecessary code
Hmmm I don't know if your teacher wanted to own you... but the do{} will execute only once since $count is never < 1.
The output of your teacher's code is:
10
8
I presume there was a mistake in the code and the while would be while($count > 1) which would make more sense (since it's weird to ask for a loop to output only 10 8) and would result in this output:
10
8
6
4
2
20
Then a good for() loop would have been :
$x = 0;
$count = 10;
for($i = $count; $i > 1; $i -= 2)
{
$count -= 2;
echo $i . "<br>";
$x += $count;
}
echo $x;
Which will output the same values.
If you can, ask your teacher for this, and comment the answer ^^ ahahah
I was coding PHP when suddenly Im being confused with variable scopes.
If I do the loop like this ...
function foo()
{
$ctr = 0;
for ($i = 0 ; $i > 8 ; $i++)
{
$ctr = $ctr + 1;
}
return $ctr;
}
$ctr returns a 0.
But if I do the loop like this ...
function foo()
{
$collections = //This has 7 counts;
$ctr = 0;
foreach ($collections as $collection)
{
$ctr = $ctr + 1;
}
return $ctr;
}
CTR IS RETURNING 7!
So what's is the problem in the first loop?
The for loop you are trying to do seems to be a bit wrong.
for ($i = 0 ; $i > 8 ; $i++)
^^^^^^
Means, Set $i to 0. For as long as $i is bigger than 8, do this loop then increment.
Since $i is set to 0, the condition is never met, so the loop is never executed.
Change $i > 8 to $i < 8.
Your loop condition:
for ($i = 0 ; $i > 8 ; $i++)
^^^^^^
since the loop starts at 0, you have 0 > 8, which is false, and your loop terminates immediately. Remember, loops terminate when that 2nd argument becomes false. it has to be TRUE for the loop body to execute.
The problem in the first loop might be hard to spot, I must say.
It's about the $i > 8, your code doesn't even enter the loop. Invert the operator, ($i = 0 ; $i < 8 ; $i++)
This worked for me.
I'm trying to make it so it loops fractions as 1, 1/2, 1/3, 1/4, etc up to 1/100, then add all them together and only show the sum and not the chain of fractions.
The code I have so far looks like this:
<?
$sum = 0;
for($n = 1; $n<=1/100; $n+=1/$n)
{
$sum = $sum + $n;
}
echo $sum;
But it never gives me the right answer, which is 5.18. Any advice?
You need to limit the loop. And you need to increment $n
$sum = 0;
for($n = 1; $n<=100; $n++)
{
$sum = $sum + (1/$n);
}
echo $sum;
//answer 5.1873775176396
Just FYI, for a simple operation like this, your for loop does not even need a body.
$sum = 0;
for($n=1; $n<=100; $sum +=(1/$n++));
echo $sum;
See example four in the php for documentation.
for($n = 1; $n<=; $n+=1/$n)
You must define a limit insecond parameter of for, it runs in infinity
EDIT:
Between second and third parameter, you must insert ;
<?php
$sum = 0;
for ($n=1; $n<=100; $n++) {
$sum = $sum + (1/$n);
}
echo $sum;
// or use round if you want it rounded
//echo round($sum,2);
This code is PSR-2 compliant
Try this:
for ($n = 0, $sum = 0; $n < 100; $n++, $sum += 1 / $n);
echo $sum;
I accidentially stumbled over the following code snippet that had me scratch my head for quite a while:
$sum = 0; $realSum = 0;
foreach (range(0,5) as $number) {
$sum =+ $number;
$realSum += $number
}
echo "Sum: $sum, RealSum: $realSum";
// prints 'Sum: 5, RealSum: 15'
?>
What I wanted was obviously the += statement, but somehow PHP wouldn't raise any errors or warnings about the += at all. My IDE also didn't complain about it.
What's this =+ thing in $sum =+ $number? I couldn't find anything on this in the official documentation.
This sign is the sign of the number. So if $number equals 3, if you put -$number, the value will be -3.
$sum =+$number;
This one is like $sum = 0 + $number, it gets last value of the array which is 5
$realSum += $number;
But this one is like $realSum = $realSum + $number