Home  >  Article  >  Backend Development  >  Detailed explanation of "if statement" examples of PHP conditional control statements

Detailed explanation of "if statement" examples of PHP conditional control statements

怪我咯
怪我咯Original
2017-04-13 09:57:464760browse

if语句的作用

在所有的编程语言中,例如C语言。JAVA等,都有if语句的存在,当然也包括PHP。if语句的作用就是按照条件选择执行不同的代码片段。

我们看一下if语句的格式

if(表达式)
       PHP语句;

如果表达式为真的话,,那么就执行PHP语句,否则就会跳过该语句,往下执行

如果执行的语句不只一条,我们可以使用“{}”包起来,像下面这样

if(表达式){
PHP语句1;
PHP语句2;
PHP语句3;
}

   在“{}”语句我们称为语句组。

看一下if语句的控制流程

Detailed explanation of if statement examples of PHP conditional control statements

下面我们看一下if语句的示例,代码如下

$num 是偶数";
}
?>

实例解释:

首先使用rand()函数生成一个1,50之间的数,然后判断这个数是不是偶数,如果是偶数,则输出结果,否则,什么也不输出。

Detailed explanation of if statement examples of PHP conditional control statements rand()函数的作用是取得一个随机的整数。

代码运行结果:

Detailed explanation of if statement examples of PHP conditional control statements

因为是随机生成的数,有时候不是偶数的话,是不会现在出现内容的。一般遇到这种情况的时候,我们会有一个当不满足条件的时候执行的PHP语句,这就需要我们下一节的内容了,下一节,我们给大家讲PHP条件控制语句中的“if...else”语句


The above is the detailed content of Detailed explanation of "if statement" examples of PHP conditional control statements. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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