/***************************by garcon1986********************************/
<?php //if 语句 $a = $b = 3; if($a = $b) print "a is equal to b<br>"; //else 语句 if($a < $b){ print "a is smaller than b"; } else { print "a is not smaller than b<br>"; } //elseif = else if 语句 if($a>$b){ print "a is bigger than b"; }else if($a = $b){ print "a is equal than b"; }else { print "a is smaller to b<br>"; } // when you don't use "{}", you can't separate elseif. //当不用{}时,elseif不能分开 if($a>$b): print "a>b"; elseif($a==$b): //else if($a==$b): // this is wrong print "a=b"; print '<br>'; endif; //while语句 $i = 0; while($i<=10){ print $i++.'<br>'; } $i=15; while($i<=20): print $i; $i++; endwhile; print '<br>'; //The difference between 'do while' and 'while' is: 'do while' will execute once at least. //do while 和while的区别在于前者至少要执行一次 $i=0; do { echo $i; }while($i>0); echo '<p>'; //do while example 2 do { if ($i < 5) { echo "i is not big enough"; break; } $i *= 2; if ($i < 50) { break; } echo "i is ok"; /* process i */ } while (0); //for(expression1; expression2; expression3) statement语句 //example1 for($i=0;$i<=6;$i++){ print $i; } echo '<br>'; //example2 for($i=0; ; $i++){ if($i>10){ break; } print $i; } echo '<br>'; //example3 for($i=0;;){ print $i; $i++; if($i>6){ break; } } echo '<br>'; //example4 $i=0; for(;;){ if($i>6){ break; } print $i; $i++; } echo '<br>'; //example5 for($i=0;$i<=10;print $i, $i++); echo '<p>'; //foreach语句 //example1 $arr = array('s','j','g','w','l'); foreach($arr as $value){ echo "$value<br>"; //$value is user-defined. } unset($value); //list to replace foreach, same result //使用list代替foreach,结果是一样的 $arr=array('s','j','g','w','l'); reset($arr); while(list(,$value)= each($arr)){ echo "value: $value <br>/n"; } //example2 //foreach version $arr = array('i','n','i','a','o','w'); foreach($arr as $key => $value){ echo "key:$key, value:$value<br>/n"; } // list($key,$value) $arr = array('w','o','a','i','n','i'); //reset($arr); while(list($key,$value)=each($arr)){ echo "key:$key, value:$value<br>/n"; } //foreach new examples //一维数组不含key $a = array(1,2,3,4,5); foreach($a as $v){ echo "current line of /$a is: $v<br>"; } //一维数组含key $i=5; foreach($a as $k => $v1){ echo "/$a[$i] : $k => $v1 <br>/n"; $i++; } //一维数组key,指定 $a = array("sjg"=>123,"ai"=>456,"wl"=>7890); foreach($a as $ke=>$va){ echo "$ke, $va<br>"; echo "/$a[$ke]=>$va<br>"; } //multiple-arrays多维数组 $s = $w = 0; $a = array( "sjg"=>array("work"=>"hard", "sleep"=>"well","business"=>"good"), "ai"=>array("lo"=>"ve","ve"=>"ry", "mu"=>"ch"), "wl"=>array("study"=>"hard", "rest"=>"pasmal","exam"=>"passed") ); foreach($a as $key=>$value1){ foreach($value1 as $key2=>$value2){ echo "$key, $key2, $value2<br>/n"; } //echo "$a, $value1"; } /* $x2 = array("早上"=>array("8点"=>"起床", "8点半"=>"到单位", "中午前"=>"工作"), "下午"=>array("12点到1点"=>"吃饭休息", "1点到6点"=>"继续工作"), "晚上"=>array("6点到7点"=>"吃好晚餐", "7点到10点"=>"自由活动") ); //输出我早上中午前在干什么 echo $x2["早上"]["中午前"]; */ // $a = array(); $a[0][0] = "a"; $a[0][1] = "b"; $a[1][0] = "y"; $a[1][1] = "z"; //二维数组 foreach ($a as $key1=> $v1) { foreach ($v1 as $key2=>$v2) { echo "$key1,$key2,$v2.<br>/n"; } } //break //eaxmple1 $arr = array("one", "two","three",4,'5'); while(list(,$var)=each($arr)){ if($var==4){ break; } echo "$var<br>/n"; } //example2 $i = 0; while(++$i){ switch($i){ case 4: echo "line 4<br>/n"; break 1; case 8: echo "line 8<br>/n"; break 2; default: break; } } //continue //example1 while (list ($key, $value) = each($arr)) { if (!($key % 2)) { // skip odd members continue; } do_something_odd($value); } $i = 0; while ($i++ < 5) { echo "Outer<br />/n"; while (1) { echo " Middle<br />/n"; while (1) { echo " Inner<br />/n"; continue 3; } echo "This never gets output.<br />/n"; } echo "Neither does this.<br />/n"; } //example2 for($i=0; $i<5; ++$i){ if($i==2) continue ; print "$i/n"; } //switch //example1:对比elseif 和 switch if($i == 0){ echo "i equals 0"; }elseif($i==1){ echo "i equals 1"; } switch($i){ case 0: echo "i equals 0"; break; case 1: echo "i equals 1"; break; } //example2 switch($i){ case "apple": echo "i is apple"; break; case "banana": echo "i is banana"; break; default: echo "i is not a fruit."; } //example3 switch($i): case 0: echo "i equals 0"; case 1: echo "i equals 1"; default: echo "i is not equals 0 or 1"; endswitch; //require() 语句包含并运行指定文件。 require ""; require $somefile; require ('somefile.txt'); //include 语句包含并运行指定文件。 //这两种结构除了在如何处理失败之外完全一样。include() 产生一个警告而 require() 则导致一个致命错误。 //换句话说,如果想在遇到丢失文件时停止处理页面就用 require()。 //require_once同一个文件只包含一次,不区分大小写。 //include_once同一个文件只包含一次,不区分大小写。 ?>