I'm still getting used to ternary operators and I find it a helpful way of minimizing code. I can make sense out of it if it is simple like the example showed below (example 1)
Example 1
$OrderType = ($name == 'first' ? 'Fred' : ($name == 'last' ? 'Dabo' : 'RAND()'))
This can be read as: if $name is 'first' then use 'Fred' else if $name is 'last' then use 'Dabo' else use 'RAND()'
However I saw this (example 2) on another website and it doesn't make any sense to me.
Example 2
$score = 10;
$age = 20;
echo 'Taking into account your age and score, you are: ',($age > 10 ? ($score < 80 ? 'behind' : 'above average') : ($score < 50 ? 'behind' : 'above average')); // returns 'You are behind'
So can someone explain to me in simple language how this ternary operator will read?
In simple language, that ternary says, if $age > 10, consider 80 a good score, otherwise consider 50 a good score.
Rather than nesting ternaries, consider breaking out the nested logic into its own helper function. I find the following code much more understandable.
function adult_score($score) {
return $score > 80 ? "behind" : "above average";
}
function child_score($score) {
return $score < 50 ? "behind" : "above average";
}
$score = 10;
$age = 20;
echo $age > 10 ? adult_score($score) : child_score($score);
So, you understand a ternary, part before the ? is the if statement, part between ? and : is the "to do if truthy" and the part after : is the "to do if falsey". So if you took that ternary and wrapped the "if" part with an if statement and wrapped the "true" and "false" with curly braces and replaced the : with an else, you end up with this:
if($age > 10){
if($score < 80){
return 'behind';
} else {
return 'above average';
}
} else {
if($score < 50){
return 'behind';
} else {
return 'above average';
}
}
Like others have said though, it is ugly and hard to follow. Unless your goal was to make it hard for others to follow your code, then don't do this. Just please, don't. They are nice for one off if statements, but get confusing fast. Do yourself and any future readers of your code a favor.
Related
I'm a fan if the short if-version, example:
($thisVar == $thatVar ? doThis() : doThat());
I'd like to cut out the else-statement though, example:
($thisVar == $thatVar ? doThis());
However, it wont work. Is there any way to do it that I'm missing out?
You can't use it without the else. But you can try this:
($thisVar != $thatVar ?: doThis());
or
if ($thisVar == $thatVar) doThis();
The ternary operator is designed to yield one of two values. It's an expression, not a statement, and you shouldn't use it as a shorter alternative to if/else.
There is no way to leave out the : part: what value would the expression evaluate to if you did?
If you're calling methods with side effects, use if/else. Don't take short cuts. Readability is more important than saving a few characters.
hmm interesting, because executing the below code is valid. Observe:
for ($i = 1; $i <=10; $i++) {
if ($i % 2) {
echo $i;
}
}
The above code indeed, will output 13579
Notice no 'else' clause was used in the above.
If you wanted to inform the user of whether $i % 2 == FALSE ($i's divisor yielded remainder 0), you could include an else clause to print out the even numbers like shown below:
for ($i = 1; $i <=10; $i++) {
if ($i % 2) {
echo "$i is odd";
echo "<br />";
} else {
echo "$i is even";
echo "<br />";
}
}
Giving you the output:
1 is odd
2 is even
3 is odd
4 is even
5 is odd
6 is even
7 is odd
8 is even
9 is odd
10 is even
I hope my amazingly easy to understand examples will help all newcomers to PHP, hands down the 'best' server-side scripting language for building dynamic web applications :-)
USE NULL TO SKIP STATEMENTS WHEN IT IS IN SHORTHAND
$a == $b? $a = 10 : NULL;
Just use logical operators : AND, OR, &&, ||, etc.
($thisVar === $thatVar) && doThis();
a frequent use is :
$obj = doSomething($params) or throw new \Exception('Failed to do');
Working for me:
$leftHand != $rightHand?doThis():null;
$leftHand == $rightHand?null:doThis();
This question already has answers here:
Reference Guide: What does this symbol mean in PHP? (PHP Syntax)
(24 answers)
Closed 8 years ago.
I am new to Laravel framework(i.e a framework driven from Symfony library) .I was looking at the code of Symfony and came across some short cuts that i think i can use to improve my ability to code neatly.
I want to know about the behavior of these operators? ( ?: etc)
1- $this->history = $history ?: new History();
//Does this mean create object of History class and store in $this->history?
2- $this->maxRedirects = $maxRedirects < 0 ? -1 : $maxRedirects;
3- $this->followRedirects = -1 != $this->maxRedirects;
//Not sure how operators are behaving?i know this is something to do with regex but want to know the logic.
I would appreciate if somebody could post a link of tutorial about regex programming in php like above.
The first and the second use the ternary operator:
condition ? code_if_true : code_if_false
Note that code_if_true or code_if_false can be empty.
the third assigns to the variable the result of the test: -1 != $this->maxRedirects
So, true or false.
1) $this->history = $history ?: new History();
if $history has a value equivalent to false the history attribute is set to a new instance of the class History. If $history has a value equivalent to true, the history attribute is set to the $history value.
2) $this->maxRedirects = $maxRedirects < 0 ? -1 : $maxRedirects;
if $maxRedirects is negative the maxRedirects attribute is set to -1, otherwise it is set to $maxRedirects.
3) $this->followRedirects = -1 != $this->maxRedirects;
if $this->maxRedirects is different from -1, $this->followRedirects is set to true, otherwise false.
The best way to understand it is to look at only one page:
http://php.net/manual/en/language.operators.comparison.php
I think that to work with Symfony you need be more experienced developer. Try to learn PHP first, then you can try to learn how to work with frameworks written on PHP. Also I think that before you will start to learn frameworks you should read some books about Design Patterns.
1) and 3) are known as a ternary. I.e.
echo $isFoo ? "Is foo" : "No foo for you!";
Will echo the "Is foo" if foo is true and "No foo for you!" otherwise.
Since PHP 5.3 you can omit the middle:
echo $fooLabel ?: "Default foo label";
This will show $fooLabel if it is true and "Default foo label" otherwise.
Finally 3)
$this->followRedirects = -1 != $this->maxRedirects;
This simply evaluates -1 != $this->maxRedirects. This will be true if maxRedirects is not equal to -1. The result is then stored in $this->followRedirects.
The if-else shorthand follows the following format:
<condition> ? <condition is met> : <condition is not met>
This:
$age = 20;
echo $age >= 21 ? 'Have a beer' : 'Too young! No beer for you!';
Is the same as this:
if($age >= 21){
echo 'Have a beer';
}else{
echo 'Too Young! No beer for you!';
}
Your example #1, since PHP 5.3, simply omits the first condition of the shorthand and executes new History() only if the condition is not met:
$this->history = $history ?: new History();
Note you can also omit the second condition if you'd like. Also, if the omitted portion of the shorthand is met, 1 is returned, since no other instruction is given.
I thinks it's best to illustrate how these operators work by example:
1)
$this->history = $history ?: new History();
Is equal to
if ($history) {
$this->history = $history;
} else {
$this->history = new History();
}
2)
$this->maxRedirects = $maxRedirects < 0 ? -1 : $maxRedirects;
Is equal to
if ($maxRedirects < 0) {
$this->maxRedirects = -1;
} else {
$this->maxRedirects = $maxRedirects;
}
3)
$this->followRedirects = -1 != $this->maxRedirects;
Is equal to
$this->followRedirects = (-1 != $this->maxRedirects);
Is equal to
if (-1 != $this->maxRedirects) {
$this->followRedirects = true;
} else {
$this->followRedirects = false;
}
I'd like to to some thing similar to JavaScript's
var foo = true;
foo && doSometing();
but this doesn't seem to work in PHP.
I'm trying to add a class to a label if a condition is met and I'd prefer to keep the embedded PHP down to a minimum for the sake of readability.
So far I've got:
<?php $redText='redtext ';?>
<label class="<?php if ($requestVars->_name=='')echo $redText;?>labellong">_name*</label>
<input name="_name" value="<?php echo $requestVars->_name; ?>"/>
but even then the IDE is complaining that I have an if statement without braces.
use the ternary operator ?:
change this
<?php if ($requestVars->_name == '') echo $redText; ?>
with
<?php echo ($requestVars->_name == '') ? $redText : ''; ?>
In short
// (Condition)?(thing's to do if condition true):(thing's to do if condition false);
You can use Ternary operator logic
Ternary operator logic is the process of using "(condition)? (true return value) : (false return value)" statements to shorten your if/else structures. i.e
/* most basic usage */
$var = 5;
$var_is_greater_than_two = ($var > 2 ? true : false); // returns true
Something like this?
($var > 2 ? echo "greater" : echo "smaller")
I like to use the minimalist PHP text output syntax:
HTML stuff <?= $some_string ?> HTML stuff
(This works the same as using an <?php echo $some_string; ?>)
You can also use the ternary operator:
//(condition) ? (do_something_when_true) : (do_something_when_false);
($my_var == true) ? "It's true" : "It's false ;
Ending up like this:
<?= ($requestVars->_name=='') ? $redText : '' ?>
Sample Usage
Here are a couple more uses of ternary operators, ranging from simple to advanced:
Basic Usage:
$message = 'Hello '.($user->is_logged_in() ? $user->get('first_name') : 'Guest');
Short hand Usage:
$message = 'Hello '.($user->get('first_name') ?: 'Guest');
Echo Inline
echo 'Based on your score, you are a ',($score > 10 ? 'genius' : 'nobody');
A bit Tougher
$score = 10;
$age = 20;
echo 'Taking into account your age and score, you are: ',($age > 10 ? ($score < 80 ? 'behind' : 'above average') : ($score < 50 ? 'behind' : 'above average')); // returns 'You are behind'
complicated level
$days = ($month == 2 ? ($year % 4 ? 28 : ($year % 100 ? 29 : ($year %400 ? 28 : 29))) : (($month - 1) % 7 % 2 ? 30 : 31)); //returns days in the given month
To learn more about ternary operators and usage, visit PHP.net Comparison Operators or here.
Use ternary operator:
echo (($test == '') ? $redText : '');
echo $test == '' ? $redText : ''; //removed parenthesis
But in this case you can't use shorter reversed version because it will return bool(true) in first condition.
echo (($test != '') ?: $redText); //this will not work properly for this case
Ill provide with an other answer since the original question specifies the use of if() in html
<a class="menu-item" href="/about-us"><?= (pll_current_language() == 'en') ? 'About us' : 'Om oss' ?></a>
The provided answers are the best solution in your case, and they are what I do as well, but if your text is printed by a function or class method you could do the same as in Javascript as well
function hello(){
echo 'HELLO';
}
$print = true;
$print && hello();
I am trying to convert the following code into a Ternary Operator, but it is not working and I am unsure why. I think my problem is that I do not know how to express the elseif operation in ternary format. From my understanding and elseif is performed the same way as an if operation by using the format : (condition) ? 'result'.
if ($i == 0) {
$top = '<div class="active item">';
} elseif ($i % 5 == 0) {
$top = '<div class="item">';
} else {
$top = '';
}
$top = ($i == 0) ? '<div class="active item">' : ($i % 5 == 0) ? '<div class="item">' : '';
$top = ($i == 0) ? '<div class="active item">' : (($i % 5 == 0) ? '<div class="item">' : '');
you need to add parenthesis' around the entire else block
The Ternary Operator doesn't support a true if... else if... else... operation; however, you can simulate the behavior by using the following technique
var name = (variable === 1) ? 'foo' : ((variable === 2) ? 'bar' : 'baz');
I personally don't care for this as I don't find it more readable or elegant. I typically prefer the switch statement.
switch (variable) {
case 1 : name = 'foo'; break;
case 2 : name = 'bar'; break;
default : name = 'bas'; break;
}
Too late probably to share some views, but nevertheless :)
Use if - else if - else for a limited number of evaluations. Personally I prefer to use if - else if - else when number of comparisons are less than 5.
Use switch-case where number of evaluations are more. Personally I prefer switch-case where cases are more than 5.
Use ternary where a single comparison is under consideration (or a single comparison when looping), or when a if-else compare is needed inside the "case" clause of a switch structure.
Using ternary is faster when comparing while looping over a very large data set.
IMHO Its finally the developer who decides the trade off equation between code readability and performance and that in turn decides what out of, ternary vs. if else-if else vs. switch-case, can be used in any particular situation.
//Use this format before reducing the expression to one liner
$var=4; //Change value to test
echo "Format result: ";
echo($var === 1) ? 'one' : //if NB.=> $varname = || echo || print || var_dump(ternary statement inside); can only be (placed at the start/wrapping) of the statement.
(($var === 2) ? 'two' : //elseif
(($var === 3) ? 'three' : //elseif
(($var === 4) ? 'four' : //elseif
'false' //else
))); //extra tip: closing brackets = totalnumber of conditions - 1
// Then echo($var === 1)?'one':(($var === 2)?'two':(($var === 3)?'three':(($var === 4)?'four':'false')));
echo "<br/>";
var_dump("Short result: ", ($var === 1)?'one':(($var === 2)?'two':(($var === 3)?'three':(($var === 4)?'four':'false'))) );
I wan to convert the following code from this
$diff = strtotime($row['start']) - strtotime($current);
if ($diff < 7200) {
echo 'Starts soon';
} else if ($diff <= 0) {
echo 'Started';
} else {
echo 'Starts';
}
to this ?
<?= ($current > $row['start']) ? 'Started' : 'Starts'; ?>
How can ( if possible ) be written in such way ?
It's not very readable, so I wouldn't use it but here you go:
echo ($diff < 7200) ? 'Starts soon': (($diff <= 0) ? 'Started': 'Starts');
That's not really pretty, but you can do it like this :
<?php
$diff = strtotime($row['start']) - strtotime($current);
echo ($diff < 7200 ? 'Start soon' : ($diff <= 0 ? 'Started' : 'Starts'));
?>
Or
<?= ((strtotime($row['start']) - strtotime($current)) < 7200 ? 'Start soon' : ((strtotime($row['start']) - strtotime($current)) <= 0 ? 'Started' : 'Starts')); ?>
There is nothing wrong with an if elseif statement covering a few lines. It makes it easy to read, easy to follow and easy to see what is happening if you are checking your code later - or more importantly if someone else is reading your code.
Remember, it is always easier to write code than it is to read is.
From the documents:
<?php
// on first glance, the following appears to output 'true'
echo (true?'true':false?'t':'f');
// however, the actual output of the above is 't'
// this is because ternary expressions are evaluated from left to right
// the following is a more obvious version of the same code as above
echo ((true ? 'true' : false) ? 't' : 'f');
// here, you can see that the first expression is evaluated to 'true', which
// in turn evaluates to (bool)true, thus returning the true branch of the
// second ternary expression.
?>
It's really not too advisable as it is hard to read and easy to mis-read.
else if can be applied when in else part You add new if.
<?= (($diff < 7200) ? "Starts soon" : (($diff <= 0) ? "Started" : "Starts")); ?>