Home >Backend Development >PHP Tutorial >Please look at this php code

Please look at this php code

WBOY
WBOYOriginal
2016-07-06 13:53:11869browse

This code, when entering the first if, will judge $ch $en $ma>220, but if $ch $en $ma<220, the event stream will jump out of this if and execute the following else ?

<code><?php 
$ch=60;
$ma=62;
$en=65;

if($ch>60&&$ma>60&&$en>60)
{
    if($ch+$en+$ma>220)
    {$tg='合格';};
}
else{
$tg='不合格';
}
?>
<html>
<p><?php echo $tg; ?></p>
</html>


                            
                        </p>


                                                                                                                        
                     <h2>Reply content: </h2>
                      
                                                            
<p>This code, when entering the first if, will judge $ch $en $ma>220, but if $ch $en $ma<220, the event stream will jump out of this if and execute the following else ? </p>
<pre class="brush:php;toolbar:false"><code><?php 
$ch=60;
$ma=62;
$en=65;

if($ch>60&&$ma>60&&$en>60)
{
    if($ch+$en+$ma>220)
    {$tg='合格';};
}
else{
$tg='不合格';
}
?>
<html>
<p><?php echo $tg; ?></p>
</html>


                            
                        
            </p>
<p class="answer fmt" data-id="1020000005744429">
                                    
</p>
<p>1. Only if...else in the same team will jump based on whether the condition in the if is met, different ones will not, even if they are nested. So when the nested if condition in your if is not met, it will look for an else that matches it. Sorry, there is no matching else here, so $tag will be empty. <br>2. Pay attention to the writing format, the readability of the code, the indentation, the position of the curly brackets immediately after the judgment condition, and there must be spaces on both sides of the operator. </p>
<pre class="brush:php;toolbar:false"><code><?php 
$ch = 60;
$ma = 62;
$en = 65;

if($ch > 60 && $ma > 60 && $en>60)
{
    if($ch + $en+ $ma > 220){
        $tg = '合格';
    }else{
        $tg = '不合格';
    }
}
else{
    $tg = '不合格';
}
?>
<html>
<p><?php echo $tg; ?></p>
</html></code>

Of course, xiayongsheng’s way of writing is more concise, but slightly less readable.

The following else is the first if to judge 60, and the if inside does not have an else, that is to say, tg is empty
It can be modified as follows

<code><?php 
$ch=60;
$ma=62;
$en=65;
# 我觉得你漏了等于这个情况,所以加上了
$condition = ($ch+$en+$ma) >= 220 && $ch >= 60 && $ma >= 60 && $en >= 60;
$tg = $condition ? '及格' : '不及格';
?></code>

<code>if(明天下雨){
   // 伞或雨衣
   if(骑车) {
      // 雨衣
   } 
} else {
   // 不带
}</code>

The premise of a raincoat is that it rains.

if and else appear in pairs (but else is not necessary).
The if at the outermost level corresponds to the else at the outermost level, and so on.

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