I need to subtract 10 from a value until it's below ten and then use it outside of the loop but the value doesn't seem to change.
I'm not sure how many wrongs I'm making but I bet it's many!
$x = 1987;
$y = 2015;
$b = $y - $x;
for($b; $b > 10; $b - 10){
echo $b; //This is supposed to be echo:ed when the loop is done
}
Thanks in advance!
You are not actually modifying $b:
$x = 1987;
$y = 2015;
$b = $y - $x;
for($b; $b > 10; $b = $b - 10) { // <- this line
echo $b;
}
Also, there is no need for the initial $b here:
for(; $b > 10; $b = $b - 10) {
Or you could get rid of:
$b = $y - $x;
And just use:
for($b = $y - $x; $b > 10; $b = $b - 10) {
Or you could just do
$x = 1987;
$y = 2015;
$b = ($y - $x) % 10;
Which is basically what you're doing, only you chose the hard way with the for loop :)
$x = 1987;
$y = 2015;
$b = $y - $x;
for($b; $b > 10; $b -= 10) {
echo $b;
}
$x = 1987;
$y = 2015;
$b = $y - $x;
for(; $b > 10; $b -= 10);
echo $b;
echo will happen only after completing loop. This will reduce the $b value by multiples of 10. $b will be less than 10 after loop.
A while loop is more readable than a for loop. Also, put the echo statement after the loop has completed.
$x = 1987;
$y = 2015;
$b = $y - $x;
while($b > 10) {
$b -= 10;
}
echo $b;
Related
I am searching for a php for loop, which should output
22,23,24, 32,33,34, and so on 92,93,94 and all following like 102,103,104,122,123 until 902,903,904,992,993,9949, but not or 12,13,14... 112,13,114 ...912,913,914.
my code:
for ($a = 22; $a <= 1000; $a+=10) {
$b = $a + 1;
$c = $a + 2;
echo "$a <br>";
echo "$b <br>";
echo "$c <br>";
}
I need here an exception, because 112,123 and 124 and so on until 912,913,914 shouldn't be echoed.
All results should be stored in an array.
Try using this code:
$data = array();
for ($a = 2; $a < 10; $a++) {
for ($b = 2; $b <= 4; $b++) {
$c = ($a * 10) + $b;
$data[] = $c;
}
}
I have found all parts do finish kwestgrounds answer with my exceptions.
$delete = array_merge(range (112, 912, 100), range (113, 913, 100),range (114, 914, 100));
$data = array();
for ($a = 2; $a < 100; $a++) {
for ($b = 2; $b <= 4; $b++) {
$c = ($a * 10) + $b;
{
if (in_array($c, $delete)) continue;
}
$data[] = $c;
}
}
Let's say I have 3 variables
$a;
$b;
$c = 30;
What I wish to do here is that I have to divide $c and put it in the first 2 variables which can be easily done by doing.
$a = $c / 2;
$b = $c / 2;
However what if in $b there is a maximum value limit of 10 and $a is limitless.
In this case the values have to be.
$a = 20;
$b = 10;
What would be the best solution for this?
If B has a limit, then you would need to calculate B first. Then work out the difference between C & B and set A as the answer.
<?php
$a;
$b;
$c = 100;
$b = min(10, $c / 2);
$a = ($c - $b);
echo "A: " . $a;
echo "B: " . $b;
?>
Try using min
$a = $c / 2;
$b = min(10, $c / 2);
This question already has answers here:
Double? Integer? -- PHP
(2 answers)
Closed 8 years ago.
The code below generates two random decimal values, then subtracts them to get $c.
The do-while loop is trying to ensure that $c will not be a whole number. But I keep getting times where $c actually is a whole number.
do{
unset($a);
unset($b);
unset($c);
unset($adjuster);
unset($c_is_int);
$a = mt_rand(5, 75);
$b = mt_rand(5, 75);
$adjuster = mt_rand(2, 20);
$decimal_selector = mt_rand(1, 6);
if ($decimal_selector == 1){
$a = $a / 10;
$b = $b / 10;
}
if ($decimal_selector == 2){
$a = $a / 10;
$b = $b / 100;
}
if ($decimal_selector == 3){
$a = $a / 100;
$b = $b / 10;
}
if ($decimal_selector == 4){
$a = $a / 100;
$b = $b / 100;
}
if ($decimal_selector == 5){
$a = $a / 1000;
$b = $b / 1000;
}
if ($decimal_selector == 6){
$a = $a / 1000;
$b = $b / 100;
}
if($b < $a){
$b = $b + ($a - $b) + $adjuster;
}
$c = $b - $a;
if(intval($c) == $c) {
$c_is_int = 1;
} else {
$c_is_int = 0;
}
echo $a . '<br><br>';
echo $b . '<br><br>';
echo intval($c) . '<br>';
echo $c_is_int . '<br>';
echo $c . '<br><br>';
} while($c_is_int == 1);
The attached image shows the results of one of these failing times. Any ideas on where this is going wrong?
Why not check to see if the number has a decimal?
$c = 123.456;
if(strpos((string) $c, '.') !== FALSE) {
// is decimal/float/double
}
You can also just check to see if it's an int
if(is_int($c))
You want to keep in mind that the integer 3 and float 3.0 will compare as equal when using the ==.
Consider:
$c_is_int = $c == floor( $c );
...
while( $c_is_int )
The following code generates two random decimal values, then subtracts them to get $c.
$a = mt_rand(5, 75);
$b = mt_rand(5, 75);
$adjuster = mt_rand(2, 20);
do {
$decimal_selector = mt_rand(1, 6);
if ($decimal_selector == 1) {
$a = $a / 10;
$b = $b / 10;
}
if ($decimal_selector == 2) {
$a = $a / 10;
$b = $b / 100;
}
if ($decimal_selector == 3) {
$a = $a / 100;
$b = $b / 10;
}
if ($decimal_selector == 4) {
$a = $a / 100;
$b = $b / 100;
}
if ($decimal_selector == 5) {
$a = $a / 1000;
$b = $b / 1000;
}
if ($decimal_selector == 6) {
$a = $a / 1000;
$b = $b / 100;
}
if($b < $a)
$b = $b + ($a - $b) + $adjuster;
$c = $b - $a;
} while((is_int($a) == true && is_int($b) == true) || is_int($c) == true);
The do-while loop is trying to ensure that $a and $b are both not integers, and also that $c is not an integer. But I keep getting times where $c actually is an integer.
If I use gettype it keeps saying $c is a "double". Why, when $c ends up being something like 7?
EDIT:
I keep getting random infinite loops from this code below. Any ideas why?
do{
$a = mt_rand(5, 75);
$b = mt_rand(5, 75);
$adjuster = mt_rand(2, 20);
$decimal_selector = mt_rand(1, 6);
if ($decimal_selector == 1){
$a = $a / 10;
$b = $b / 10;
}
if ($decimal_selector == 2){
$a = $a / 10;
$b = $b / 100;
}
if ($decimal_selector == 3){
$a = $a / 100;
$b = $b / 10;
}
if ($decimal_selector == 4){
$a = $a / 100;
$b = $b / 100;
}
if ($decimal_selector == 5){
$a = $a / 1000;
$b = $b / 1000;
}
if ($decimal_selector == 6){
$a = $a / 1000;
$b = $b / 100;
}
if($b < $a){
$b = $b + ($a - $b) + $adjuster;
}
$c = $b - $a;
if(intval($c) == $c) {
$c_is_int = 1;
} else {
$c_is_int = 0;
}
echo intval($c) . '<br>';
echo $c_is_int . '<br>';
echo $c . '<br><br>';
} while($c_is_int == 1);
A double subtracting a double results in a double, even if that number is a whole number
$c = (int) ($b - $a);
Do something like this to see if $c is a whole number (possibly stored as a double):
if(intval($c) == $c) {
echo "I'm a whole number";
}
See here: http://3v4l.org/ubPK4
Can a do-while loop have multiple conditions? If so, I can't figure out why the code below is failing on all but the first condition.
Functions used...
function gcf($a,$b) {
$a = abs($a); $b = abs($b);
if( $a < $b) list($b,$a) = Array($a,$b);
if( $b == 0) return $a;
$r = $a % $b;
while($r > 0) {
$a = $b;
$b = $r;
$r = $a % $b;
}
return $b;
}
function factors($n){
$factors_array = array();
for ($x = 1; $x <= sqrt(abs($n)); $x++)
{
if ($n % $x == 0)
{
$z = $n/$x;
array_push($factors_array, $x, $z);
}
}
return $factors_array;
}
Code...
$a = $b;
do{
$a = mt_rand(8, 100);
$a_factors_array = factors($a);
$b = mt_rand(8, 100);
$b_factors_array = factors($b);
} while ($a == $b && count($a_factors_array) < 4 && count($b_factors_array) < 4 && gcf($a, $b) == 1);
echo $a . '<br>';
echo $b . '<br>';
echo count($a_factors_array) . '<br>';
echo count($b_factors_array) . '<br>';
echo gcf($a, $b) . '<br>';
I keep getting numbers for $a and $b that have less than 4 factors and have a GCF of 1. Any ideas?
You'll need || instead of &&. You want to repeat the loop as long as any one of your conditions is met. Currently the loop is only repeated if all of the conditions are met.
I think you have an ANDs where you meant an ORs:
do{
$a = mt_rand(8, 100);
$a_factors_array = factors($a);
$b = mt_rand(8, 100);
$b_factors_array = factors($b);
} while ($a == $b || count($a_factors_array) < 4 || count($b_factors_array) < 4 || gcf($a, $b) == 1);
With your way, the while stops if $a !== $b which is probably not what you want.