PHP Shorthand if/else if - Basic

Asked
Active3 hr before
Viewed126 times

7 Answers

basicshorthand
90%

You're looking for ternary statements.,Syntax for nested ternary statements:,Just a warning, nested ternary statements are hard to read, so it's recommended to unnested ternary statements., 11 Ugh, don't ever recommend nested ternary statements, that make code so hard to read – Brandon Wamboldt Jun 18 '14 at 13:05

Syntax:

$condition ? $value_if_true : $value_if_false
load more v
88%

Ternary operator logic is the process of using "(condition) ? (true return value) : (false return value)" statements to shorten your if/else structures.,The ternary operator IS NOT the same as if/else; a ternary operator assures that a variable is given an assignment.,If you code for longer, you start to let go of niftyness and look more towards readability. Using ternary operator wrong, indicates to me a lack of experience and/or youthful enthusiasm.,I preach a lot about using shorthand CSS and using MooTools to make JavaScript relatively shorthand, so I look towards PHP to do the same. If/Else statements aren't optimal (or necessary) in all situations. Enter ternary operators.

/* most basic usage */
$var = 5;
$var_is_greater_than_two = ($var > 2 ? true : false); // returns true

Here are a couple more uses of ternary operators, ranging from simple to advanced:

 /* another basic usage */
 $message = 'Hello '.($user - > is_logged_in() ? $user - > get('first_name') : 'Guest');
 /* shorthand usage */
 $message = 'Hello '.($user - > get('first_name') ? : 'Guest');
 /* echo, inline */
 echo 'Based on your score, you are a ', ($score > 10 ? 'genius' : 'nobody'); //harsh!
 /* 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'
 /* "thankfully-you-don't-need-to-maintain-this" 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
load more v
72%

The elseif statement is only executed if the preceding if expression and any preceding elseif expressions evaluated to false, and the current elseif expression evaluated to true. , There may be several elseifs within the same if statement. The first elseif expression (if any) that evaluates to true would be executed. In PHP, you can also write 'else if' (in two words) and the behavior would be identical to the one of 'elseif' (in a single word). The syntactic meaning is slightly different (if you're familiar with C, this is the same behavior) but the bottom line is that both would result in exactly the same behavior. , Alternative syntax for control structures ,Note: Note that elseif and else if will only be considered exactly the same when using curly brackets as in the above example. When using a colon to define your if/elseif conditions, you must not separate else if into two words, or PHP will fail with a parse error.

(PHP 4, PHP 5, PHP 7, PHP 8)

elseif, as its name suggests, is a combination of if and else. Like else, it extends an if statement to execute a different statement in case the original if expression evaluates to false. However, unlike else, it will execute that alternative expression only if the elseif conditional expression evaluates to true. For example, the following code would display a is bigger than b, a equal to b or a is smaller than b:

elseif

(PHP 4, PHP 5, PHP 7, PHP 8)

elseif, as its name suggests, is a combination of if and else. Like else, it extends an if statement to execute a different statement in case the original if expression evaluates to false. However, unlike else, it will execute that alternative expression only if the elseif conditional expression evaluates to true. For example, the following code would display a is bigger than b, a equal to b or a is smaller than b:

if

(PHP 4, PHP 5, PHP 7, PHP 8)

elseif, as its name suggests, is a combination of if and else. Like else, it extends an if statement to execute a different statement in case the original if expression evaluates to false. However, unlike else, it will execute that alternative expression only if the elseif conditional expression evaluates to true. For example, the following code would display a is bigger than b, a equal to b or a is smaller than b:

else

(PHP 4, PHP 5, PHP 7, PHP 8)

elseif, as its name suggests, is a combination of if and else. Like else, it extends an if statement to execute a different statement in case the original if expression evaluates to false. However, unlike else, it will execute that alternative expression only if the elseif conditional expression evaluates to true. For example, the following code would display a is bigger than b, a equal to b or a is smaller than b:

else

(PHP 4, PHP 5, PHP 7, PHP 8)

elseif, as its name suggests, is a combination of if and else. Like else, it extends an if statement to execute a different statement in case the original if expression evaluates to false. However, unlike else, it will execute that alternative expression only if the elseif conditional expression evaluates to true. For example, the following code would display a is bigger than b, a equal to b or a is smaller than b:

if

(PHP 4, PHP 5, PHP 7, PHP 8)

elseif, as its name suggests, is a combination of if and else. Like else, it extends an if statement to execute a different statement in case the original if expression evaluates to false. However, unlike else, it will execute that alternative expression only if the elseif conditional expression evaluates to true. For example, the following code would display a is bigger than b, a equal to b or a is smaller than b:

if

(PHP 4, PHP 5, PHP 7, PHP 8)

elseif, as its name suggests, is a combination of if and else. Like else, it extends an if statement to execute a different statement in case the original if expression evaluates to false. However, unlike else, it will execute that alternative expression only if the elseif conditional expression evaluates to true. For example, the following code would display a is bigger than b, a equal to b or a is smaller than b:

else

(PHP 4, PHP 5, PHP 7, PHP 8)

elseif, as its name suggests, is a combination of if and else. Like else, it extends an if statement to execute a different statement in case the original if expression evaluates to false. However, unlike else, it will execute that alternative expression only if the elseif conditional expression evaluates to true. For example, the following code would display a is bigger than b, a equal to b or a is smaller than b:

elseif
load more v
65%

Search Answer Titles

< ? php
$v = 1;

$r = (1 == $v) ? 'Yes' : 'No'; // $r is set to 'Yes'
$r = (3 == $v) ? 'Yes' : 'No'; // $r is set to 'No'

echo(1 == $v) ? 'Yes' : 'No'; // 'Yes' will be printed

// and since PHP 5.3
$v = 'My Value';
$r = ($v) ? : 'No Value'; // $r is set to 'My Value' because $v is evaluated to TRUE

$v = '';
echo($v) ? : 'No Value'; // 'No Value' will be printed because $v is evaluated to FALSE
?
>
load more v
75%

if statement - executes some code if one condition is true,The if statement executes some code if one condition is true.,if...else statement - executes some code if a condition is true and another code if that condition is false,The if...else statement executes some code if a condition is true and another code if that condition is false.

$a = 50;
$b = 10; >
{
   echo "Hello World";
}
40%

This article will introduce how we can use the short-hand method to write the if...else condition in PHP. We will use the ternary operator and null coalescing operator with the demonstrations.,Use the Ternary Operator for true/false Declaration in PHP,Let’s see how we can write equivalent ternary operation of the if-else condition. Let’s have a look at the following if-else condition.,The output section shows the output as false, which is a boolean type. In this way, we can use the ternary operator to declare the true and false values in PHP.

We can use the ternary operator as a short-hand method of if-else condition in PHP. However, the ternary operator can be used in every other programming language. The word ternary means having three elements. So, the ternary operator has three operands. We write the condition at first, which is the first operand. Then the symbol ? separates the condition with the expression to be evaluated if the condition is true. Lastly, the : symbol separates the truthy and the falsy expression. The syntax is written below.

condition ? trueExpression : falseExpression
load more v
22%

Using the ternary operator the same code could be written in a more compact way:,Tip: Code written using the ternary operator can be hard to read. However, it provides a great way to write compact if-else statements.,The ternary operator provides a shorthand way of writing the if...else statements. The ternary operator is represented by the question mark (?) symbol and it takes three operands: a condition to check, a result for true, and a result for false.,Using the null coalescing operator the same code could be written as:

< ? php
$d = date("D");
if ($d == "Fri") {
   echo "Have a nice weekend!";
} ?
>
load more v