1";elseif($num==3"/> 1";elseif($num==3">
Rumah > Artikel > pembangunan bahagian belakang > PHP的根本语法(三)
PHP的基本语法(三)
如果指定条件为真,则执行代码
如果条件为 true,则执行代码;如果条件为 false,则执行另一端代码
选择若干段代码块之一来执行
<code class=" hljs php"><span class="hljs-variable">$num</span>=<span class="hljs-number">3</span>;<span class="hljs-keyword">if</span>(<span class="hljs-variable">$num</span>><span class="hljs-number">3</span>)<span class="hljs-keyword">echo</span> <span class="hljs-string">"\$num>1"</span>;<span class="hljs-keyword">elseif</span>(<span class="hljs-variable">$num</span>==<span class="hljs-number">3</span>)<span class="hljs-keyword">echo</span> <span class="hljs-string">"\$num=3"</span>;<span class="hljs-keyword">else</span><span class="hljs-keyword">echo</span> <span class="hljs-string">"\$num<3"</span>;</code>
运行结果:$num=3
有选择地执行若干代码块之一
工作原理:
<code class=" hljs php"><span class="hljs-variable">$x</span>=<span class="hljs-number">4</span>;<span class="hljs-keyword">switch</span> (<span class="hljs-variable">$x</span>){<span class="hljs-keyword">case</span> <span class="hljs-number">1</span>: <span class="hljs-keyword">echo</span> <span class="hljs-string">"Number 1"</span>; <span class="hljs-keyword">break</span>;<span class="hljs-keyword">case</span> <span class="hljs-number">2</span>: <span class="hljs-keyword">echo</span> <span class="hljs-string">"Number 2"</span>; <span class="hljs-keyword">break</span>;<span class="hljs-keyword">default</span>: <span class="hljs-keyword">echo</span> <span class="hljs-string">"No number between 1 and 2"</span>;}</code>
运行结果:No number between 1 and 2
只要指定条件为真,则循环代码块
先执行一次代码块,然后只要指定条件为真则重复循环
循环代码块指定次数
遍历数组中的每个元素并循环代码块
<code class=" hljs handlebars"><span class="xml"><span class="hljs-tag"><span class="php"><span class="hljs-preprocessor"><?PHP</span><span class="hljs-variable">$its</span> = <span class="hljs-keyword">array</span>(<span class="hljs-string">"Apple"</span>,<span class="hljs-string">"Google"</span>,<span class="hljs-string">"Microsoft"</span>,<span class="hljs-string">"Solidot"</span>); <span class="hljs-keyword">foreach</span> (<span class="hljs-variable">$its</span> <span class="hljs-keyword">as</span> <span class="hljs-variable">$it</span>) { <span class="hljs-keyword">echo</span> <span class="hljs-string">"$it <br>"</span>;}<span class="hljs-preprocessor">?></span></span></span></span></code>
运行结果: Apple <br>Google <br>Microsoft <br>Solidot
函数是可以在程序中重复使用的语句块。
页面加载时函数不会立即执行。
函数只有在被调用时才会执行。
<code class=" hljs php"><span class="hljs-preprocessor"><?php</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">passwd</span><span class="hljs-params">(<span class="hljs-variable">$account</span>,<span class="hljs-variable">$password</span>=<span class="hljs-number">123456</span>)</span>{</span><span class="hljs-keyword">return</span> <span class="hljs-variable">$password</span>;}<span class="hljs-variable">$pLee</span> = passwd(<span class="hljs-string">"Lee"</span>);<span class="hljs-keyword">echo</span> <span class="hljs-string">"$pLee"</span>;<span class="hljs-preprocessor">?></span></code>
运行结果:123456
版权声明:本文为Lshare原创文章,需要转载的请联系我,有问题欢迎评论或私信。