PHP 基本语法 条件 循环 函数杨亮
程序的基本结构 程序 输 入 运算 (+ - x / &! ) 逻辑 ( 条件 循环 递归 ) 输出 辅助 ( 变量 数组 函数 )
小测验 用你熟悉的程序找出 1~1000 中的所有质数
我们直接看代码好了
if else elseif 1 <?php 2 if( $totalqty == 0 ) 3 echo 'You did not order anything on the previous page!<br />'; 4 5 if ($totalqty == 0) { 6 echo '<p style="color:red">'; 7 echo 'You did not order anything on the previous page!'; 8 echo '</p>'; 9 } 10 11 if ($totalqty == 0) { 12 echo "You did not order anything on the previous page!<br />"; 13 } else { 14 echo $tireqty." tires<br />"; 15 echo $oilqty." bottles of oil<br />"; 16 echo $sparkqty." spark plugs<br />"; 17 } 18 19 if ($tireqty < 10) { 20 $discount = 0; 21 } elseif (($tireqty >= 10) && ($tireqty <= 49)) { 22 $discount = 5; 23 } elseif (($tireqty >= 50) && ($tireqty <= 99)) { 24 $discount = 10; 25 } elseif ($tireqty >= 100) { 26 $discount = 15; 27 }
switch 29 if ($find == "a") { 30 echo "<p>regular customer.</p>"; 31 } elseif ($find == "b") { 32 echo "<p>customer referred by TV advert.</p>"; 33 } elseif ($find == "c") { 34 echo "<p>customer referred by phone directory.</p>"; 35 } elseif ($find == "d") { 36 echo "<p>customer referred by word of mouth.</p>"; 37 } else { 38 echo "<p>we do not know how this customer found us.</p>"; 39 } 40 41 switch($find) { 42 case "a" : 43 echo "<p>regular customer.</p>"; 44 break; 45 case "b" : 46 echo "<p>customer referred by TV advert.</p>"; 47 break; 48 case "c" : 49 echo "<p>customer referred by phone directory.</p>"; 50 break; 51 case "d" : 52 echo "<p>customer referred by word of mouth.</p>"; 53 break; 54 default : 55 echo "<p>we do not know how this customer found us.</p>"; 56 break; 57 } 58?>
1 <html> 2 <body> 3 <table border= 0 cellpadding= 3 > 4 <tr> 5 <td bgcolor= #CCCCCC align= center >Distance</td> 6 <td bgcolor= #CCCCCC align= center >Cost</td> 7 </tr> 8 <tr> 9 <td align= right >50</td> 10 <td align= right >5</td> 11 </tr> 12 <tr> 13 <td align= right >100</td> 14 <td align= right >10</td> 15 </tr> 16 <tr> 17 <td align= right >150</td> 18 <td align= right >15</td> 19 </tr> 20 <tr> 21 <td align= right >200</td> 22 <td align= right >20</td> 23 </tr> 24 <tr> 25 <td align= right >250</td> 26 <td align= right >25</td> 27 </tr> 28 </table> 29 </body> 30 </html> Distance Cost 50 5 100 10 150 15 200 20 250 25
1 <html> 2 <body> 3 <table border="0" cellpadding="3"> 4 <tr> 5 <td bgcolor="#cccccc" align="center">distance</td> 6 <td bgcolor="#cccccc" align="center">cost</td> 7 </tr> 8 <?php 9 $distance = 50; 10 while ($distance <= 250) { 11 echo "<tr> 12 <td align=\"right\">".$distance."</td> 13 <td align=\"right\">".($distance / 10)."</td> </tr> \n"; 14 $distance += 50; 15 } 16?> 17 </table> 18 </body> 19 </html> 9 for ($distance = 50; $distance <= 250; $distance += 50) { 10 echo "<tr> Distance Cost 50 5 100 10 150 15 200 20 250 25 while 11 <td align=\"right\">".$distance."</td> 12 <td align=\"right\">".($distance / 10)."</td> </tr>\n"; for 13 }
数组与循环 2 $prices = array('tires'=>100, 'Oil'=>10, 'Spark Plugs'=>4); 3 4 foreach ($prices as $key => $value) { 5 echo $key." ".$value."<br />"; 6 } 7 8 while ($element = each($prices)) { 9 echo $element['key']; 10 echo " "; 11 echo $element['value']; 12 echo "<br />"; 13 } 14 15 reset($prices); 16 while (list($product, $price) = each($prices)) { 17 echo "$product - $price<br />"; 18 }
软件 工程中的代码重 用 因为我们都懒 ( 做过的事情不想再做第 二次 ) 降低开发成本 增加代码的可靠性 增强程序的 一致性
PHP 中的代码重 用 将其他 文件中的 html 或者 php 代码引 入到本 文件 require() 与 include(); require_once() 与 include_once(); 可以引 入其他的函数库, 或者代码 片段 1 <?php 2 //reusable.php 3 echo 'Here is a very simple PHP statement.<br />'; 4?> 1 <?php 2 //use.php 3 echo 'This is the main file.<br />'; 4 require( 'reusable.php' ); 5 echo 'The script will end now.<br / 6?>
PHP 实现 网 页模板 menu header contenta require( header.php ); require( menu.php ); contenta require( footer.php ); footer header require( header.php ); require( menu.php ); contentb require( footer.php ); menu contentb footer header.php footer.php menu.php
找出 1~1000 中的所有质数 1 <?php 2 for ($i=2; $i<1000; $i++) { 3 for ($j=2; $j<sqrt($i); $j++) { 4 if (doubleval($i)%doubleval($j) == 0 ) { 5 break; 6 } 7 } 8 if ($j > sqrt($i)) { 9 echo $i.'<br>'; 10 } 11 } 12?> 判断 $i 是否为质数 1 <?php 2 for ($i=2; $i<1000; $i++) { 3 if (is_zhishu($i)) { 4 echo $i.'<br>'; 5 } 6 } 7?>
DRY: Don t Repeat Yourself
Ctrl-C & Ctrl-V
代码的可读性 良好的代码 风格
为什么 自 己写有函数 代码的可读性 代码的可重 用性 实现功能的模块化 实现递归调 用 使变量名不 至于太 长 ( 作 用域 )
PHP 中的函数 实参 $res = my_function($val1, $val2); 函数调 用 返回值 函数名关键字 functon my_function($param1, $param2) { //function implementation return $result; } 形参 函数定义
2 for ($i=2; $i<1000; $i++) { 3 for ($j=2; $j<sqrt($i); $j++) { 4 if (doubleval($i)%doubleval($j) == 0 ) { 5 break; 6 } 7 } 8 if ($j > sqrt($i)) { 9 echo $i.'<br>'; 10 } 11 } 1 <?php 2 for ($i=2; $i<1000; $i++) { 3 if (is_zhishu($i)) { 4 echo $i.'<br>'; 5 } 6 } 7?> 2 function is_zhishu($num) { 3 for ($j=2; $j<sqrt($num); $j++) { 4 if (doubleval($num)%doubleval($j) == 0 ) { 5 break; 6 } 7 } 8 if ($j > sqrt($num)) { 9 return 1; 10 }else{ 11 return 0; 12 } 13 }
缺省参数值 在函数定义时指定缺省参数值 functon my_function($param1=default1, $param2=default2) { //function implementation return $result; } $res = my_function($val1, $val2); //$param1=$val1, $param2=$val2 $res = my_function($val1); //$param1=$val1, $param2=default2 $res = my_function(); //$param1=default1, $param2=default2 缺省参数按照重要性排序, 越可以 自动确定的越放到后 面
可变参数函数 所有输 入值求和 所有输 入值的最 大公约数 ( 最 小公倍数 ) fun_num_args() 与 fun_get_args(); 1 <?php 2 function var_args() { 3 echo "Number of parameters:"; 4 echo func_num_args(); 5 echo "<br />"; 6 $args = func_get_args(); 7 foreach ($args as $arg) { 8 echo $arg."<br />"; 9 } 10 } 11?>
变量作 用域 局部变量 ( 函数中定义 ), 从定义处到函数结束 全局变量 ( 非函数中定义 ), 从定义出到 文件尾, 但不包括其中的函数 超全局变量 ( 下节课会讲到 ), 所以地 方 require 和 include 都不影响变量作 用域, 只是起到了 将代码 片段组合的作 用 局部变量加上关键字 global, 可令其变为全局变量, 即在函数外也可以访问
斐波那契数列 x(1)=1; x(2)=1; 通过函数实现递归 X(n)=X(n-1)+x(n-2), n>2 2 function my_fabo($num) { 2 $n = 100; 3 $x=array(); 4 $x[1]=1; 5 $x[2]=1; 6 for($i=3; $i<=$n; $i++) { 7 $x[$i] = $x[$i-1] + $x[$i-2]; 8 echo "x($i)=".$x[$i].'<br>'; 9 } 3 if ($num == 1) { 4 return 1; 5 }elseif ($num == 2) { 6 return 1; 7 }else{ 8 return my_fabo($num-1) + my_fabo($num-2); 9 } 10 } 11 12 $n = 10; 13 echo "x($n)=".my_fabo($n);
作业 一 用递归函数实现汉诺塔如何移动
作业 二 用函数实现矩阵的加减乘除转置操作