自定義函數(shù)語(yǔ)法格式
<?php
table();
echo "1111111111111111111<br>";
table();
echo "xxxxxxxxxxxxxxxxxxxxxxx<br>";
table();
echo "hello world <br>";
table();
table();
table();
function table(){
echo '<table border="1" width="800" align="center">';
echo '<caption><h1>表格</h1></caption>';
for($i=0; $i<10; $i++) {
$bg = ($i%2==0) ? "#cccccc" : "";
echo '<tr bgcolor="'.$bg.'">';
for($j=0; $j < 20; $j++ ) {
echo '<td>'.($i*10+$j).'</td>';
}
echo '</tr>';
}
echo '</table>';
}
table();
自定義函數(shù)參數(shù)
<?php
table("用戶表",10, 10, 2, 'red', 1, 800, 'left'); //實(shí)參 (實(shí)際的參數(shù))
table("成績(jī)表", 50, 5, 3, 'green', 10, 400, 'right');
//聲明函數(shù)時(shí),提供的參數(shù)炉擅, 形參(形式上的參數(shù))
function table($tablename, $rows, $cols, $num, $color, $border, $width, $align){
echo '<table border="'.$border.'" width="'.$width.'" align="'.$align.'">';
echo '<caption><h1>'.$tablename.'</h1></caption>';
for($i=0; $i<$rows; $i++) {
$bg = ($i%$num==0) ? $color : "";
echo '<tr bgcolor="'.$bg.'">';
for($j=0; $j < $cols; $j++ ) {
echo '<td>'.($i*10+$j).'</td>';
}
echo '</tr>';
}
echo '</table>';
}
自定義函數(shù)返回值
<?php
error_reporting(0);
echo table("用戶表",10, 10, 2, 'red', 1, 800, 'left'); //實(shí)參 (實(shí)際的參數(shù))
file_put_contents("demo.html", table("成績(jī)表", 50, 5, 3, 'green', 10, 400, 'right'));
//聲明函數(shù)時(shí)膘侮,提供的參數(shù), 形參(形式上的參數(shù))
function table($tablename, $rows, $cols, $num, $color, $border, $width, $align){
$str .= '<table border="'.$border.'" width="'.$width.'" align="'.$align.'">';
$str .= '<caption><h1>'.$tablename.'</h1></caption>';
for($i=0; $i<$rows; $i++) {
$bg = ($i%$num==0) ? $color : "";
$str.= '<tr bgcolor="'.$bg.'">';
for($j=0; $j < $cols; $j++ ) {
$str .= '<td>'.($i*10+$j).'</td>';
}
$str .= '</tr>';
}
$str .= '</table>';
return $str;
echo "#################";
}
if(function_exists("table2")) {
echo "存在";
}else{
echo "不存在";
}
局部變量
函數(shù)內(nèi)部聲明的變量玩徊, 只能在函數(shù)內(nèi)部調(diào)用, 這就是--------局部變量
函數(shù)的參數(shù)租悄,就是一個(gè)局部變量, 聲明成形成的恩袱, 可以在調(diào)用時(shí)泣棋,給值, 傳值
function demo($sex) {
$age = 30;
echo "高老師很帥{$age}, 是{$sex}<br>";
}
demo("男");
echo $sex;
全局變量
函數(shù)內(nèi)部聲明的變量憎蛤, 只能在函數(shù)內(nèi)部調(diào)用, 這就是--------局部變量
函數(shù)的參數(shù)外傅,就是一個(gè)局部變量, 聲明成形成的俩檬, 可以在調(diào)用時(shí)萎胰,給值, 傳值
全局變量棚辽, 在函數(shù)外部聲明的變量技竟,可以在每個(gè)函數(shù)中使用。 (順序)
在函數(shù)內(nèi)部如果需要使用函數(shù)外部的變量屈藐, 需要使用global關(guān)鍵字榔组,將外部變量引入
$_POST['a']=100;
$_GET['b']=300;
function demo() {
echo $_POST['a'];
}
demo();
靜態(tài)變量
在函數(shù)中聲明的靜態(tài)變量, 只在第一次調(diào)用時(shí)聲明.
第二次以后联逻, 一看是靜態(tài)變量搓扯, 就先到靜態(tài)區(qū)中,看一下有沒(méi)有這個(gè)變量包归, 如果有就使用锨推, 而不去再聲明
靜態(tài)變量,在同一個(gè)函數(shù)多次調(diào)用中 共享
static $c=99;
function demo() {
static $a = 0;
$a++;
echo $a."<br>";
}
function test() {
static $a = 0;
$a++;
}
echo $c;
demo();
demo();
test();
test();
test();
test();
demo();
demo();
demo();
默認(rèn)參數(shù)函數(shù)
<?php
function demo($name ="one",$age="two",$sex="three",$email="four"){
echo "{$name}----{$age}---{$sex}----{$email}<br>";
}
demo();
demo("妹子");
demo("妹子",20);
demo("妹子",20,"女");
demo("妹子",20,"女",'mz@lampbrother.net');
demo()
?>
可變參數(shù)
<?php
function demo(){
$arr=func_get_args();
$sun=0;
for ($i=0; $i < count($arr); $i++) {
$sum +=$arr[$i];
}
return $sum;
}
echo demo(1,2,3,4,5,6,7,8,9);
?>
變量函數(shù)
如果將一個(gè)函數(shù)名稱(字符串), 給一個(gè)變量(字符串), 然后在這個(gè)變量后面加上括號(hào), 就會(huì)調(diào)用這個(gè)變量值對(duì)應(yīng)函數(shù)
<?php
function add($a,$b){
return $a+$b;
}
function chen($a,$b){
return $a*$b;
}
function chu($a,$b){
if ($b!=0)
return $a/$b;
else
return false;
}
$var ="add";
$var ="chen";
$var ="chu";
echo $var(10,20);
add(10,20);
?>
回調(diào)函數(shù)
在使用一個(gè)函數(shù)的時(shí)候换可, 如果傳一個(gè)變量椎椰, 不能解決多大的問(wèn)題, 就需要將一個(gè)過(guò)程進(jìn)入到函數(shù)中沾鳄, 改變函數(shù)的執(zhí)行行為.
在函數(shù)的調(diào)用時(shí)慨飘, 在參數(shù)中傳的不是一個(gè)變量或一個(gè)值, 而是一個(gè)函數(shù)译荞, 這就是回調(diào)函數(shù)參數(shù)
<?php
$arr =array("aaa","aaaaaaa","a","aaaaa","aaaaaaaaaaaaaa","ssssss");
function mycom($a,$b){
if(strlen($a)>strlen($b))
return 1;
else if(strlen($a)<strlen($b))
return -1;
else
return 0;
}
print_r($arr);
usort($arr,"mycom");
echo '<br>';
print_r($arr);
?>
制作回調(diào)函數(shù)
在使用一個(gè)函數(shù)的時(shí)候瓤的, 如果傳一個(gè)變量, 不能解決多大的問(wèn)題磁椒, 就需要將一個(gè)過(guò)程進(jìn)入到函數(shù)中堤瘤, 改變函數(shù)的執(zhí)行行為.
在函數(shù)的調(diào)用時(shí), 在參數(shù)中傳的不是一個(gè)變量或一個(gè)值浆熔, 而是一個(gè)函數(shù)本辐, 這就是回調(diào)函數(shù)參數(shù)
制作回調(diào)函數(shù)
<?php
function demo($num,$n){
for($i=0;$i<$num;$i++){
if($n($i))
continue;
echo $i."<br>";
}
}
function test($i){
if($i==strrev($i))
return true;
else
return false;
}
demo(500,"test");
?>
制作回調(diào)函數(shù)
參數(shù)個(gè)數(shù)如果是變長(zhǎng)時(shí), 就不能直接調(diào)用這個(gè)函數(shù)
<?php
function demo($num,$n){
for($i=0;$i<$num;$i++){
if(call_user_func_array($n,array($i)))
continue;
echo $i."<br>";
}
}
function test($i){
if($i==strrev($i))
return true;
else
return false;
}
demo(500,"test");
?>
制作回調(diào)函數(shù)
<?php
error_reporting(0);
function demo($num,$n){
for($i=0;$i<$num;$i++){
if(call_user_func_array($n,array($i)))
continue;
echo $i."<br>";
}
}
class Filter{
function one($i){
if($i%3==0){
return true;
}else{
return false;
}
}
static function two($i){
if(preg_match('/3/',$i)){
return true;
}else{
return false;
}
}
}
function test($i){
if($i==strrev($i))
return true;
else
return false;
}
demo(500,array(new Filter(),"one"));
?>
系統(tǒng)函數(shù)
<?php
$dirname="./phpmyadmin";
function fordir($dirname){
$dir=opendir($dirname);
readdir($dir);
readdir($dir);
while($file=readdir($dir)){
$nfile=$dirname.'/'.$file;
if(is_dir($nfile)){
echo "目錄:{$nfile}<br>";
}else{
echo "文件:{$nfile}<br>";
}
}
closedir($dir);
}
fordir($dirname);
?>
遞歸函數(shù)
<?php
$dirname="./phpmyadmin";
function fordir($dirname){
$dir = opendir($dirname);
readdir($dir);
readdir($dir);
while($file=readdir($dir)){
$nfile=$dirname.'/'.$file;
if(is_dir($nfile)){
echo "目錄:{$nfile}<br>";
fordir($nfile);
}else{
echo"文件:{$nfile}<br>";
}
}
closedir($dir);
}
fordir($dirname);
?>
加載自定義函數(shù)庫(kù)
<?php
require "function.inc.php";
if($a == "a")
include "demo.txt";
else
include "demo2.html";
one();
two();
three();
匿名函數(shù)
<?php
/*
function funname($a, $b, $c) {
return $a+$b+$c;
}
$var = "funname";
var_dump($var);
echo $var(1,2,3);
*/
$var = function($a, $b, $c) {
return $a+$b+$c;
}; //一定要加分號(hào)結(jié)束
$aa = $var;
echo $aa(1,2,3);