PHP 三元运算符
在本文中,我们将通过各种示例了解PHP三元运算符。
什么是三元运算符
它也被称为条件运算符。三元运算符是一种能够在执行比较时减少代码长度的运算符。它是使用if-else和嵌套if-else语句的替代方式。当需要简化if-else语句并将复杂的程序结构转化为高效代码时,我们可以使用三元运算符。
语法
以下是php中三元运算符的语法。
(Condition) ? (code1) : (code2);
在上述语法中,
- Condition: 它是一个PHP表达式,将被评估以返回一个布尔值。
- code1: 当条件结果为真时将执行的语句。
- code2: 当条件结果为假时将执行的语句。
PHP的流程图
示例1
<! Doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<title> Ternary Operator Example in PHP </title>
<style>
body {
color: #FFFFFF;
font-family: 'League Gothic', Impact, sans-serif;
text-transform: uppercase;
text-align: center;
font-size: 1em;
text-align: center;
line-height: 12px;
margin: 120px;
background: #282537;
background-image: -webkit-radial-gradient(top, circle cover, #3c3b52 0%, #252233 80%);
background-image: -moz-radial-gradient(top, circle cover, #3c3b52 0%, #252233 80%);
background-image: -o-radial-gradient(top, circle cover, #3c3b52 0%, #252233 80%);
background-image: radial-gradient(top, circle cover, #3c3b52 0%, #252233 80%);
}
h2 {
font-family: 'Indie Flower', cursive;
font-size: 32px;
color: #03A9F4;
font-weight: bold;
text-align: center;
padding: 10px 0;
}
</style>
<body>
<h2> Ternary Operator Example in PHP </h2>
</body>
</html>
<?php
a = 40;b = a>45 ? 20 : 5;
print ("<h2>Value of b is: " .b . "</h2>");
?>
解释:
在上面的示例中,我们设置了一个条件,如果条件为真,则显示20,否则显示5。
输出:
以下是这个示例的输出:
示例2
<! Doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<title> Ternary Operator Example in PHP </title>
<style>
body {
color: #FFFFFF;
font-family: 'League Gothic',Impact,sans-serif;
text-transform: uppercase;
text-align: center;
font-size: 1em;
text-align: center;
line-height: 12px;
margin: 120px;
background: #282537;
background-image: -webkit-radial-gradient(top, circle cover, #3c3b52 0%, #252233 80%);
background-image: -moz-radial-gradient(top, circle cover, #3c3b52 0%, #252233 80%);
background-image: -o-radial-gradient(top, circle cover, #3c3b52 0%, #252233 80%);
background-image: radial-gradient(top, circle cover, #3c3b52 0%, #252233 80%);
}
h2 {
font-family: 'Indie Flower', cursive;
font-size: 32px;
color: #03A9F4;
font-weight: bold;
text-align: center;
padding: 10px 0;
}
</style>
<body>
<h2> Ternary Operator Example in PHP </h2>
</body>
</html>
<?php
result = 62;
echo (result >= 40) ? "Passed" : " Failed";
?>
解释:
在上面的示例中,我们设置了一个条件,如果条件为真,则显示传递的消息,否则显示失败消息。
输出:
以下是此示例的输出:
示例3
<! Doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<title> Ternary Operator Example in PHP </title>
<style>
body {
color: #FFFFFF;
font-family: 'League Gothic',Impact,sans-serif;
text-transform: uppercase;
text-align: center;
font-size: 1em;
text-align: center;
line-height: 12px;
margin: 120px;
background: #282537;
background-image: -webkit-radial-gradient(top, circle cover, #3c3b52 0%, #252233 80%);
background-image: -moz-radial-gradient(top, circle cover, #3c3b52 0%, #252233 80%);
background-image: -o-radial-gradient(top, circle cover, #3c3b52 0%, #252233 80%);
background-image: radial-gradient(top, circle cover, #3c3b52 0%, #252233 80%);
}
h2 {
font-family: 'Indie Flower', cursive;
font-size: 32px;
color: #03A9F4;
font-weight: bold;
text-align: center;
padding: 10px 0;
}
</style>
<body>
<h2> Ternary Operator Example in PHP </h2>
</body>
</html>
<?php
age = 20;
print (age >= 18) ? "Adult" : "Not Adult";
?>
解释:
在上面的示例中,我们设定了一个条件,如果年龄大于,则显示成年人的消息,否则显示未成年人的消息。
输出:
以下是该示例的输出:
示例4
<! Doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<title> Ternary Operator Example in PHP </title>
<style>
body {
color: #FFFFFF;
font-family: 'League Gothic', Impact, sans-serif;
text-transform: uppercase;
text-align: center;
font-size: 1em;
text-align: center;
line-height: 12px;
margin: 120px;
background: #282537;
background-image: -webkit-radial-gradient(top, circle cover, #3c3b52 0%, #252233 80%);
background-image: -moz-radial-gradient(top, circle cover, #3c3b52 0%, #252233 80%);
background-image: -o-radial-gradient(top, circle cover, #3c3b52 0%, #252233 80%);
background-image: radial-gradient(top, circle cover, #3c3b52 0%, #252233 80%);
}
h2 {
font-family: 'Indie Flower', cursive;
font-size: 32px;
color: #03A9F4;
font-weight: bold;
text-align: center;
padding: 10px 0;
}
</style>
<body>
<h2> Ternary Operator Example in PHP </h2>
</body>
</html>
<?php
eligible = true;has_credit = false;
message =eligible
? (has_credit
? 'Eligible for credit card?
: 'Not eligible for credit card ')
: 'Not eligible to buy';
echomessage;
?>
说明:
在上面的示例中,我们设置了一个条件,如果条件为真,则显示符合申请信用卡的消息,否则显示不符合申请信用卡的消息。
输出:
以下是此示例的输出: