PHP flow control if else syntax
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)時(shí)執(zhí)行,只能寫(xiě)一行代碼; ?> <?php if(布爾條件) 布爾值為真(true)時(shí)執(zhí)行,只能寫(xiě)一行代碼; else 布爾值為假(false)時(shí)執(zhí)行,只能寫(xiě)一行代碼; ?>
<?php if(布爾條件){ 布爾值為真(true)時(shí)執(zhí)行,可寫(xiě)多行代碼; } ?> <?php if(布爾條件){ 布爾值為真(true)時(shí)執(zhí)行,可寫(xiě)多行代碼; }else{ 布爾值為假(false)時(shí)執(zhí)行,可寫(xiě)多行代碼; } ?>Many people like to buy lottery tickets. Let’s use the process of buying lottery tickets to write an example of if.
<?php //定義一下中獎(jiǎng)變量,變量的值為true,表示中獎(jiǎng)了 $zhongjiang = true; //由于$zhongjiang 結(jié)果為true,所以顯示了:“買(mǎi)個(gè)房子” //可以改為false試試執(zhí)行結(jié)果,如果為false的話,不會(huì)執(zhí)行echo '買(mǎi)個(gè)房子'; if($zhongjiang){ echo '買(mǎi)個(gè)房子'; } //后續(xù)代碼 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 //我們定義一個(gè)泡到美女的變量($pao)為false,意思為沒(méi)泡到 $pao = false; if($pao) //你可以試試在這兒寫(xiě)多行代碼會(huì)不會(huì)報(bào)錯(cuò)。 echo '我愿意去死'; else echo '我不愿意去死'; //if...else執(zhí)行結(jié)束,后續(xù)代碼 ?> 在if...In
else, we write another example that can be enclosed in curly brackets and have multiple sentences:
<?php
//我們定義一個(gè)泡到美女的變量($pao)為true,意思為泡到了
$pao = true;
if($pao){
echo '我愿意去死';
echo '林志玲,我愛(ài)死你了。';
}else{
echo '我不愿意去死';
echo '鳳姐,我肯定不會(huì)愛(ài)你的';
}
//if...else執(zhí)行結(jié)束,后續(xù)代碼
?>