PHP If...Else 语句
<div id="article_content" class="article_content clearfix"><link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/kdoc_html_views-1a98987dfd.css">
<link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/ck_htmledit_views-6e43165c0a.css">
<div id="content_views" class="htmledit_views">
<p>当您编写代码时,您常常需要为不同的判断执行不同的动作。您可以在代码中使用条件语句来完成此任务。</p>
<p>在 PHP 中,提供了下列条件语句:</p>
<ul><li><strong>if 语句</strong> - 在条件成立时执行代码</li><li><strong>if...else 语句</strong> - 在条件成立时执行一块代码,条件不成立时执行另一块代码</li><li><strong>if...elseif....else 语句</strong> - 在若干条件之一成立时执行一个代码块</li><li><strong>switch 语句</strong> - 在若干条件之一成立时执行一个代码块</li></ul>
<hr>
<h2>PHP - if 语句</h2>
<p>if 语句用于<strong>仅当指定条件成立时执行代码</strong>。</p>
<h3>语法</h3>
<pre>if (条件)
{
条件成立时要执行的代码;
}</pre>
<p>如果当前时间小于 20,下面的实例将输出 "Have a good day!":</p>
<h2>实例</h2>
<p><?php $t=date("H"); if ($t<"20") { echo "Have a good day!"; } ?></p>
<p><br><ahref="https://www.runoob.com/try/showphp.php?filename=demo_if">运行实例 »</a></p>
<p></p>
<hr>
<h2>PHP - if...else 语句</h2>
<p><strong>在条件成立时执行一块代码,条件不成立时执行另一块代码</strong>,请使用 if....else 语句。</p>
<h3>语法</h3>
<p>if (<em>条件</em>)<br> {<br><em>条件成立时执行的代码;</em><br> }<br> else<br> {<br><em>条件不成立时执行的代码;</em><br> }</p>
<p>如果当前时间小于 20,下面的实例将输出 "Have a good day!",否则输出 "Have a good night!":</p>
<h2>实例</h2>
<p><?php $t=date("H"); if ($t<"20") { echo "Have a good day!"; } else { echo "Have a good night!"; } ?></p>
<p><br><ahref="https://www.runoob.com/try/showphp.php?filename=demo_if_else">运行实例 »</a></p>
<p></p>
<hr>
<h2>PHP - if...elseif....else 语句</h2>
<p><strong>在若干条件之一成立时执行一个代码块</strong>,请使用 if....elseif...else 语句。.</p>
<h3>语法</h3>
<pre>if (条件)
{
if 条件成立时执行的代码;
}
elseif (条件)
{
elseif 条件成立时执行的代码;
}
else
{
条件不成立时执行的代码;
}</pre>
<p>如果当前时间小于 10,下面的实例将输出 "Have a good morning!",如果当前时间不小于 10 且小于 20,则输出 "Have a good day!",否则输出 "Have a good night!":</p>
<h2>实例</h2>
<p><?php $t=date("H"); if ($t<"10") { echo "Have a good morning!"; } elseif ($t<"20") { echo "Have a good day!"; } else { echo "Have a good night!"; } ?></p>
</div>
</div>
<div id="treeSkill"></div>
页:
[1]