Using if...else shorthand in PHP
This article will show you how we can use shorthand methods to write conditionals in PHP if...else
. We will use the ternary operator and the null coalescing operator in our demonstration.
Introduction to PHP ternary operator
We can use the ternary operator as if-else
a shorthand way of writing conditions in PHP. However, the ternary operator can be used in all other programming languages. The word ternary means having three elements. Hence, the ternary operator has three operands. We write the condition first, which is the first operand. Then the symbol _ ?
separates the condition from the expression to be evaluated if the condition is true. Finally, :
the _ separates the true and false value expressions. The syntax is written below.
condition ? trueExpression : falseExpression
Here, condition
is an expression to be evaluated. Option trueExpression
is an expression that condition
will be executed if is true, and if condition
is false, then falseExpression
will be executed.
Let's see how to write if-else
the equivalent of a ternary operation for a conditional. Let's look at the following if-else
conditional.
Sample code:
$num = rand(0,10);
echo "The number is: " . "$num"."<br>";
if($num>5){
echo "heads";
}else{
echo "tails";
}
Output:
The number is: 9
heads
Here, we use if-else
the conditional to display heads or tails using rand()
the function. If the number is greater than 5, we display heads and if the number is less than or equal to 5, we display tails. Here, rand(0,10)
a random number from 0
to is generated 10
. In the following example, the random number is 9 and the result is displayed as heads. An important point to consider is that we cannot echo
use the statement with the conditional operator itself. We can use them only after the condition has been evaluated to a result. We can replace the conditional by using the ternary operator with the above program if-else
.
Sample code:
$num = rand(0,10);
echo "The number is: " . "$num"."<br>";
echo ($num>5)? "heads":"tails";
Output:
The number is: 6
heads
In the following example, the random number is 6. It is greater than 5, so the program displays heads. if-else
The significant difference between the operator and the ternary operator is that we can use echo
the statement with the ternary operator. With this, we can use the ternary operator.
true/false
Using the ternary operator with the statement in PHP
We can also use the ternary operator to declare basic true
or false
values. We can use the Boolean values and in trueExpression
and respectively . For example, create a variable and assign it to the value of . Next, create another variable and write the ternary operation in the variable. Write the condition and write the values and as and . Finally, dump the variable using the function.falseExpression
true
false
$age
14
$can_vote
$age>17
true
false
trueExpression
falseExpression
var_dump()
$can_vote
The output section will display the output as , which is a Boolean type. In this way, we can use the ternary operator to declare and values false
in PHP .true
false
Code example:
$age= 14;
$can_vote = ($age>17 ? true : false);
var_dump($can_vote);
Output:
bool(false)
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Check if a Post exists in PHP
Publish Date:2025/04/13 Views:170 Category:PHP
-
PHP $_POST is a super global variable that can contain key-value pairs of HTML form data submitted through the post method. We will learn different ways to check $_POST if a and contains some data in this article. These methods will use iss
PHP with Ajax
Publish Date:2025/04/13 Views:139 Category:PHP
-
We will use PHP and ajax by printing a simple sum of two numbers 2 and . Also, print a php array in JSON. 3 object We will also use PHP with ajax by getting the HTML formatted output from the number division in PHP. Printing simple addition
Store Div Id in PHP variable and pass it to JavaScript
Publish Date:2025/04/13 Views:51 Category:PHP
-
This article shows you how to div id store a in a PHP variable and pass it to JavaScript code. We will answer the following questions. What is div id ? How to div id store in a PHP variable? How to pass variables to JavaScript code? Let’s
Returns the article tag with ID from the action page
Publish Date:2025/04/13 Views:80 Category:PHP
-
Let's say you're in a login form and you enter the wrong information; in this case, you probably want to go back to the login page. PHP has a built-in function header() to redirect a page to a specific page. But what if the login page is at
Switching PHP versions on Ubuntu
Publish Date:2025/04/13 Views:78 Category:PHP
-
Different tasks may require running multiple versions of PHP. You may need to switch PHP versions by running two sites on the same server or testing older versions of code using outdated methods. We can switch PHP versions on Ubuntu using t
Resizing images in PHP
Publish Date:2025/04/13 Views:155 Category:PHP
-
In this tutorial article, we will discuss about resizing images in PHP. Load the image before resizing Before we can resize an image, we must first load it as an image resource in our script. This is file_get_contents() different from using
PHP upload image
Publish Date:2025/04/13 Views:61 Category:PHP
-
We can upload images in PHP using simple file upload operation, but first, php.ini file upload should be enabled from Files. This tutorial demonstrates how to upload images in PHP. php.ini Enable file upload from file in PHP to upload image
Creating a signature from Hash_hmac() and Sha256 in PHP
Publish Date:2025/04/13 Views:107 Category:PHP
-
PHP has one of the best encryption functions for data security. Hash_hmac() The encrypt function is one of the most famous encryptors. We'll show you how to use hash_hmac and sha256 encryptors to create 安全签名 one that you can store i
Updating PHP 7.x to 7.4 on CentOS
Publish Date:2025/04/13 Views:131 Category:PHP
-
This article shows the steps to update the PHP version from 7.x version to 7.4 in CentOS. How to Update PHP from 7.X to 7.4 in CentOS Update operating system packages. yum update -y Check your PHP version in CentOS. php -v Prints a list of