>  기사  >  백엔드 개발  >  前端学PHP之话语

前端学PHP之话语

WBOY
WBOY원래의
2016-06-13 12:28:11949검색

前端学PHP之语句

目录
[1]条件语句 if语句 switch语句 [2]循环语句 while循环 do…while循环 for循环 foreach循环

条件语句

  用于基于不同条件执行不同的动作

if语句

<span style="color: #0000ff;">if</span><span style="color: #000000;"> (条件) {  当条件为 </span><span style="color: #0000ff;">true</span><span style="color: #000000;"> 时执行的代码;}</span>
<span style="color: #0000ff;">if</span><span style="color: #000000;"> (条件) {  条件为 </span><span style="color: #0000ff;">true</span><span style="color: #000000;"> 时执行的代码;} </span><span style="color: #0000ff;">else</span><span style="color: #000000;"> {  条件为 </span><span style="color: #0000ff;">false</span><span style="color: #000000;"> 时执行的代码;}</span>
<span style="color: #0000ff;">if</span><span style="color: #000000;"> (条件) {  条件为 </span><span style="color: #0000ff;">true</span><span style="color: #000000;"> 时执行的代码;} elseif (条件) {  条件为 </span><span style="color: #0000ff;">true</span><span style="color: #000000;"> 时执行的代码;} </span><span style="color: #0000ff;">else</span><span style="color: #000000;"> {  条件为 </span><span style="color: #0000ff;">false</span><span style="color: #000000;"> 时执行的代码;}</span>
<span style="color: #000000;">php</span><span style="color: #800080;">$t</span>=<span style="color: #008080;">date</span>("H"<span style="color: #000000;">);</span><span style="color: #0000ff;">if</span> (<span style="color: #800080;">$t</span>) {  <span style="color: #0000ff;">echo</span> "Have a good morning!"<span style="color: #000000;">;} </span><span style="color: #0000ff;">elseif</span> (<span style="color: #800080;">$t</span>) {  <span style="color: #0000ff;">echo</span> "Have a good day!"<span style="color: #000000;">;} </span><span style="color: #0000ff;">else</span><span style="color: #000000;"> {  </span><span style="color: #0000ff;">echo</span> "Have a good night!"<span style="color: #000000;">;}</span>?>

 

switch语句

<span style="color: #0000ff;">switch</span><span style="color: #000000;"> (expression){</span><span style="color: #0000ff;">case</span> label1:<span style="color: #000000;">  code to be executed </span><span style="color: #0000ff;">if</span> expression =<span style="color: #000000;"> label1;  </span><span style="color: #0000ff;">break</span><span style="color: #000000;">;  </span><span style="color: #0000ff;">case</span> label2:<span style="color: #000000;">  code to be executed </span><span style="color: #0000ff;">if</span> expression =<span style="color: #000000;"> label2;  </span><span style="color: #0000ff;">break</span><span style="color: #000000;">;</span><span style="color: #0000ff;">default</span>:<span style="color: #000000;">  code to be executed  </span><span style="color: #0000ff;">if</span><span style="color: #000000;"> expression is different   from both label1 and label2;}</span>
<span style="color: #000000;">php</span><span style="color: #0000ff;">switch</span> (<span style="color: #800080;">$x</span><span style="color: #000000;">){</span><span style="color: #0000ff;">case</span> 1:  <span style="color: #0000ff;">echo</span> "Number 1"<span style="color: #000000;">;  </span><span style="color: #0000ff;">break</span><span style="color: #000000;">;</span><span style="color: #0000ff;">case</span> 2:  <span style="color: #0000ff;">echo</span> "Number 2"<span style="color: #000000;">;  </span><span style="color: #0000ff;">break</span><span style="color: #000000;">;</span><span style="color: #0000ff;">case</span> 3:  <span style="color: #0000ff;">echo</span> "Number 3"<span style="color: #000000;">;  </span><span style="color: #0000ff;">break</span><span style="color: #000000;">;</span><span style="color: #0000ff;">default</span>:  <span style="color: #0000ff;">echo</span> "No number between 1 and 3"<span style="color: #000000;">;}</span>?>

 

循环语句

  在编写代码时,经常需要反复运行同一代码块,可以使用循环来执行这样的任务

while循环

<span style="color: #0000ff;">while</span><span style="color: #000000;"> (条件为真) {  要执行的代码;}</span>
<span style="color: #000000;">php</span><span style="color: #800080;">$sum</span> = 12;<span style="color: #008000;">//</span><span style="color: #008000;">小宠物当前的饥饿程度</span><span style="color: #0000ff;">echo</span> "我饿啦:-("<span style="color: #000000;">;</span><span style="color: #0000ff;">echo</span> "<br>"<span style="color: #000000;">;</span><span style="color: #0000ff;">while</span>(<span style="color: #800080;">$sum</span>){<span style="color: #008000;">//</span><span style="color: #008000;">小宠物的饥饿程度到100,表示小宠物吃饱啦,不用继续喂了,没吃饱继续喂食</span>    <span style="color: #800080;">$num</span> = <span style="color: #008080;">rand</span>(1,20);<span style="color: #008000;">//</span><span style="color: #008000;">随机数,模拟喂食小宠物的小面包</span>  <span style="color: #800080;">$sum</span> = <span style="color: #800080;">$sum</span> + <span style="color: #800080;">$num</span>; <span style="color: #008000;">//</span><span style="color: #008000;">小宠物吃小面包</span>  <span style="color: #0000ff;">echo</span> "我还没吃饱呢!"<span style="color: #000000;">;  </span><span style="color: #0000ff;">echo</span> "<br>"<span style="color: #000000;">;}</span><span style="color: #0000ff;">echo</span> "终于吃饱啦^_^"<span style="color: #000000;">;</span>?>

 

do…while循环

  循环首先会执行一次代码块,然后检查条件,如果指定条件为真,则重复循环

<span style="color: #0000ff;">do</span><span style="color: #000000;"> {  要执行的代码;} </span><span style="color: #0000ff;">while</span> (条件为真);
<span style="color: #000000;">php  </span><span style="color: #800080;">$sum</span>  = 0<span style="color: #000000;">;   </span><span style="color: #0000ff;">do</span><span style="color: #000000;">{    </span><span style="color: #800080;">$num</span> = <span style="color: #008080;">rand</span>(1,6);<span style="color: #008000;">//</span><span style="color: #008000;">获取1至6的随机数,模拟掷骰子</span>    <span style="color: #800080;">$sum</span> = <span style="color: #800080;">$sum</span>  + <span style="color: #800080;">$num</span>;<span style="color: #008000;">//</span><span style="color: #008000;">前进步长</span>  }<span style="color: #0000ff;">while</span>(<span style="color: #800080;">$num</span>==6<span style="color: #000000;">);  </span><span style="color: #0000ff;">echo</span> "do...while例子执行完毕,前进:".<span style="color: #800080;">$sum</span> ."<br>"<span style="color: #000000;">;</span>?>

 

for循环

  for循环语句中,初始化在循环开始前无条件求值一次,循环条件在每次循环开始前求值。如果值为true,则继续循环,执行循环体语句;如果值为false,则终止循环。递增语句在每次循环后执行

<span style="color: #0000ff;">for</span><span style="color: #000000;"> (init counter; test counter; increment counter) {  code to be executed;}    </span>
<span style="color: #000000;">php </span><span style="color: #0000ff;">for</span> (<span style="color: #800080;">$x</span>=0; <span style="color: #800080;">$x</span>$x++<span style="color: #000000;">) {  </span><span style="color: #0000ff;">echo</span> "数字是:<span style="color: #800080;">$x</span> <br>"<span style="color: #000000;">;} </span>?>

 

foreach循环

  foreach循环只适用于数组,用于遍历数组中每个键/值对。每进行一次循环迭代,当前数组元素的值就会被赋值给$value变量,并且数组指针会逐一移动,直到到达最后一个数组元素。一般有两种方式:不取下标、取下标

[1]只取值,不取下标

<span style="color: #0000ff;">foreach</span> (<span style="color: #800080;">$array</span> <span style="color: #0000ff;">as</span> <span style="color: #800080;">$value</span><span style="color: #000000;">) {  code to be executed;}    </span>
<span style="color: #000000;">php </span><span style="color: #800080;">$colors</span> = <span style="color: #0000ff;">array</span>("red","green","blue","yellow"<span style="color: #000000;">); </span><span style="color: #0000ff;">foreach</span> (<span style="color: #800080;">$colors</span> <span style="color: #0000ff;">as</span> <span style="color: #800080;">$value</span><span style="color: #000000;">) {  </span><span style="color: #0000ff;">echo</span> "<span style="color: #800080;">$value</span> <br>"<span style="color: #000000;">;}</span>?>

[2]同时取下标和值

<span style="color: #0000ff;">foreach</span> (<span style="color: #800080;">$array</span> <span style="color: #0000ff;">as</span> <span style="color: #800080;">$index</span> => <span style="color: #800080;">$value</span><span style="color: #000000;">) {  code to be executed;} </span>
<span style="color: #000000;">php </span><span style="color: #800080;">$colors</span> = <span style="color: #0000ff;">array</span><span style="color: #000000;">(  </span>"r"=>"red",  "g"=>"green",  "b"=>"blue",  "y"=>"yellow"<span style="color: #000000;">); </span><span style="color: #0000ff;">foreach</span> (<span style="color: #800080;">$colors</span> <span style="color: #0000ff;">as</span> <span style="color: #800080;">$key</span> => <span style="color: #800080;">$value</span><span style="color: #000000;">) {  </span><span style="color: #0000ff;">echo</span> <span style="color: #800080;">$key</span>.":".<span style="color: #800080;">$value</span>."<br>"<span style="color: #000000;">;}</span>?>

 

1楼文昊学PHP
PHP是楼主接触的第一个编程语言?
Re: 小火柴的蓝色理想
@文昊学PHP,我学前端的,主要是js,PHP了解即可
성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.