Home > Backend Development > PHP Tutorial > PHP tips to make using the ternary operator easier

PHP tips to make using the ternary operator easier

巴扎黑
Release: 2023-03-14 18:30:02
Original
1478 people have browsed it

Today I discovered a small usage of the PHP ternary operator. This gave my dry brain a little fun!

PHP ternary operator is a concise main usage when assigning values ​​to parameters. . One major usage: The PHP ternary operator allows you to describe the decision code in one line of code, thereby replacing code similar to the following:

<?php
if (isset($value)) {  
  $output = $value;
} else {   
 $output = &#39;No value set.&#39;;
}
Copy after login
Copy after login

Use the following code to replace:

<?php
$output = isset($value) ? $value : &#39;No value set.&#39;;
Copy after login
Copy after login

The two code examples are very concise usage, and in many situations (not all), this is a very practical usage. There is a lot of debate about whether you should use the ternary operator; let me say, this is a tool, Like other tools, it just needs to be used correctly.

The commonly used syntax is (expression)? value if truthy: value if falsy. This expression can be a variable, test whether the variable is true or false:

<?php$output = $value ? $value : &#39;No value set.&#39;;
Copy after login

The problem is: the above example is very common and annoyingly repetitive: writing $value twice feels like a mistake.

Fortunately, I found out today that PHP 5.3 introduced a more concise syntax using the ternary operator. You can learn it from the manual, but here how do we make the above example more concise:

<?php
$output = $value ?: &#39;No value set.&#39;;
Copy after login
Copy after login

Look at this It looks familiar, because it is very similar to other abbreviation operators:

<?php
$value = $value . $other_value;
Copy after login
Copy after login

converts to:

<?php
$value .= $other_value;
Copy after login
Copy after login

For the sake of simplicity, this means that we can abbreviate this way but it does not mean that we canshould be written like this. However, when we write concise code, this way will look clearer, we should be written like this, (and this feature allows us to Even shorter ternary operators in PHP using ?:

Today I discovered a small usage of the PHP ternary operator. This gave my dry brain a little fun!
PHP ternary operator is a concise way to assign values ​​to parameters. Main usage. A main usage: PHP ternary operator allows you to describe the judgment code in one line of code, thereby replacing code similar to the following:

<?php
if (isset($value)) {  
  $output = $value;
} else {   
 $output = &#39;No value set.&#39;;
}
Copy after login
Copy after login

Use the following code to replace:

<?php
$output = isset($value) ? $value : &#39;No value set.&#39;;
Copy after login
Copy after login

The second code example is a very concise usage, and in many cases (not all), it is a very practical usage. There are many debates about whether you should use the ternary operator; let me say, this is one Tools are like other tools, they just need to be used correctly or not.

The commonly used syntax is (expression)? value if truthy: value if falsy. This expression can be a variable, test whether the variable is true or false False:

<?php
$output = $value ? $value : &#39;No value set.&#39;;
Copy after login

The problem is: the above example is very common and annoyingly repetitive: writing $value twice seems like a mistake.

Fortunately, I found out today that

PHP 5.3

introduced a more concise syntax using the ternary operator. You can learn it from the manual, but here how do we make the above example more concise:

<?php
$output = $value ?: &#39;No value set.&#39;;
Copy after login
Copy after login

This one looks familiar, and that's because it's very similar to other abbreviation operators:

<?php
$value = $value . $other_value;
Copy after login
Copy after login
converts to:
<?php
$value .= $other_value;
Copy after login
Copy after login
For simplicity, this means we can abbreviate it like this but it doesn't mean we Just

should

be written like this. However, when we write concise code, this way will look clearer, we

should

be written like this,

(and this feature allows us Use this operator in many cases[this feature allows us to DRY up the ternary operator in many cases])

The above is the detailed content of PHP tips to make using the ternary operator easier. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template