This question already has answers here:
corresponding nested ternary operator in php? [duplicate]
(2 answers)
Closed 6 years ago.
getting 2 different outputs while using the same currency 'egp'
$currency = ($q->currency == 'egp')? '£' : (($q->currency == 'usd') ? '$' : '€');
this line outputs $
$currency = ($q->currency == 'egp')? '£' : ($q->currency == 'usd') ? '$' : '€';
this one outputs £
and I can't find why?
note: the only difference is the () around the second ternary operator statement
Consider this code:
echo (true?"Left is first":(true?"Right is first":""));
Left is first
Versus
echo (true?"Left is first":true?"Right is first":"");
Right is first
The exaplanation can be found at http://php.net/manual/en/language.operators.comparison.php#language.operators.comparison.ternary.
In short, in the second case PHP will evaluate true?"Left is first":true as the condition for the ternary expression. This will evaluate to Left is first which evaluates to true and therefore Right is first will be echoed
Related
This question already has answers here:
PHP Elseif Ternary Operators
(4 answers)
Stacking Multiple Ternary Operators in PHP
(11 answers)
Closed 4 years ago.
I want to use ternary operator for if-elseif-else. But it is not working for me.
Kindly check the code below:
$openMon = "00:01"; // The below Line Returns Time
$openMon = "00:00"; // The below Line Returns Close
<td><?=(($openMon == '00:00')?'Close':date("g:i a", strtotime($openMon)));?></td>
The above line working perfectly because it returns True or false.
But when $openMon blank it returns 1am. I want when $openMon blank it returns "-"
I want Three Things in one line like:
$openMon = ""; // This is I want in ternary operator with above two conditions
if($openMon == "" ){
$openMon ="-";
}
I tried:
<td><?=isset((openMon == "")?'-':(($openMon == '00:00')?'Close':date("g:i a", strtotime($openMon)));?></td>
This is not working for me.
Basically I want in ternary operator:
if($openMon == ''){
$openMon = "-";
}else{
if($openMon == '00:00'){
$openMon = "Close";
}else{
$openMon = date("g:i a", strtotime($openMon)));
}
}
Any Idea or suggestions would be helpful.
You can use nested ternary operator.
Same question was here.
Your code will be like this:
$openMon = !$openMon ? "-" : ($openMon == '00:00' ? "Close" : date("g:i a", strtotime($openMon)))
Your can test this code snippet here.
This question already has an answer here:
What does ( !'which npm' ) mean in a PHP script?
(1 answer)
Closed 4 years ago.
I have used if(!$url) but I don't know the exact meaning of the below code in PHP
Can we check string with logical operator
if (! 'redirect cart'){
// data
}
Is it allow to use in a logical operation?
It equals to comparing to false:
if (false == 'redirect cart') { ...
Which could be true if your string is empty:
if (! '') { // this condition will be met
But this makes no sense to write this condition with a direct string, this should be a variable:
$str = my_function() ? 'redirect cart' : '';
if (!$str) { ...
The ! Operator negates the value of the expression. Code like this $value = (!true); will return false i.e $value will be false. The same happens when you apply the operator to an expression that returns false
This question already has answers here:
Can you pass by reference while using the ternary operator?
(5 answers)
Closed 7 years ago.
I use followed somewhere in my code:
if (isset($flat[$pid])) {
$branch = &$flat[$pid]['items'];
} else {
$branch = &$tree;
}
All ok, but when I want to short it to:
$branch = isset($flat[$pid]) ? &$flat[$pid]['items'] : &$tree;
I get:
syntax error, unexpected '&' ...
What I'm doing wrong?
This is because the ternary operator is an expression, so it doesn't evaluate to a variable. And a quote from the manual:
Note: Please note that the ternary operator is an expression, and that it doesn't evaluate to a variable, but to the result of an expression. This is important to know if you want to return a variable by reference. The statement return $var == 42 ? $a : $b; in a return-by-reference function will therefore not work and a warning is issued in later PHP versions.
This will work as alternative,
(isset($flat[$pid])) ? ($branch = &$flat[$pid]['items']) : ($branch = &$tree);
Edit:
The shortest it can go will be two lines,
#$temp = &$flat[$pid]['items'];
$branch = &${isset($flat[$pid]) ? "temp" : "tree"};
This question already has answers here:
PHP : Difference between '&&' and 'AND' [duplicate]
(1 answer)
'AND' vs '&&' as operator
(10 answers)
Closed 7 years ago.
[update]
This isn't a duplicate question of "what's the difference between AND and &&" rather it's a question regarding the use of AND and the ternary operator. anyway i think i got my answer i didn't know that all operators have precedence which alex kindly pointed out. http://php.net/manual/en/language.operators.precedence.php
[original]
echo TRUE && TRUE ? 'A' : 'B';
// Outputs: A
echo TRUE AND TRUE ? 'A' : 'B';
// Outputs: 1
I know that the ternary operator syntax is
condition ? true : false
and the difference between && and AND is evaluation precedence (of the condition)
e.g. TRUE AND TRUE && TRUE is evaluated at (TRUE AND (TRUE && TRUE))
so shouldn't using both AND and && yield the same return? is this a PHP bug? (im using ver5.5.23)
or should i just accept the fact that if i used AND in the condition it actually becomes excluded from the condition
e.g.
TRUE AND TRUE ? 'A' : 'B' => (TRUE AND (TRUE ? 'A' : 'B'))
This question already has answers here:
What is ?: in PHP 5.3? [duplicate]
(3 answers)
Closed 8 years ago.
This condition was found in this function:
$kValues = getValueCluster($clusters, $data);
foreach($cPos as $k => $position)
{
$cPos[$k] = empty($kValues[$k]) ? 0 : avg($kValues[$k]);
}
return $cPos
I have been trying to find out what this is. I've searched it in google and it has nothing on it.
... ? ... : ... is a ternary operator. It exists in a lot of languages. It's used like that:
variable = test ? assignIfTrue : assignIfFalse;
In your case, $cPos[$k] will be assigned to 0 if $kValues[$k] is empty and to avg($kValues[$k]) if not.
That's PHP's ternary operator.
Basic example:
$x = true;
$y = $x ? 'true!' : 'false';