admin 发表于 2023-2-16 18:54:41

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>当您编写代码时&#xff0c;您常常需要为不同的判断执行不同的动作。您可以在代码中使用条件语句来完成此任务。</p>
<p>在 PHP 中&#xff0c;提供了下列条件语句&#xff1a;</p>
<ul><li><strong>if 语句</strong> - 在条件成立时执行代码</li><li><strong>if...else 语句</strong> - 在条件成立时执行一块代码&#xff0c;条件不成立时执行另一块代码</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&#xff0c;下面的实例将输出 &#34;Have a good day!&#34;&#xff1a;</p>
<h2>实例</h2>
<p><?php $t&#61;date(&#34;H&#34;); if ($t<&#34;20&#34;) { echo &#34;Have a good day!&#34;; } ?></p>
<p><br><ahref="https://www.runoob.com/try/showphp.php?filename&#61;demo_if">运行实例 »</a></p>
<p></p>
<hr>
<h2>PHP - if...else 语句</h2>
<p><strong>在条件成立时执行一块代码&#xff0c;条件不成立时执行另一块代码</strong>&#xff0c;请使用 if....else 语句。</p>
<h3>语法</h3>
<p>if (<em>条件</em>)<br> {<br><em>条件成立时执行的代码;</em><br> }<br> else<br> {<br><em>条件不成立时执行的代码;</em><br> }</p>
<p>如果当前时间小于 20&#xff0c;下面的实例将输出 &#34;Have a good day!&#34;&#xff0c;否则输出 &#34;Have a good night!&#34;&#xff1a;</p>
<h2>实例</h2>
<p><?php $t&#61;date(&#34;H&#34;); if ($t<&#34;20&#34;) { echo &#34;Have a good day!&#34;; } else { echo &#34;Have a good night!&#34;; } ?></p>
<p><br><ahref="https://www.runoob.com/try/showphp.php?filename&#61;demo_if_else">运行实例 »</a></p>
<p></p>
<hr>
<h2>PHP - if...elseif....else 语句</h2>
<p><strong>在若干条件之一成立时执行一个代码块</strong>&#xff0c;请使用 if....elseif...else 语句。.</p>
<h3>语法</h3>
<pre>if (条件)
{
    if 条件成立时执行的代码;
}
elseif (条件)
{
    elseif 条件成立时执行的代码;
}
else
{
    条件不成立时执行的代码;
}</pre>
<p>如果当前时间小于 10&#xff0c;下面的实例将输出 &#34;Have a good morning!&#34;&#xff0c;如果当前时间不小于 10 且小于 20&#xff0c;则输出 &#34;Have a good day!&#34;&#xff0c;否则输出 &#34;Have a good night!&#34;&#xff1a;</p>
<h2>实例</h2>
<p><?php $t&#61;date(&#34;H&#34;); if ($t<&#34;10&#34;) { echo &#34;Have a good morning!&#34;; } elseif ($t<&#34;20&#34;) { echo &#34;Have a good day!&#34;; } else { echo &#34;Have a good night!&#34;; } ?></p>
                </div>
      </div>
      <div id="treeSkill"></div>
页: [1]
查看完整版本: PHP If...Else 语句