if and else syntax
This is a very important chapter and an important syntax in PHP.
[Note] My definition level of this grammar is: tacit writing level. That is, you can write it with your eyes closed if you need it.
Explanation in English:
*if * Pronunciation: [ɪf]
Explanation in Chinese: If
##else Pronunciation: [ɛls]Chinese explanation: Otherwise
<?php if(布尔条件) 布尔值为真(true)时执行,只能写一行代码; ?> <?php if(布尔条件) 布尔值为真(true)时执行,只能写一行代码; else 布尔值为假(false)时执行,只能写一行代码; ?>
<?php if(布尔条件){ 布尔值为真(true)时执行,可写多行代码; } ?> <?php if(布尔条件){ 布尔值为真(true)时执行,可写多行代码; }else{ 布尔值为假(false)时执行,可写多行代码; } ?>Many people like to buy lottery tickets. Let’s use the process of buying lottery tickets to write an example of if.
<?php //定义一下中奖变量,变量的值为true,表示中奖了 $zhongjiang = true; //由于$zhongjiang 结果为true,所以显示了:“买个房子” //可以改为false试试执行结果,如果为false的话,不会执行echo '买个房子'; if($zhongjiang){ echo '买个房子'; } //后续代码 echo '该干嘛干嘛'; ?>In the chapter "3.2.2 Boolean is the Knowledge of the Book of Changes", I gave you a wretched example:
For example, a sentence often mentioned in TV dramas : If I get that beautiful girl (handsome guy), I am willing to die.
Then: I am willing to die
If I don’t pick up a beautiful girl
Then: I am not willing to die This is it:
For the above example, we can completely use if...else... to translate it into code:
<?php //我们定义一个泡到美女的变量($pao)为false,意思为没泡到 $pao = false; if($pao) //你可以试试在这儿写多行代码会不会报错。 echo '我愿意去死'; else echo '我不愿意去死'; //if...else执行结束,后续代码 ?> 在if...In
else, we write another example that can be enclosed in curly brackets and have multiple sentences:
<?php
//我们定义一个泡到美女的变量($pao)为true,意思为泡到了
$pao = true;
if($pao){
echo '我愿意去死';
echo '林志玲,我爱死你了。';
}else{
echo '我不愿意去死';
echo '凤姐,我肯定不会爱你的';
}
//if...else执行结束,后续代码
?>