for ($x = 0.01;
$x <= 0.99;
$x++) {?>
<option value="<?php echo $x;?>"><?php echo $x;?></option>
<?php
} ?>
This does not work.
I am trying to get a decimal loop from .01 to .99
The amount of time I have spent on this I could have just typed it out manually :)
You just have to change this:
$x++ //Increments the value by 1
to this:
$x = $x + 0.01 //Increments the value by 0.01
Modify your code this way,
for ($x = 0.01; $x <= 0.99; $x = $x + 0.01) {?>
<option value="<?php echo $x;?>"><?php echo $x;?></option>
<?php
} ?>
You can define the incremental value as shown above
Try this:
for ($x = 0.01; $x <= 0.99; $x++0.1)
Initially, x = 0.01.
While incrementing (x++), x = 1.01.
So your condition fails second time, since 1.01 is greater than 0.99.
Hope you can figure it on your own on how to fix.
Related
I am looking for a way to populate an option value.
<?php for ($i = 1; $i <= 300; $i++) : ?>
<option value="<?php echo $i; ?>">1.<?php print $i; ?> AVAX</option>
<?php endfor; ?>
I want for the above code to work something like this. 1.00 - 1.99 after reaching 1.99 I want it to navigate to 2.00 - 2.99 and so forth and onward until the numbers run out. How can I achieve that? The above result returns only 1.300
You can set the increment to whatever you want
<?php for ($i = 1; $i <= 300; $i+=0.01) : ?>
<option value="<?php echo number_format($i, 2, '.', ''); ?>"><?php echo number_format($i, 2, '.', ''); ?> AVAX</option>
<?php endfor; ?>
demo
What do you mean by until numbers run out?
You do realize that numbers are infinite.
Therefore, I can only assume that you want the options to have the series:
1.01, 1.02.... 1.99, 2.00, 2.01, 2.02, .... until 10.00.
If 10 is not the highest number you want to reach, then you can change the end limit of $i in the following code:
<?php
for ($i = 1; $i <= 10; $i++) {
for ($j = 0; $j <= 99; $j++) {
$num = "$i." . sprintf('%02d',$j); // 1.01, 1.02, ...
echo '<option value="' . $num . '">AVAX</option>';
}
}
?>
I'm trying to use ONE PHP for loop to echo the sum of the numbers 1-10 as well as echo the sum of only the even numbers. I seem to have a problem as these iterations won't be "parallel"
Code:
<?php
$sum = 0; $evensum = 0;
for($x = 1, $y=2; $x<=10, $y<=6; $x++, $y += 2) {
$sum = $sum + $x; $evensum = $evensum + $y;
}
echo "total sum= ". $sum, ", even sum=" . $evensum;
?>
total sum should reflect 55 (1+2+3+4+5+6+7+8+9+10) and even sum should reflect 30 (2+4+6+8+10)
Just Use
<?php
$sum = 0; $evensum = 0;
for($x = 1; $x<=10; $x++) {
// sum all the number
$sum = $sum + $x;
// check the number is even
if( $x % 2 === 0 ) {
// sum only the even numbers
$evensum = $evensum + $x;
}
}
// output
echo "total sum= ". $sum, ", even sum=" . $evensum;
?>
Another approach is using range() and array_functions
$arr=range(1,10);
echo $sum=array_sum($arr);
function even($var)
{
return(!($var & 1));
}
echo $even=array_sum(array_filter($arr, "even"));
Sum all ranges
$all_sum=array_sum(range(1,10));
Sum even number (0,2,4,6,8,10)
$even_sum = array_sum(range(0,10,2));
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
PHP: Here is what I am supposed to accomplish. And below is my code. I am definitely missing something..
Write a FOR loop using an array that prints “The product of first 10 numbers is ” followed by the product of numbers 1 through 10. Here’s a hint: do NOT begin counter at 0 or else the product will be a zero!
<?php
$numbers = 0;
$numbers = range(1 , 10);
$arrlength = count($numbers);
for ($x = 1; $x <= $arrlength; $x++) {
$numbers[$x] = $numbers + $x;
}
echo "The product of first 10 numbers is $numbers.<br>";
?>
Essentially what you're doing is creating a factorial function for a limited case. The problems with your implementation are that you seemed to re-use the $numbers variable when it should be one variable to hold the product and one for the array 1-10. The second issue is in your loop where you loop from 1-10 but what you probably really want to do is loop from 0-9 which are the array indices so that you can get the values from the array like this:
<?php
$prod = 1;
$numbers = range(1 , 10);
$arrlength = count($numbers);
for ($x = 0; $x < $arrlength; $x++) {
$prod *= $numbers[$x];
}
echo "The product of first 10 numbers is $prod.<br>";
?>
The output of which will be:
The product of first 10 numbers is 3628800.
Please note I have not tested this code, but I'm sure it should work.
<?php
$numbers = range(1, 10);
$product = 1;
for ($i = 0; $i < count($numbers); $i++) {
$product *= $numbers[$i];
}
echo 'The product of the first 10 numbers is ' . $product . '<br />';
?>
hello i'm beginner for programming I've got a homework. googled it but couldnt find anything...
i need to get the total value of numbers from 1 to 10. this need to be done in loop. but couldn't figure which loop should i use. if you can also give me an example code thats would be great.
This is a homework question, I'm not sure why people are just giving you an answer to copy-paste.
Achieving the sum of numbers 1..10 is pretty simple. You will need to initialise an empty int var before your loop, and for each iteration from 0 up to and including 10 you will add your int var to the current iteration.
For example:
sum = 0;
for num in range 1 to 10:
sum = sum + num;
<?php
$start = 0; // set the variable that will hold our total
for($i=1;$i<11;$i++){ // set a loop, read here: http://php.net/manual/en/control-structures.for.php for more info
$start += $i; // add $i to our start value
}
echo $start; // display our final value
I would use a for loop.
$total = 0;
for($i = 1; $i <= 10; $i++){
$total += $i;
}
Using the for loop:
<?php
$sum = 0;
for($i = 1; $i <= 10; $i++){
$sum += $i;
}
Using the foreach loop:
<?php
$sum = 0;
foreach(range(1,10) as $num){
$sum += $num;
}
echo $sum; // prints 55
And disregarding your assignment, here is an easier way:
echo array_sum(range(1,10));