I have 5 variables $a $b $c $d $e . These variables has numerical values. Im trying to compare these variables where each variable will be compared to the rest and if the condition is true it echoes something. Here is my code
if ($a > ($b && $c && $d && $e)) {
$result = '<div>Im A</div>'
} else if ($b > ($a && $c && $d && $e)) {
$result = '<div>Im B</div>'
} else if ($c > ($a && $b && $d && $e)) {
$result = '<div>Im C</div>'
} else if ($d > ($a && $b && $c && $e)) {
$result = '<div>Im D</div>'
} else if ($e > ($a && $b && $c && $d)) {
$result = '<div>Im E</div>'
}
return $result;
The result stops at first condition even though it is false and it should pass it to other conditions.
Some different approach:
$a = 1;
$b = 3;
$c = 4;
$d = 5;
$e = 0;
// Make array [a=>1, b=>3...]
$arr = compact('a','b','c','d','e');
// Sort it in descending order with saving keys
arsort($arr);
// Get the 1st key
echo 'I\'m ' . strtoupper(key($arr)); // I'm D
First of all - you have parentheses around ($b && $c && $d && $e), this means that in $a > ($b && $c && $d && $e) the result of ($b && $c && $d && $e) will be counted first, and then will be compared to $a.
So, $a > ($b && $c && $d && $e) is not
$a is greater than $b and $a is greater than $c and etc.
it is
$a is greater than result of ($b and $c and $d and $e)
And result of $b and $c and $d and $e is either true or false.
So, in the end you compare $a > true or $a > false. According to value of $a you can get different results.
In a simple case, if you want to check if something is greater than anything else you need to write a condition like:
if ($a > $b && $a > $c && $a > $d && $a > $e) {
Other more tricky solutions you will find in other users' anwers.
You can iterate your five variables and keep track of the one with the highest value.
for ($i='a', $x = 0, $max = 'x'; $i <= 'e'; $i++) {
if ($$i > $$max) {
$max = $i;
}
}
$result = "<div>I'm " . strtoupper($max) . "</div>";
I think you should steer clear of if statements ;) Here's a solution without them that can be used with any number of variables. You could use the $key as an index into a list of functions and make the program extensible.
// Make the numbers into an array
$o = [$a, $b, $c, $d, $e];
// Find the highest value
$max = max($o);
// Look up the highest value to get the index
$key = array_search($max, $o);
// Now you know which one it is, you can do anything with it!
switch ($key) {
case 0: $result = '<div>Im A</div>'; break;
case 1: $result = '<div>Im B</div>'; break;
...
}
Try this:
$array = ['A'=>$a,'B'=>$b,' C'=>$c,'D'=>$d,'E'=> $e];
$maxs = array_keys($array, max($array));
foreach ($maxs as $maxi )
echo '<div>Im '.$maxi.'</div>."<br>"';
This way it covers cases were there are multiple max values.
Refer to this answer for more about how it works.
Related
I have a variable that I need to check against other variables to make sure it is greater than them. I've simplified the example below, but here's a working example of the desired result.
<?php
$a = 5;
$b = 4;
$c = 3;
$d = 2;
$e = 1;
if(($a > $b) && ($a > $c) && ($a > $d) && ($a > $e)){
echo "A is the biggest";
}else{
echo "A is not the biggest";
}
?>
My question is, is there a simpler way to write the if statement so that we don't have to have $a written four separate times? Something along the lines of ...
if($a > $b,$c,$d,$e){
I've seen Simpler way to check if variable is not equal to multiple string values?
but this is for checking the presence of strings.
Just like you can have an array of strings, you can have an array of variables.
The 'most simplistic' approach would be to return the max() of the array variables, and then simply check whether $a is greater than that. This way you only have to do one comparison.
if (($a > $b) && ($a > $c) && ($a > $d) && ($a > $e)) { }
Can be re-written as:
$values = array($b, $c, $d, $e); // array(4, 3, 2, 1);
if ($a > max($values)) { } // if (5 > 4) { }
Hope this helps!
I'd write something like that:
if (max($a, $b, $c, $d, $e) == $a) {
....
}
I'm having a problem regarding comparing two dates. I know this sound kinda silly but I want you all to try this example.
$a = date('d-m-Y', strtotime('2014-07-03'));
$b = date('d-m-Y', strtotime('2014-03-17'));
$c = date('d-m-Y', strtotime('2015-03-16'));
if(($a > $b) && ($a < $c) ) {
echo "1";
}
else {
echo "2";
}
How come the result is 2?
Your "logic" is wrong.
> xkcd
$a = '2014-07-03';
$b = '2014-03-17';
$c = '2015-03-16';
if(($a > $b) && ($a < $c)) {
echo "1"; // it works!
}
Big-endian formats are awesome.
Because your date format is not good for comparing. Always use YYYY-MM-DD format when comparing dates (or use DateTime() objects). This is because 04-01-2014 is less than 05-01-2014 when compared as strings.
$a = '2014-07-03';
$b = '2014-03-17';
$c = '2015-03-16';
if(($a > $b) && ($a < $c) ) {
echo "1";
}
else {
echo "2";
}
Or when using DateTime() objects:
$a = new DateTime('2014-07-03');
$b = new DateTime('2014-03-17');
$c = new DateTime('2015-03-16');
if(($a > $b) && ($a < $c) ) {
echo "1";
}
else {
echo "2";
}
After your date/strtotime function, you have :
$a = '03-07-2014'
$b = '17-03-2014'
$c = '16-03-2015'
So when you compare, $a is less than $b because it compares the day first.
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 )
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.
There are three numbers in set A {3,4,7} and in set B {2,4,7}.
It is not possible to get the result true because the the first numbers in a and b are not same.
But I need to get the result as true by comparing other two number and leaving the first number.
How is it possible to do it in PHP?
try this it will helps you.. it will display the 1 => for more number of combinations as same and 0 => for more of combination as different . in the below code the more number of combination as different so its returns 0.
<?php
$A = array(2,3,7,5,6);
$B = array(4,3,7,8,9);
$flagTrue = 0;
$flagFalse = 0;
for($i=0; $i < count($A); $i++)
{
if($A[$i] == $B[$i])
{
$flagTrue=$flagTrue+1;
}
else
{
$flagFalse=$flagFalse+1;
}
}
$var_is_greater_than_two = ($flagTrue >= $flagFalse) ? 1 : 0;
echo $var_is_greater_than_two;
?>
<?php
$a = array(3,4,7);
$b = array(2,4,7);
echo $a === $b ? 'TRUE' : 'FALSE';
echo PHP_EOL;
array_shift($a);
array_shift($b);
echo $a === $b ? 'TRUE' : 'FALSE';
?>
Shows:
FALSE TRUE
UPD:
If you need to extract values from strings, then:
$strA = '3,4,7';
$strB = '2,4,7';
$a = explode(',', $strA);
$b = explode(',', $strB);
array_shift($a);
array_shift($b);
echo $a === $b ? 'TRUE' : 'FALSE';
Should work.
<?
$A = array(2,3,7);
$B = array(4,3,7);
$isTrue=1;
for($i=1; $i < count($A); $i++) if($A[$i]!=$B[$i]) $isTrue=0;
echo $isTrue;
?>
EDIT:
If you want to return true if exactly two elements are the same, then the code would be:
$common=0;
for($i=0; $i < count($a); $i++) if($a[$i]==$b[$i]) $common++;
if($common==2) $isTrue=1;
function compareSets($a, $b) {
$result = TRUE;
$diffArray = array_diff($a, $b);
foreach ($diffArray as $key => $value) {
if ($key > 0) {
$result = FALSE;
break;
}
}
return $result;
}
<?php
$a = array(3,4,7);
$b = array(2,4,7);
for($i=0;$i<3;$i++)
{
if($a[$i] > $b[$i]
echo true;
}
?>
will give true if a is greater.