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.
Related
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
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:
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';
This question already has answers here:
How to replace "if" statement with a ternary operator ( ? : )?
(16 answers)
Closed 9 years ago.
I'm struggling the past couple of hours to write in shorthand the PHP conditional statement below:
public static $url = $_SERVER['HTTP_REFERER'];
if (false !== strpos($url,'en')) {
$currlang = 'en';
} else {
$currlang = 'fr';
}
I can't figure out how to do this although I have tried many variations and I've also read online examples. Can you please help?
$currlang = false !== strpos($url, 'en') ? 'en' : 'fr';
PHP Manual: Ternary Operator
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
What is the PHP ? : operator called and what does it do?
I've been programming PHP for years, but have never understood what this syntax does or means. I'm hoping you guys can explain it to me, it's about time I knew the answer:
list($name, $operator) = (strpos($key, '__')) ? explode('__', $key) : array($key, null);
Specifically, I'm curious about the SOMETHING ? SOMETHING : SOMETHING;
It's shorthand for if() { } else {}.
if($i == 0) {
echo 'hello';
} else {
echo 'byebye';
}
is the same as:
echo $i == 0 ? 'hello' : 'byebye';
The first statement after '?' is executed if the first expression before '?' is true, if not the last is executed. It also evaluates to the value of the executed expression.
Its conditional operator just like if in simple words if in one line
(condition) ? statement1 : statement2
If condition is true then execute statement1 else statement2
this is the pure if else tertiary operation
if(a==b) {
c = 3;
} else {
c = 4;
}
this is same as
c = (a==b) ? 3:4;