I am trying to run the while loop until it equals to thirty.
<?php
$num1=0;
$num2=0;
$sum= $num1 + $num2;
while($sum=30){
$num1++;
$num2++;
echo "$sum is equal to 30";
}
?>
You need to change while($sum=30) to while($sum<30).
Then, the while loop will end after you reached 30. The echo then comes after the closing bracket.
Sp your working code will look like this:
while( $sum < 30 )
{
$num1++;
$num2++;
$sum = $num1 + $num2;
}
echo "sum is $sum (which is 30)";
You are calculating the sum outside the while loop, so inside the loop the $sum never changes. You have to calculate the sum inside the loop. also = is assignment operator. you must use comparison operator to compare.
$num1=0;
$num2=0;
$sum=0; //initilize you sum to 0
while($sum<30){ // loop while your sum is less than 30
$sum= $num1 + $num2; //calculate the sum
$num1++;
$num2++;
}
echo "$sum is equal to 30";
?>
Reference:Comparison Operators
use equality operator (==) not assignment operator (=)
This loop not ends. $sum ever is 0 !.
$num1=0;
$num2=0;
$sum= $num1 + $num2;
$x = 0;
while(!$x && $sum <=30){
$num1++;
$num2++;
$sum= $num1 + $num2;
if($sum ==30){
echo $sum." is equal to 30";
$x = 1;
}
}
Try this! Have a nice day !!
Related
Here's my problem:
Use a while loop to write a program that displays the sum of the perfect squares of numbers 1 to 20. Use the variable $counter as the loop control variable. Create variables named $squares and $sum to hold the results. After and outside the loop body, use echo to display the string The sum of the perfect squares is $sum. The variable $sum should be replaced with its value.
Hint: A perfect square is a number that is the product of an integer multiplied by itself. For example, the perfect square of 2 is 4 since 2*2 = 4.
problem 1: Expected output is "The sum of the perfect squares is 2870."
problem 2: Expecting two occurrences of the variable named 'squares'.
problem 3: Expecting four occurrences of assignment operations that assign the values to the variables 'counter', 'sum', and 'squares'.
here's my code:
<?php
$counter = 1;
while($counter <= 20)
{
$squares = $counter * $counter;
$sum = $squares + $squares;
echo "The sum of the perfect squares is $sum. \n";
$counter ++;
}
?>
<?php
$counter = 1;
$sum = 0;
$squeares = array() ;
while($counter <= 20)
{
$squares[$counter] = $counter * $counter;
$sum += $squares[$counter];
$counter ++;
}
echo "The sum of the perfect squares is ". $sum;
?>
check it
I have the following If statement and I want to make it shorter (eg. a one liner), without writing my if statement in one line.
if ($start + $count > $total) {
$count = $total;
}
Basically I want to achieve that $count + $total is never higher then $total and if this is the case I want to set $count equal to $total.
You can use min() for that:
$count = min($total, $start + $count);
What you want is called a ternary operation.
$count = (($start + $count) > $total ? $total : null);
References:
https://davidwalsh.name/php-shorthand-if-else-ternary-operators
http://php.net/manual/en/language.operators.comparison.php
https://www.abeautifulsite.net/how-to-use-the-php-ternary-operator
You could use the ternary operator.
$count = ($total < $start+$count) ? $total : $count;
This puts the if-logic in one line.
I need to find the averege after using a loop counting ever 3rd to a 100. The loop part is easy enough, but I need to sum every value then divide the sum on the total of values.
for ($x = 3; $x < 100; $x+=3) {
echo $x.", ";
}
This is the loop I need to use. How to I sum the values this produces and how do I find how many values this loop produces?
I believe the intention here is to learn about loops, otherwise this stuff can be done without looping too.
For learning purpose, you can simply introduce two variables count and sum and compute them inside the loop. For count, you just increment it on each iteration. For sum, you add the current value of x into sum. After the loop you print both variables.
$count = 0;
$sum = 0;
for ($x = 3; $x < 100; $x+=3) {
echo $x.", ";
$count++;
$sum+=$x;
}
echo $sum;
echo $count;
add your elements into an array and then use array_sum to sum the array elements , then divide the sum by the count of your array
$arr = [];
for ($x = 3; $x < 100; $x+=3) {
// echo $x.", \n";
$arr[] = $x;
}
print_r(array_sum($arr) / count($arr));
// Output : 51
$i=0;
$tempx=0;
for ($x = 3; $x < 100; $x+=3) {
//total sum
$tempx = $tempx + $x;
//count of how many times the loop ran in this case 33 times
$i++;
}
//first $i was 0 so we add 1
$i=$i + 1;
//getting the average
$average=$tempx / $i;
echo $average;
//output
For the last answer i think we should not do:
//first $i was 0 so we add 1 $i=$i + 1;
Regards
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.
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