This question already has answers here:
Can you 'exit' a loop in PHP?
(6 answers)
Closed 4 years ago.
How to prevent for loop from exceeding condition?
Loop below returns 25. I want it to return 20.
for ($i = 5; $sum < 23;)
{
echo $sum += $i;
}
This loop is just an example. There will be variables with any value in place of 5 and 23
You have to write your loop differently.
A for loop is designed to run until the conditions evaluates false. In your case, the loop at sum = 20 will still run, becuase sum < 23 evaluates to true. So if you want 20, simply write $sum < 20.
Or if I give it a second thought, you may want to do it like that:
<?PHP
$sum = 0;
for($i = 5; ($sum+$i) < 23;)
{
$sum += $i;
}
echo $sum;
?>
This question already has answers here:
Test if number is odd or even
(20 answers)
Closed 4 years ago.
I am trying to create this algorithm with PHP. I want to be able to echo out the result of this operation:
1 - 2 + 3 - 4 + 5 - 6 +...100
I want to get the result of this till I get to 100.
This how I have already started the code, however I am stuck and don't know how to proceed:
<?php
$somme = 0;
$I = 1;
while($I <= 100){
}
?>
How do I go on from this?
All answers are appreciated
It's a case of knowing, within the loop, when to add and when to subtract. A simple tracker will help us here.
$somme = 0;
$i = 1;
$op = 'sub';
while($i <= 100){
$somme = $op == 'sub' ? $somme - $i : $somme + $i;
$op = $op == 'sub' ? 'add' : 'sub';
$i++;
}
As noted in the comments, you could also decide the operation based on whether $i is even or odd.
This question already has answers here:
PHP array combinations
(8 answers)
PHP algorithm to generate all combinations of a specific size from a single set
(5 answers)
Closed 5 years ago.
Its hard to explain this in the title
I have array of different value. lets say this is the array:
$char = array("a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","1","2","3","4","5","6","7","8","9","0");
how do I print every possible 3 char of this array.
like start off...
aaa
aab
aac
and so on...
Here is the example of possible two alphanumerics
<?php
$start = base_convert("10",36,10);
$end = base_convert("zz",36,10);
for($start;$start <= $end;$start++){
echo base_convert($start,10,36)."\n";
}
?>
Live Demo
You can start from 100 to zzz to get all possible digits of three albhanumeric.
You need to loop 3's to find your desire output.
$char = array("a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","1","2","3","4","5","6","7","8","9","0");
$length = count($char);
for($i = 0; $i < $length; $i++)
for($j = 0; $j < $length; $j++)
for($k = 0; $k < $length; $k++)
echo $char[$i].$char[$j].$char[$k]."<br/>";
Output:
aaa
aab
aac
aad
aae
aaf
aag
aah
aai
...
This question already has answers here:
Formatting a number with leading zeros in PHP [duplicate]
(11 answers)
Closed 8 years ago.
I'm trying to get the results of the for loop like: 00001, 00002, 00003, etc. but the result is not displaying 0's: instead I get: 1, 2, 3, etc.
This is the code:
$min = 00001;
$max = 00005;
for ($x = $min; $x <= $max; $x++) {
echo "$x ";
}
Just change your echo statement to this:
echo sprintf('%05d', $x);
Also i would recommend you to change your $min and $max variables to this:
$min = 1;
$max = 5;
Because if you have leading zeros then number gets interpreted as an octal number! So 00012 would not be 12 it would be 10.
<?php
$min = 00001;
$max = 00005;
for ($x = $min; $x <= $max; $x++) {
printf("%5d",$x);
}
?>
this is new code
<?php
$min = 00001;
$max = 00005;
for ($x = $min; $x <= $max; $x++) {
printf("%05d",$x);
echo '<br>';
}
?>
Thanks a lot for the help. Doing the trick via MySQL got everything done. Just had to change the value to decimal and assign the field size (7) with the corresponding zerofill attribute.
Now i can insert the count of the for loop without zeros but the db inserts them for me. Thanks
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)