IF/ELSE PHP shorthand [duplicate] - php

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

Related

How to use ternary operators if-elseif-else in php? [duplicate]

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.

What is the use of logical operator with string [duplicate]

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

ternary operator with ampersand [duplicate]

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"};

I came across this sign in PHP " ? 0 : ".What does it mean? [duplicate]

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';

PHP Programming Syntax Question 'x ? x : x;' [duplicate]

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;

Categories