This question already has answers here:
PHP: How do you determine every Nth iteration of a loop?
(8 answers)
Closed 3 years ago.
In php i have loop e.g.
for ($i = 0; $i <= 1000; $i++) {
if ($i = 10 || $i == 20 || $i == 30 || $i == 40 || $i == 50 || $i == 60) {
echo $i;
}
}
imagine i need to echo $i every 10,20,30,40,50,60,..970,980,990 there should be way to not write 100 conditions in if statement. Is there some logical way to see if $i increased by 10 then do something like:
if ($i == $i+10) {
...
}
P.S. if possible i dont want to introduce another variable to count i need to find solution with using only $i
Try:
if ($i % 10 == 0)
That will trigger whenever your index is exactly divisible by 10.
rewrite your for loop to:
for ($i = 0; $i <= 1000; $i+=10) {
And I don't know whether it worked for you with commas like this (as in your initial post):
for ($i = 0, $i <= 1000, $i++) {
Skip extra looping:
for ($i = 10; $i <= 1000; $i=$i+10) {
echo $i;
}
Or if you still want to loop every single digit between:
for ($i = 0; $i <= 1000; $i++) {
if( $i % 10 === 0 ) {
echo $i;
}
}
Test Here
Put this inside your main loop. '%', or 'mod', gives you the remainder of $i / 10. If the remainder is '0', then you want to print.
if(0 === ($i % 10)) {
echo $i;
}
Related
I am working with some legacy PHP code so re-writing this isn't an option at this point but I have a dropdown for number of years and months of employment and currently they go from 0 - 11, 0 - 65. Can a PHP loop array numbers starting at NULL, which adds -Select- as the default forcing user to make a selection, but also have 0 as the starting number?
I've tried:
for ($i = NULL; $i <= 11; $i++) {
echo $i;
}
But 0 is no loner an option
This is what I have currently:
for ($i = 0; $i <= 11; $i++) {
echo $i;
}
I need it to display as:
-Select-
0
1
2
3
4
etc
Echo the text before echoing the numbers using a loop.
<?php
echo '-Select-';
for($i = 0; $i <= 11; ++$i) {
echo $i;
}
NULL++ would not increase the value of NULL, which is essentially nothing. Why not start at -1, and if ($i == -1) then echo select?
It would look like this:
for ($i = -1; $i <= 11; $i++) {
if ($i == -1) {
echo '-Select-';
} else {
echo $i;
}
}
I am stuck with this php loop. Then n = 3, k = 5, s = 21.
Can anyone help me please ?
Rows 3. In the first row 5 chairs:
1 row: ⑁⑁⑁⑁⑁ (5 chairs)
2 row: ⑁⑁⑁⑁⑁⑁⑁ (7 chairs)
3 row: ⑁⑁⑁⑁⑁⑁⑁⑁⑁ (9 chairs)
Chairs in total: 21
<?php
for ($i=0; $i<=6; $i++) {
for ($j=0; $j<$i; $j++) {
if($i == 1) {
echo '';
}else{
echo '⑁';
}
} echo ' ';
}
?>
If I understand correctly what you want,
$n is number of row you needed,
$k number of chair at row 1.
Each row chair is added by two.
<?php
$n = 3;
$k = 5;
for($i = 0; $i < $n; $i++) {
if($i >= 1) {
echo PHP_EOL;
}
echo str_repeat('-', $k + ($i * 2));
}
Example: When n = 3 and k = 8, it must be obtained that order s = 30 chairs.
Create a PHP solution that specifies the N-row queue in the variables, K-how many chairs should be in the first row.
For example: N = 3; K = 5;
After loading the page (after performing the program actions with the variables available) the following should be displayed:
Rows 3. First row 5 chairs:
Queue 1: ⑁⑁⑁⑁⑁ (5 chairs)
Queue 2: ⑁⑁⑁⑁⑁⑁⑁ (7 chairs)
Queue 3: ⑁⑁⑁⑁⑁⑁⑁⑁⑁ (9 chairs)
Total required chairs: 21
I think you want to use 'if' syntax. Is it right?
If it's not correct, You should describe your work more detail
<?php
// ...
for($i = 0; $i< sizeof($rows); $i++ ){
if ($i % 2 == 0) {
print "It's even";
}
}
// ... If a row is an Array, use this one.
for($i = 0; $i< sizeof($rows); $i++ ){
for($j = 0; $j< sizeof($rows[$i]); $j++ ){
if ($j % 2 == 0) {
print "It's even index of a row";
//do Something.
}
}
}
?>
If you have a simple counter loop, how do you detect special patterns, for example, every 10 increments but at 6/16/26/36. $i needs to start at 0 too.
The only approach I can think of is this one, but obviously it doesn't work for large loops:
for ($i=0; $i < 1000; $i++) {
// if ( $i == 6 || $i == 16 || $i == 26...... etc ) { do something }
}
There's not going to be one single answer for all types of patterns, but so long as there is a pattern, you can figure it out:
for ($i=0; $i<1000; $i++) {
if (($i-6)%10 == 0) {
// every time $i minus 6 is evenly divisible by 10
}
}
So I have a query, and though my query can possibly return 20 results, I only want it to show the first 9 results. There is a dumb reason I'm not just limiting the query results to 9, for this purpose I need to know how to stop the while function if $i reaches 9.
Code is
$i = 0;
while($array = mysql_fetch_array($queryresults) && $i <= '9')
{
echo $array['id'];
$i++;
}
How do I get it to stop putting out more echoes after 9th result? Thank you!
Putting the limiting condition first will also make sure you only fetch 9 rows, not and 10 and one discard because && is called lazy (if the first part is false, it wont even look at the next part).
$i = 0;
while($i < 9 && $array = mysql_fetch_array($queryresults)){
echo $array['id'];
$i += 1;
}
or if you want to be fancy:
$i = 9;
while($i-- > 0 && $array = mysql_fetch_array($queryresults)){
echo $array['id'];
}
But I find this error prone ..
$i = 0;
while($array = mysql_fetch_array($queryresults) && $i++ < 9)
echo $array['id'];
}
Notes:
No need to put 9 in quotes
You can increment-after-compare by doing $i++ in the while loop condition (although you don't have to, you could also put it after echo $array['id'] in the while loop body).
Careful! You want < 9 and not <= 9 since starting at zero, or you'll get 10 iterations of the loop.
Cheers
Your $i++ needs to be inside the while loop
You should also have $i <= 9 instead of $i <= '9' but it should still work as is
'9' needs to be 9
$i = 0;
while($array = mysql_fetch_array($queryresults) && $i < 9){
echo $array['id'];
$i += 1;
}
This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
php display number with ordinal suffix
I'm attempting to add ordinal contractions i.e.(st/nd/rd/th) to an increment.
Somehow I need to get the last digit of $i to test it against my if statements...
Here is my code so far:
$i = 1;
while($i < 101 ){
if($i == 1){$o_c = "st";}else{$o_c = "th";}
if($i == 2){$o_c = "nd";}
if($i == 3){$o_c = "rd";}
echo $i.$o_c."<br/>";
$i++;
}
You can use the modulus (%) operator to get the remainder when dividing by 10.
$i = 1;
while($i < 101 ){
$remainder = $i % 10;
if($remainder == 1){$o_c = "st";}else{$o_c = "th";}
if($remainder == 2){$o_c = "nd";}
if($remainder == 3){$o_c = "rd";}
echo $i.$o_c."<br/>";
$i++;
}
What about using the modulus operator: $i % 10?
Display numbers with ordinal suffix in PHP
(that thread has other solutions. I liked that one)