三元运算符和if else_PHP If-Else,Switch Case和速记三元运算符示例

三元运算符和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提供了一些可用于此的条件语句。

  1. If statement – its used when you need to execute the piece of code only when the condition is true.

    if语句–仅在条件为true时才需要执行代码时使用。
  2. 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时执行另一段代码。
  3. 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语句–当可以根据条件执行多个代码时使用,一旦条件之一为真且其代码块被执行,控制就从该语句中发出。
  4. Switch statement – Its same as if-else-if statement but it make code cleaner.

    Switch语句–与if-else-if语句相同,但是它使代码更清晰。
  5. 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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值