• function 相关


    <?php


    $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来创建匿名函数

    $func=create_function('','echo "this is a test....";');
    $func();
    echo "<hr>";
    $fun=create_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,"*");

    //不知道怎么用implode=join
    // $text=implode(range(0,0),range("a","z"),range("A","Z"));
    // $text=str_shuffle($text);$text=substr( $text,0,4);
    // $rand_color=imagecolorallocate($image,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));

    // $xuehua=imagesetpixel($image, mt_rand...
    //
    // -------------------------------------------------------------------------------
    // ----------------------------------字符串---------------------------------------------
    echo "<hr>";
    echo "<hr>";
    $str='abcdef';
    echo $str{0};
    echo "<br/>";
    echo $str{3}."<br/>";

    $str{1}="k";

    echo $str;
    echo "<hr>";
    //若是中文,中文在UTF8下占三个字符

    $str="你好";
    echo $str{0};
    echo $str{1};
    echo $str{2};//三个连续输出才以显示
    echo "<hr>";
    $str='abc';
    $str{0}='';
    echo $str;
    var_dump($str);

    //增加
    $str="abc";
    $str{3}='d';
    echo $str;
    var_dump($str);

    function code($len){
    //验证码。
    $str="abcdeftghijklmnopqrstuvwxyz";
    $str.=strtoupper($str);
    $str.='0123456789';
    $checkcode='';
    for ($i=1;$i<=$len;$i++){
      $checkcode.=$str{mt_rand(0,strlen($str)-1)};
    }
    return $checkcode;
    }

    echo code(5);
    echo "<hr>";
    // 定界符
    // heredoc语法结构:
    // <<<标识名称
    //     内容

    //     标识名称;
    // 标识名称,以字母下划线等开始,
    // //可以不有再管相关的字符了
    echo "heredoc";
    $username="wwkakak";
    $table=<<<EOF
    <table border='1px' width='80%' align='center'>
    <tr><td>1111</td><td>2222</td><td>3333333</td></tr>
    <tr><td>1111</td><td>2222</td><td>3333333</td></tr>
    <tr><td>1111</td><td>2222{$username}</td><td>33ip"ss"'3'3'333</td></tr>

    </table>

    EOF;
    echo $table;

    echo <<<eof
        hello baby
    eof;

    //nowdoc语法结构和heredoc语法结构一样,只是有单引号的作用
    echo '<hr/>';
    echo 'nowdoc<br/>';

    $str=<<<'eof'
    helle $username;
    eof;
    echo $str;
    echo "<br/>字符串类型的转换";
    echo 123;
    echo '<br/>';
    echo 34.5;
    echo '<br/>';
    echo true;
    echo '<br/>';
    echo 'a',false,'b';
    echo 'c',null,'d';
    echo '<br/>';
    $array=array(12,3,4);
    echo '<br/>';
    echo $array;
    echo '<br/>';
    echo $array[0];
    echo '<br/>';

    $handle=fopen('func.php','r');
    echo $handle;
    echo '<br/>';
    $obj=new stdClass();
    var_dump($obj);
    //echo $obj;
    //Catchable fatal error: Object of class stdClass could not be converted to string in D:wamp64wwwphptfunc.php on line 300
    echo '<br/>';

    $var=123;
    $var=123.2;
    $var=true;
    $var=false;
    $var=null;
    $var=array(1,2,3);
    $var=new stdClass();
    //Catchable fatal error: Object of class stdClass could not be converted to string
    $var=fopen('func.php','r');
    $res=(string)$var;
    var_dump($var,$res);

    //------------------------字符串函数库----------------------------------------
    echo "字符串函数库<hr>";
    $var='hello baby';

    var_dump(is_string($var));
    //检测字符串的长度
    //strlen($var)
    echo strlen($var);
    echo '<br/>';
    $res=strtoupper($var);
    echo $res;
    echo '<br/>';
    $res=strtolower($var);
    echo $res;
    echo '<br/>';
    //首字母大写
    $res=ucfirst($var);
    echo $res;
    echo '<br/>';
    //每个单词首字母大写
    $res=ucwords($var);
    echo $res;

  • 相关阅读:
    bzoj5328: [Sdoi2018]物理实验
    HDU
    bzoj4820: [Sdoi2017]硬币游戏
    bzoj4600: [Sdoi2016]硬币游戏
    阿里云配置防火墙规则
    博客园 添加 Live 2D 模型
    R语言做逻辑回归
    R语言错误的提示(中英文翻译)
    用随机森林分类
    python 切换虚拟环境
  • 原文地址:https://www.cnblogs.com/nfyx/p/10484693.html
Copyright © 2020-2023  润新知