Blogger Information
Blog 18
fans 0
comment 0
visits 14866
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
php 函数
牛粪也香的博客
Original
769 people have browsed it

$array=array(1,2,3,4,5,6);

function test1(&$var){

    $var*=3;

}

$res=array_walk($array,"test1");//array_walk(array,funcname);

print_r($res);

print_r($array);



$array=array(1,2,3,4,5,6,7);

function odd($var){

    if($var%2==1){

        return $var;

    }

}

$res=array_filter($array,'odd');


var_dump($res);


//call_user_func() 和 call_user_func_array();


function study($username){

    echo $username." is studying...";

}

function play($username){

    echo $username." is playing";

}

function test(){

    echo "this is a test</br>";

}

//

call_user_func('play',"king");

call_user_func('test');

echo call_user_func('md5',"queen");


echo "<hr>";


    function add($i,$j){

        return $i+$j;

    }

    function reduce($i,$j){

        return $i-$j;

    }


echo call_user_func("add",3,5);//这里是两个参数

echo "<hr>";

echo call_user_func_array('reduce',array(7,4));//若感觉参数太多,也可以传一个数组

echo call_user_func_array('add',array(1,4));



echo "<hr>";

//匿名函数,也叫闭包函数,即临时创建一个没有指定名字的函数,

//最经常用作回调函数参数的值。

//匿名函数也可作为变量的值来使用。

$func=function(){

 return 'this is a test';

};


echo $func();//这是以变量的形式负值

echo "<hr>";

$fun=function($username){//带参数的形式

    return 'say hi to '.$username;    

};


echo $fun('haha');


echo "<hr>";

//用creat_function来创建匿名函数,在php7.2中被取消了


$func=create_function('','echo "this is a test....";');

$func();

echo "<hr>";

$fun=function($i,$j){ return $i+$j;};

echo $fun(3,8);



echo "<hr>";

$array=[1,2,3,4,5];

//把匿名函数当成一个参数

$res=array_map(function($var){return $var*3;},$array);//array_map(funname,array)


print_r($res);

echo "<hr>";


//把匿名函数当成一个参数

call_user_func(function($username){echo 'say hi to '.$username ;},'kaka');


echo "<hr>";


//ddddd//递归函数-----------------------------------------

//通俗的讲就是自己调用自己的函数,通过特殊条件结束执行

//用到的地方,php遍历,目录的复制,删除非空目录等操作

//以及无限极分类


function testdd($i){

    echo $i."</br>" ;

    --$i;

    if($i>=0){

     testdd($i);

    }


}

testdd(3);

echo "<hr>";


function testda($i){

    echo $i,"<br>";

    --$i;    

    if($i>=0){


        testda($i);

    }


    echo $i,"<br/>";    


}


testda(4);

echo "<hr>";

//优化

 function testdb($i){

    echo $i,"<br/>";

    if($i>=0){

      $func=__FUNCTION__;//__FUNCTION__魔术常量获取当前函数的函数名

     $func($i-1);        

    }

    echo $i,"<br/>";

}


testdb(4);




function getext1($filename){

    

    return strtolower(pathinfo($filename,PATHINFO_EXTENSION));


}


$a='cc.ip.cccccccccc';

echo getext1($a);



function getday($y='年',$m='月',$d='日'){

$dayarr=array('日','一','二','三','四','五','六');

$day=date('w');

return date("Y{$y}m{$m}d{$d} 星期").$dayarr[$day];

}


echo '<center><h1>'.getday().'<h1></center>';


echo "<br/>";

$day=date('w');

echo $day;

echo "<hr>";


function cal($num1,$num2,$opt='+'){

    switch($opt){

        case "+":

        $res=$num1+$num2;

        break;


        case "-":

        $res=$num1-$num2;

        break;

    

        case "*":

        $res=$num1*$num2;


        break;


        case "/":

        if($num2==0){

        exit("0不能做除数");

        //break;

        }else{

        $res=$num1/$num2;

        break;

        }

        case "%":

        $res=$num1%$num2;

        break;

    }


    return "{$num1}{$opt}{$num2} = ".$res;

}

echo cal(7,55,"*");


Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post