三元运算符和if else
Sometimes we need to perform different actions based on the decision, PHP provides several conditional statements that can be used for this.
有时我们需要根据决策执行不同的操作,PHP提供了一些可用于此的条件语句。
- If statement – its used when you need to execute the piece of code only when the condition is true. if语句–仅在条件为true时才需要执行代码时使用。
- If-Else statement – its used to execute a piece of code when the condition is true or another piece of code when condition is false. if-Else语句–用于在条件为true时执行一段代码,在条件为false时执行另一段代码。
- If-Else-If Statement – its used when there are multiple code that can be executed based on the condition, as soon as one of the condition is true and its code block is executed, control comes out of the statement. If-Else-If语句–当可以根据条件执行多个代码时使用,一旦条件之一为真且其代码块被执行,控制就从该语句中发出。
- Switch statement – Its same as if-else-if statement but it make code cleaner. Switch语句–与if-else-if语句相同,但是它使代码更清晰。
- Ternary Operator – Ternary operator provides a shorthand way to write all the above conditional statements. If the condition is not very complex, its better to use ternary operator to reduce the code size but for more complex conditions, it can become confusing. The syntax is
(Condition) ? <Condition=True>:<Condition=False>
三元运算符–三元运算符提供了一种编写上述所有条件语句的简便方法。 如果条件不是很复杂,最好使用三元运算符来减少代码大小,但是对于更复杂的条件,可能会造成混淆。 语法是(Condition) ? <Condition=True>:<Condition=False>
(Condition) ? <Condition=True>:<Condition=False>
Here is an example PHP script showing usage of all the conditional statement and using ternary operator to implement the same logic in very small size of code.
这是一个PHP脚本示例,显示了所有条件语句的用法,并使用三元运算符以非常小的代码大小实现了相同的逻辑。
<?php
$a=15;
$str="David";
$str1="Pankaj";
$color="Red";
//if condition example
if($a==15){
echo "value of a is 15";
echo "<br>";
}
//if-else condition example
if($str1 == "Pankaj"){
echo "Hi Pankaj";
echo "<br>";
}else {
echo "Hi There!";
echo "<br>";
}
//if-else-if example
if($str == "Pankaj"){
echo "Hi Pankaj";
echo "<br>";
}else if($str == "David"){
echo "Hi David";
echo "<br>";
} else{
echo "Hi There!";
echo "<br>";
}
//switch statement example
switch ($color){
case "Red":
echo "Red Color";
break; //for breaking switch condition
case "Green":
echo "Green Color";
break;
default:
echo "Neither Red or Green Color";
}
echo "<br>";
//PHP ternary operator example
// implementing the above if example with ternary operator
echo ($a == 15) ? "value of a is 15"."<br>":"";
// implementing the above if-else example with ternary operator
echo ($str1 == "Pankaj") ? "Hi Pankaj"."<br>" : "Hi There!"."<br>";
//implementing above if-else-if example with ternary operator
echo ($str == "Pankaj") ? "Hi Pankaj"."<br>" : (($str == "David") ? "Hi David"."<br>" : "Hi David"."<br>");
//implementing switch statement with ternary operator
echo ($color == "Red") ? "Red Color" : (($color == "Green") ? "Green Color" : "Neither Red or Green Color");
?>
Output of the above PHP script is:
上面PHP脚本的输出是:
value of a is 15
Hi Pankaj
Hi David
Red Color
value of a is 15
Hi Pankaj
Hi David
Red Color
Further reading: PHP Operators
进一步阅读: PHP运算符
翻译自: https://www.journaldev.com/1518/php-if-else-switch-case-and-shorthand-ternary-operator-example
三元运算符和if else