<?php header("Content-type: text/html; charset=utf-8"); echo table(5,5); function table($rows,$cols){ //函数 $str = ''; $str .= '<table border="1" width="800" align="center">'; $str .= '<caption><h1>表格</h1></caption>'; for($i=0;$i<$rows;$i++){ $bg=($i%2==0)?"#cccccc":""; $str .= '<tr bgcolor="'.$bg.'">'; for($j=0;$j<$cols;$j++){ $str .= '<td>'.($i*$cols+$j).'</td>'; } $str .= '</tr>'; } $str .= '</table>'; return $str; } echo table(10,10); if(function_exists("table")){ //判断函数是否存在的函数 echo "existed!<br>"; }else{ echo "not existed!<br>"; } ?>
<?php header("Content-type: text/html; charset=utf-8"); $name = "I"; define("cha1","i love u."); function demo(){ echo cha1.'<br>'; //常量/系统自带的一些变量($_POST,$_GET)可以不用global声明 $name = "She"; echo $name.'<br>'; global $name; //声明接下来使用全局变量$name echo $name.'<br>'; $name = "U"; echo $name.'<br>'; } echo $name.'<br>'; demo(); echo $name.'<br>'; /** 静态变量(用处:统计函数被调用的次数~) 1.在函数中声明的静态变量,只在第一次调用时声明, 2.第二次以后,一看是静态变量,就到静态区中看一下有没有这个变量,如果有就使用,而不去再声明 3.静态变量在同一个函数多次调用中共享,在不同函数中不共享。(不仅存储了静态变量名,还存储了是属于哪个函数)