Hi this is a table converting celsius to kelvin and fahrenheit, I am just wondering why my code does not loop :( it only displays the first two lines and stops. Thank you!
<?php
$celsius = 100;
$stop_kelvin = 0;
print '<table>';
print '<tr><th>Degrees Celsius(C)</th><th>Kelvin(K)</th><th>Degrees Fahrenheit(F)</th></tr>';
while ($kelvin <= $stop_kelvin) {
$fahr = ($celsius*1.8) + 32;
$kelvin = $celsius + 273;
print"<tr><td>$celsius</td><td>$kelvin</td><td>$fahr</td></tr>";
$fahr += 1;
}
print '</table>';
?>
In your code, $kelvin variable is not initialized. Also, please re-think your loop logic.
First of all you havent initialized $kelvin; because of which $kelvin is taking some random values.
After first loop the value of $kelvin becomes 373 and in your loop it is $kelvin <= $stop_kelvin means the condition is false and it jumps out of the loop
Related
A total noob needs to do a math calculation in a for loop inside a function.
The closest I come is this, but it does not work of course.
function z1()
{
for ($x=1; $x<=11; $x++)
{
$z1=2/3+5*$x/(3-$x);
}
return $z1;
}
Any help or a solution would be appreciated
EDIT: FIXED
Needed to declare what to do if it is NaN or infinite
Not entirely sure what you're trying to do, but I'll make some assumptions.
In addition to a divide by zero error this gets, you are overwriting the value of $z1 each time the loop executes, so the value of $z1 once you return is whatever the final iteration of the loop assigned to $z1.
I think you may want to initialize the variable before the loop and use the += operator inside the loop, so the value of $z1 is incremented each time the loop iterates.
function z1() {
$z1 = 0;
for ($x = 1; $x <= 11; $x++) {
$z1 += 2 / 3 + 5 * $x / (3 - $x);
}
return $z1;
}
How do you calculate (sum) + a value in foreach loop?
I am working on a cricket application where i have to count the loop for each 6 times and then count specific value and then echo the total.
I have a code not exact but something like this.
And there are two values:
Balls $balls['1']; array like 1,2,3,4,5 and up to 300-1000 balls
Runs $balls['6']; array like 2,3,1,5 random numbers could be any;
Values comes from mysql table columns balls and runs
foreach( $balls as $ball ){
$countball++;
// here is what i need to know how do i calculate the values of $ball + $ball?
// so i can echo it inside the below if condition?
$runs = $ball['runs'] + $ball['runs']; // not working
if($countball == 6){
echo $runs;
}
$runs+= $ball; // reset the ball counting to continue addition from loop?
// and reset the
}// end foreach
however something like this works fine for the first $countball == 6. but after that it does not show the exact value
You forget to reset the $countball.
You may change the if part as :
if($countball == 6)
{
echo $runs;
$countball = 0;
}
Maybe this is what you need:
$runs = 0;
foreach( $balls as $ball ){
$runs += $ball['runs'];
}
echo $runs;
With help of #Barmar from above i got the desired output as followings
$runs = 0;
$countball=0;
foreach( $balls as $ball ){
$countball++;
$runs += $ball['runs'];
if($countball == 6){
// reset runs from current loop runs for next, if i reset $runs = 0
// for some reason it does not (SUM) + this loops $ball['runs'] with last 5;
$runs = $ball['runs'];
$countball=0;
}// end if $countball == 6
}// end foreach
echo $runs;
Newb question: I'm using a foreach loop to get items from an array.
I need to start looping at an offset number- (I'm using a $i variable to do this, no problem).
But when my foreach reaches the end of the array I want it to start going through the array again until it reaches the offset number.
I need to do this so I can have a user open any image in an artist's portfolio and have this image used as the first image presented in a grid of thumbnail icons , with all the other images subsequently populating the rest of the grid.
Any ideas?
Please bear in mind I'm new to PHP! :)
See below for an example of my current code...
$i=0;
$limit=50;// install this in the if conditional with the offset in it (below) to limit the number of thumbnails added to the page.
$offset=$any_arbitrary_link_dependant_integer;
foreach($portfolio_image_array as $k=>$image_obj){//$k = an integer counter, $image_obj = one of the many stored imageObject arrays.
$i++;
if ($i > $offset && $i < $limit) {// ignore all portfolio_array items below the offset number.
if ($img_obj->boolean_test_thing===true) {// OK as a way to test equivalency?
// do something
} else if ($img_obj->boolean_test_thing===false) { // Now add all the non-see_more small thumbnails:
// do something else
} else {
// error handler will go here.
}
} // end of offset conditional
}// end of add boolean_test_thing thumbnails foreach loop.
};// end of add thumbnails loop.
$i = 0;
$limit = 50;
$offset = $any_arbitrary_link_dependant_integer;
$count = count($portfolio_image_array);
foreach($portfolio_image_array as $k=>$image_obj){//$k = an integer counter, $image_obj = one of the many stored imageObject arrays.
$i++;
if ($i > $offset && $i < $limit && $i < ($count - $offset)) {// ignore all portfolio_array items below the offset number.
if ($img_obj->boolean_test_thing===true) {// OK as a way to test equivalency?
// do something
} else if ($img_obj->boolean_test_thing===false) { // Now add all the non-see_more small thumbnails:
// do something else
} else {
// error handler will go here.
}
} // end of offset conditional
}// end of add boolean_test_thing thumbnails foreach loop.
};
Only thing I added was a $count variable.
Edit: If your array starts at 0 I would suggest you put the $i++; at the end of your foreach loop.
A simple method is to use two separate numeric for loops, the first going from offset to end, and the second going from beginning to offset.
<?php
// Create an example array - ignore this line
$example = array(1,2,3,4,5,6);
$offset = 3;
// Standard loop stuff
$count = count($example);
for($i = $offset; $i < $count; $i++)
{
echo $example[$i]."<br />";
}
for($i = 0; $i < $offset; $i++)
{
echo $example[$i]."<br />";
}
?>
This is also almost certainly cheaper than doing multiple checks on every single element in the array, and it expresses exactly what you are trying to do to other programmers who look at this code - including yourself in 2 weeks time.
Edit: depending on the nature of the array, in order to use numeric keys you may first need to do $example = array_values($portfolio_image_array);.
Using Answer Question to force StackOverflow to let me post a decent length of text!
OK #Mark Walet et al, not sure how to post correctly on this forum yet but here goes. I got the issue sorted as follows:
$i=0;
$offset=$image_to_display_number;
$array_length = count($portfolio_image_array);
// FIRST HALF LOOP:
foreach($portfolio_image_array as $k=>$img_obj){// go through array from offset (chosen image) to end.
if ($i >= $offset && $i <= $array_length) {
echo write_thumbnails_fun($type_of_thumbnail, $image_path, $k, $i, $portfolio_image_array, $title, $image_original);
$t_total++;// update thumbnail total count.
}
$i++;
}// end of foreach loop 1.
$looped=true;// Just FYI.
$i=0;// Reset.
// SECOND HALF LOOP:
foreach($portfolio_image_array as $k=>$img_obj){// go through array from beginning to offset.
if ($i < $offset) {
echo write_thumbnails_fun($type_of_thumbnail, $image_path, $k, $i, $portfolio_image_array, $title, $image_original);
}
$i++;
}// end of foreach loop 2.
Thankyou so much for all the help!
:)
as #arkascha suggested use modulo operator
<?php
$example = array(1,2,3,4,5,6);
$count = count($example);
$offset = 3;
for($i = 0; $i < $count; $i++) {
$idx = ($offset + $i) % count
echo $example[$idx]."<br />";
}
?>
I am trying to sum the total results in an if/else statement. so the $result (in there for example) would appear 4 times. I have tried count($result), this doesnt work.
while ($sq=mysql_fetch_array($query)){
if ($avg > $btmspeed && $avg < $topspeed){
$result;
}else{
}
}
Basically I am running a while loop through some database results and these existing variables would give 4 results through the if statement and I want to reflect that. I know its probably and easy answer but banging head against a wall and search engines havent given me the answer. please help!
Use a counter.
Var i=1;
Outside of the loop.
Inside the loop add:
i++;
Do you just need to move the $result declaration outside the loop and increment it inside the 'if' block?
$result = 0
# loop starts here
if ($avg > $btmspeed && $avg < $topspeed){
$result = $result + 1
} else{
}
Assuming your result is a number. You would loop over whatever it is I assume an array with information, where you'd be checking the if-else like you originally provided.
$i = 0;
for($x=0; $x < $result; $x++;)
{
if ($avg > $btmspeed && $avg < $topspeed){
$i++;
}else{
}
}
echo $i;
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));