Ternary and mod operator in php

Ternary and mod operator in php
Ternary and mod operator in php

Ternary and mod operator in php

Using the if-else and switch case is an essential part of programming for evaluating conditions. We always look for shortcuts everywhere whether it is a route for traveling or game or code. In this Ternary Operator PHP, we will see how it is used for shortening conditional statements.

What is Ternary operator?

The ternary operator is a conditional operator that decreases the length of code while performing comparisons and conditionals. This method is an alternative for using if-else and nested if-else statements. The order of execution for this operator is from left to right. Obviously, it is the best case for a time-saving option.

It also produces an e-notice while encountering a void value with its conditionals. It is called a ternary operator because it takes three operands – a condition, a result for true, and a result for false.

Syntax:

(Condition) ? (Statement1) : (Statement2);
  • Condition: It is the expression to be evaluated which returns a boolean value.
  • Statement 1: it is the statement to be executed if the condition results in a true state.
  • Statement 2: It is the statement to be executed if the condition results in a false state.

Example program to whether student is pass or fail:

=40) ? "pass" : "Fail";
?>

Output:

pass

When do we use Ternary Operator?

We use ternary operator when we need to simplify if-else statements that are used to assign values to variables. Moreover, it is commonly used when we assign post data or validate forms.

Let’s say, we were programming a login form for a college university where we wanted to ensure that the user entered their registration number provided by the university then we could move further.

//if the registration number is not specified, notify the customer
$reg_number = (isset($_POST['reg'])) ? $_POST['reg'] : die('Please enter your registration number');

Let’s look at an example of a validation form for better understanding:



In order to get the values of our text fields, we can use the following code:


Advantages of Ternary Operator

  • It will make the code shorter
  • It will make the code more readable
  • The code becomes simpler

Ternary shorthand

Short ternary operator syntax can be used by leaving out the middle part of the ternary operator for quick shorthand evaluation. It is also referred to as Elvis operatory(?

Ternary and mod operator in php

Syntax:

expression1 ?: expression2

Elvis operator can be used in order to reduce redundancy of your conditions and shorten the length of your assignments. It is the ternary operator with the second operand omitted. It will return the first operand if the operand is true else it evaluates and returns its second operand.

$val = $_GET['user'] ?: 'default';

If you use the ternary shorthand operator like this, it will cause notice if

$_GET['user']

is not set, instead of writing some lengthy code like this:

$val = isset($_GET['user']) ? $_GET['user'] : 'default';

Null Coalescing Operator

It replaces the ternary operation in conjunction with isset() function which is used to check whether a given variable is NULL or not and returns its first operand if it exists and is not NULL else it returns the second operand.

Syntax:

(Condition)?(Statement1)?(Statement2);
$user= $_GET['user'] ?? 'nobody';

It will fetch the value of $_GET[user] and returns ‘nobody’ if it does not exist.

Instead of writing some lengthy code like this:

$user= isset($_GET['user']) ? $_GET['user'] : 'nobody';

With this we come to an end of this article, I hope you understood the ternary operator, the purpose and advantages of the ternary operator, Ternary shorthand and Null coalescing Operator.

Got a question for us? Please mention it in the comments section of ”ternary operator in php” and I will get back to you.

Enjoy this post? Give Sayantini a like if it's helpful.

Ternary and mod operator in php
9

Share

A Data Science Enthusiast. Keen to work with technologies like Machine Learning, Artificial Intelligence & Deep Learning. If you ask about Programming Skills : "I don't C, I Python" ;)

Discover and read more posts from Sayantini

Enjoy this post?

Leave a like and comment for Sayantini

Is there a ternary operator in PHP?

The term "ternary operator" refers to an operator that operates on three operands. An operand is a concept that refers to the parts of an expression that it needs. The ternary operator in PHP is the only one that needs three operands: a condition, a true result, and a false result.

Is ternary operator faster than if in PHP?

The right answer is: it depends. Most of the time, they are about the same speed and you don't need to care. But if $context['test'] contains a large amount of data, snippet 2 is much faster than snippet 1.

What is a ternary operator with example?

The ternary operator is an operator that exists in some programming languages, which takes three operands rather than the typical one or two that most operators use. It provides a way to shorten a simple if else block. For example, consider the below JavaScript code. var num = 4, msg = ""; if (num === 4) {

What are the advantage of using ternary operator?

The only "advantage" is that you can use the ternary operator in an expression (eg. function arguments), making for terser code. using an if , you'd duplicate the full expression. Show activity on this post.