$var1 = 22;
$var2 = 10;
echo $var1 = ($var1 < $var2) ? $var1 : $var2; //smaller var
echo '';
echo $var2 = ($var1 > $var2) ? $var1 : $var2; //greater var
I expect it to print 10 and 22 but it prints 10 and 10. any ideas what I am doing wrong?
Thanks
UPDATE
Thanks all.
$min = min($var1, $var2);
$max = max($var1, $var2);
$var1 = $min;
$var2 = $max;
#unicornaddict already solved your problem, but to make it simpler you can use the min and max functions PHP provides.
echo min($var1, $var2), '<br/>', max($var1, $var2);
You are re-assigning the variables in the echo.
// $var1 is being assigned minimum of 10,22 which is 10.
// after this $var1 and $var2 will both be 10.
echo $var1 = ($var1 < $var2) ? $var1 : $var2;
What you want it:
echo ($var1 < $var2) ? $var1 : $var2; // prints min.
echo '<br />';
echo ($var1 > $var2) ? $var1 : $var2; // prints max.
EDIT:
If you always want the smaller of the two values in $var1 you can do:
if($var1 > $var2) { // if $var1 is larger...swap.
list($var1,$var2) = array($var2,$var1);
}
You overwrite $var1 in your first comparison. So the second comparison compares 10 > 10.
$var1 = 22;
$var2 = 10;
echo $var1 = (10 < 22) ? 22 : 10; //smaller var -> $var1 now has the value 10
echo '<br />';
echo $var2 = (10 > 10) ? 22 : 10; //greater var -> 10 is not greater than 10, so $var2 gets a value of 10.
you assign 10 to $var1 with the first echo, so at the second they are both 10.
echo $var1 = ($var1 < $var2) ? $var1 : $var2; //smaller var
This assigns 10 to $var1. Now both variables contain 10. So what do you expect of the second line?
You need a temporary variable. Just use min,
echo min($var1, $var2);
Your question also mentions swapping the values, which the other answers don't seem to make any note of. Given your example code it looks like you want $var1 to contain the smaller of the two values and $var2 the bigger.
$var1 = 22;
$var2 = 10;
if ($var1 > $var2) {
list($var1, $var2) = array($var2, $var1);
}
// $var1 will now be smaller than (or equal to!) $var2
Related
This is not the first time I encountered the problem. I have 2 variables in PHP of type string. When both of them are not empty I want something like this :
echo $var1 . ', ' . $var2;
My problem is that sometimes $var1 or $var2 could be empty so the coma is floating. I try this :
echo $var1 && $var2 ? $var1 . ', ' . $var2 : $var1 || $var2;
But when we are in the right condition, it send 1. I hope the only way is not to test $var1 && $var2 then test $var1 and then test $var2.
Thanks in advance,
echo join(', ', array_filter([$var1, $var2]));
array_filter removes all empty values, and join puts the joiner between the remaining items, if there are more than one.
You can do it by many way e.g by checking empty, using ternary and so on but I've one way to fix this using array_filter() to remove empty values and implode with , . Then it'll not floating when any of the variable is empty string and it can consume multiple variables. Hope this helps :)
<?php
$implode = ', ';
$var1 = 'xyz';
$var2 = '';
$result = [$var1,$var2];
$filtered_result = array_filter($result);
echo implode($implode,$filtered_result);
?>
DEMO: https://3v4l.org/PZ8Rv
try this
if(!(empty($var1) && empty($var2))){
echo $var1 . ', '.$var2;
}else{
$value = !empty($var1)? $var1 : (!empty($var2)? $var2 : "both are empty");
echo $value
}
your right condition use || operator which returns a boolean. if the right statement or left statement is true it will return true (1), or it will return false (0)
then you have to do something like this
echo ($var1 && $var2 ? $var1 . ', ' . $var2 : ($var1 ? $var1 : $var2));
note that $var1 has to be defined (null or empty but it has to be defined)
I have variables $var1, $var2, $var3. Each day they are being fetched from a MYSQL query and being displayed in a row in a table. Sometimes they are positive and sometimes there are negative values. I would like one column to show the sum of only the POSITIVE values. How do I write a php statement to add only the positive values?
Do I need to write 6 if statements or is there an easier way?
You can use PHP Ternary Operator to do it with 4 lines of code.
Simply, if variable is greater than zero, positive (add to sum), else negative (skip, no need to add).
Use ternary operator.
<?php
$sum = 0; // Initialise $sum
$sum += ($var1 > 0) ? $var1 : 0; // If $var1 is greater than 0, add to $sum.
$sum += ($var2 > 0) ? $var2 : 0; // Same as of $var1
$sum += ($var3 > 0) ? $var3 : 0; // Same as of $var1
?>
How would someone neatly check if 4 variables are the same?
Obviously this wouldn't work:
if ($var1 === $var2 === $var3 === $var4)
But what would, without writing loads of code?
One way to go would be this:
if ($var1 === $var2 && $var2 === $var3 && $var3 === $var4)
Not a huge amount of code and it gets the job done.
if(!array_diff([$var2, $var3, $var4], [$var1])){
// All equal
}
if ($var1 === $var2 && $var3 === $var4 && $var1 === $var3)
You don't need to check if 2 and 4 are equal
Using array_unique you can check if the array of unique values from the variable list is 1 (which means they are all equal):
if (count(array_unique([$var1, $var2, $var3, $var4])) == 1)
// all equal
This comes in handy especially when comparing a long list of variables, compared to a long list of == checks in an if statement.
You can use ! as your master and use &&
if($var1===$var2 && $var1 === $var3 && $var1 === $var4)
But this wont let you know which of the 4 is not like the others.
In case you are dealing with many variables, I played with the below code and it works.
$m = 'var'; //Assuming you know the variable name format $var1, $var2, $var3,...
for( $n = 1; $n <= 3; $n++) { //testing for 4 variables
$v = "$m$n";
$n++;
$w = "$m$n";
if ( $$v !== $$w ) {
echo "false";
break;
}
$n--;
}
//Breaks and echo "false" as soon as one of the variables is not equal
//Note: increase the iteration for more variable.
I know it is possible in php to concatenate two strings like
$a .= $b;
which is equal to
$a = $a . $b;
Is it possible to do something with integers? but with math operations? I have two variables:
$var1 = 8;
$var2 = 2;
I need to do $var1 - $var2, but I don't want to create third variable to hold this calculation. I would try $var1 = $var1 + $var2 will this work? Is there another .= similar way of doing it?
OK, because everybody is confused now
// _Appending_ $var2 to $var1 and assign it to $var1
$var1 .= $var2;
// _Add_ $var2 to $var1 and assign the result to $var1
$var1 += $var2;
There is nothing more to remember than, that every binary operator is combineable with the assignment. Within the engine it's always expanded to
$var1 = $var1 . $var2; // You can imagine how the others looks like
In addition to the basic assignment operator, there are "combined operators" for all of the binary arithmetic, array union and string operators that allow you to use a value in an expression and then set its value to the result of that expression.
I think you need a string formatting.
$var1 = sprintf( "%d%d", $var1, $var2 );
which will make your $var1 as 82.
As in C and C++, PHP supports Compound_assignment_operators:
So, the line:
$var1 = $var1 + $var2;
is equivalent to:
$var1 += $var2;
For full list of those operators, take a look at Compound_assignment_operators
Yes
$var1 = 1
$var2 = -1;
$var1 += $var2; // $var1 = 0
Yes, Compound Assignment operators will work in php as C & Java.
you can try like this
$Var1+=$Var2;
$var1=$var1-$var2;
will work fine, but shorthand operators like
$var1=-$var2
will not work.
I will set -2 to $var1
Notation for shorthand operator is :
$var1-=$var2;
So I have 2 variables, var1, var2.
$var1 = "53,000,000" //- integer
$var2 = 10 //- string
In the end I want to compare both, so I
$var1 = (int)str_replace(",","",$var1); // => 53000000 - integer
Here's my issue .. if I do:
if($var1 > $var2)
$var2 = $var1
I get $var2 = 0 .... Why ?
.. running on PHP 5.2.14
EDIT Accidentally typed in substr_replace instead of str_replace. Updated.
I had to add a couple semicolons, but here's the code:
$var1 = "53,000,000"; //- integer
$var2 = 10; //- string
//In the end I want to compare both, so I
$var1 = (int)str_replace(",","",$var1); // => 53000000 - integer
//Here's my issue .. if I do:
if($var1 > $var2)
$var2 = $var1;
var_dump($var1, $var2);
And here's my output:
int(53000000) int(53000000)
I used 5.2.6, but it shouldn't matter. Do you have any other code in between what you're showing?
Use str_replace() instead of substr_replace().
You've specified the wrong parameters for substr_replace, so $var1 is evaluated to 0. I guess you wanted to use str_replace.
No need for type casting. just do the str_replace
Here is code
$var1 = "53,000,000" ;
$var2 = 10;
$var1=str_replace(',','',$var1);
if($var1 > $var2)
$var2 = $var1;
echo $var2;