PHP – If..Elseif..Else条件语句

[rps-include post=6522]

[rps-include post = 6522]

Php provides ability to write applications according to decision making with given statements or comparative tests. We can test given situations and conditions and dictate application execution. For example we want to check given integer value and print the positiveness and negativeness. Here we need to check this situation with if-elseif-else conditional statements.

Php可以根据给定的语句或比较测试根据决策编写应用程序。 我们可以测试给定的情况和条件并指示应用程序的执行。 例如,我们要检查给定的整数值并打印正负。 在这里,我们需要使用if-elseif-else条件语句来检查这种情况。

如果声明 (If Statement)

If statement will check given condition and if the condition is true if statement code block will be executed, if not it will be simply skipped. Here the if statement syntax.

if语句将检查给定条件,if条件将为真,如果将执行语句代码块,否则将被跳过。 这里是if语句的语法。

if(CONDITION){
CODE_BLOCK
}

I think the best way to understand the if statement is doing simple and clear example. We have a integer variables named $temp which holds temperature and we want to check whether the temperature if higher then  . If so we will print Temperature is above 0 .

我认为理解if语句的最佳方法是做一个简单明了的示例。 我们有一个名为$temp的整数变量,它保存温度,我们要检查温度是否高于。 如果是这样,我们将打印Temperature is above 0

$temp=5;

if($temp>0){
echo "Temperature is above 0";
}

Here most important part is $temp>0 where $temp value will be compared with comparison operator > and an boolean result like true will be returned.

这里最重要的部分是$temp>0 ,其中$ temp的值将与比较运算符>进行比较,并且将返回布尔值(如true

If..Elseif语句 (If..Elseif Statement)

In previous example we have check only one condition but real world is different and more complex than that. We generally check multiple conditions in a single if..elseif statement. This statement will start with if and then for each condition we will add an elseif statement like below.

在前面的示例中,我们仅检查了一种情况,但现实世界与此不同,并且更为复杂。 我们通常在单个if..elseif语句中检查多个条件。 该语句将以if开头,然后针对每个条件,我们将添加一个elseif语句,如下所示。

In this example we will check student name which is hold variable named $student . If the condition met we will print the name.

在此示例中,我们将检查学生姓名,即持有变量$student 。 如果条件满足,我们将打印名称。

$student = "Ali";

if($student == "Elif"){
echo "Elif";
}
elseif($student == "Ahmet"){
echo "Ahmet";
}
elseif($student == "Ali"){
echo "Ali";
}

As we expect last elseif statement condition will match and print Ali . Thre is no restriction about the count of additional elseif statements.

如我们所期望的, elseif语句条件将匹配并打印Ali 。 对其他elseif语句的数量没有限制。

If..Elseif..Else语句 (If..Elseif..Else Statement)

We can precisely specify the conditions and mathes. But what if we want to specify completely unmatched situation and run execute some code. We can use else statement at the end of the if..elseif statement block and this means run if none of above conditions matched.

我们可以精确地指定条件和数学。 但是,如果我们要指定完全不匹配的情况并运行,请执行一些代码。 我们可以在if..elseif语句块的末尾使用else语句,这意味着如果以上条件均不匹配,则运行。

LEARN MORE  C# Programming Language Null Reference Exception and Solution
了解更多C#编程语言Null参考异常和解决方案

In this example we will check $count and if none of the statements mathed we will print empty .

在这个例子中,我们将检查$count ,如果没有数学表达式,我们将输出empty

$count = 0;

if($ > 10){
echo "Good";
}
elseif($count > 0){
echo "Warning";
}
else{
echo "Empty";
}

[rps-include post=6522]

[rps-include post = 6522]

翻译自: https://www.poftut.com/php-if-elseif-else-conditional-statements/

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值