php入门第八天

    函数。
    php的函数是过程式编程的基础。


<?php
function foo()
{
    echo 'some','<br/>';
}
// 这是基本形式。


// 下面是有参数和返回值的。


function add($x,$y)
{
    $total = $x + $y;
    return $total;
}


echo add(1, 2),'<br/>';
foo();




//参数可以限制类型
function get_tt(tt $x)
{
    echo $x->a,'<br/>';
}
function get_iface(iface $x)
{
    echo $x->c(),'<br/>';
}


class tt {
    public $a = 4;
}
interface iface {


    public function c();
}
class pp extends tt implements iface {


    public function c()
    {
    }
}


get_tt(new pp);
get_iface(new pp)
?>


函数的参数及调用。
<?php


$add = 'add';
echo $add(1, 2),':::',call_user_func('add', 23, 24),'::::',call_user_func_array('add', array(20, 20,)),'<br/>';


// 这都可以调用add 函数, 那么 call_user_func这个是怎么做到多个参数的呢?


function sum()
{
    $i = 0;
    foreach (func_get_args() as $v)
    {
        $i += $v;
    }
    
    return $i;
}


echo sum(1, 2, 3),sum(),'<br/>';
?>


匿名函数的使用


<?php




/* 
// js是这样写
fn = function() {
};
要直接执行是这样写
(function() {
})();
 */
// php是这样


$fn = function($test)
{
    static $i;
    echo 'clojure ',$test,++$i,'<br/>';
};


$fn('closure');


//直接执行是这样
call_user_func(function($test) use($fn)
    {
        $fn("clojure ',$test");
    }, 'closure'
);


call_user_func(function($test) use($fn)
    {
        $fn("clojure ',$test");
    }, 'closure'
);


//需要注意的是,以上的参数都没有用&来传地址,但因为$fn是对像,php5自动传地址,而不是传值,所以static的值是会变的。
//下面再写个


$fn2 = clone $fn ;
call_user_func(function($test) use($fn2)
    {
        $fn2("clojure ',$test");
    }, 'closure'
);
call_user_func(function($test) use($fn)
    {
        $fn("clojure ',$test");
    }, 'closure'
);


/* 
static的使用方法
static $a; 或static $a = NULL; 
$a === NULL && $a = 给它赋值


或者
static $a = 1;


下面会报错
static $a = new object;

static $a = function();


如果不想用static, 但注意要用&
 */
 

$i = 0;

$st_fn = function() use(& $i)
{
    echo ++$i,'<br/>';
};


$st_fn();
$st_fn();


/*
javascript的实现是
var i = 0;
var $st_fn = function() {
    alert(++i);
};
$st_fn();
$st_fn();
*/


// array_filter和array_map是比较有用的函数 都可以用还避免一般循环产生的 不易管理的作用域较大的变量。可以在很大程序上避免出错。及易于维护。


?>

下面的写法要单独用个文件。


<?php


namespace abc {
    // php 的命名空间与函数
    
    function test()
    {
        return 'test abc';
    }
}
namespace efg {


    function test()
    {
        return 'test efg';
    }
    
    trait config {
        public static $ff = 123;
    }
    trait t {
        public static $ee = 321;
    }
    
    config::$ff = 321;
    class yy {
        const yy = 123;
        public static $yy=123;
    }
    class test extends yy {
        use config, t, \b;
        // use t;
        const yy = 223;
        const tt = parent::yy;
        public function __construct()
        {
            // const self::yy = 234;
        }
    }


    $t = new test;
    echo test::tt,'<br/>',test::$ee,'<br/>',test::$ef,'<br/>';
}
namespace {
    echo abc\test(),'<br/>',efg\test();
    function __autoload($class) {
        if ($class === 'b')
        {
            trait b {
                public static $ef = 'abc';
            }
        }
    }
    
    // 命名空间对变量是无效的,只对函数和类的方法和变量有效。
    // 下面的写法如果用include require引用文件,就不会影响变量
    return call_user_func(function()
    {
        $url = $_SERVER['DOCUMENT_ROOT'];
        
        $url_to = 'F:/backup'; // 备份文件夹
        
        $arr = array(
            'curr' => 'web',
            'test3',
            'cs' => 'CodeIgniter_cs',
            'eloquent',
            'lara_wei',
        );
        
        foreach ($arr as $k => &$v)
        {
            $v = array('from' => "{$url}/{$v}/", 
                'to' => "{$url_to}/{$v}/");
        }


        return $arr;
    });
}

?>



下面讲递归。


这是不考虑效率的快速排序的php代码。


<?php


var_dump(qsort(array(1,2,3,4,11,13,15,11,12,10,8,9)));
function qsort($arr)
{
    // if (isset($arr[1]))
    if (count($arr)>1)
    {
        $mid = array_shift($arr);
        return array_merge(
            qsort(array_filter($arr, function($v) use($mid) {
                return $v < $mid;
            }))
            , array($mid)
            , qsort(array_filter($arr, function($v) use($mid) {
                return $v >= $mid;
            }))
        );
    }
    else
    {
        return $arr;
    }
}

?>


js

<script>

var numbers = [1,2,3,4,11,13,15,11,12,10,8,9];
alert(q_sort(numbers));


function q_sort(arr) {
    if (arr.length > 1) {
        var mid = arr.shift();
        return [].concat(
            q_sort(arr.filter(function(num) {
                return num < mid;
            }))
            , [mid]
            , q_sort(arr.filter(function(num) {
                return num >= mid;
            }))
        );
    } else {
        return arr;
    }
}


</script>


python3


def qsort(arr):
    if arr.__len__() > 1:
        mid = arr.pop(0)
        return (qsort(list(filter(lambda v : v<mid, arr)))
            + [mid]
            + qsort(list(filter(lambda v : v>=mid, arr))))
    else:
        return arr


print(qsort([1,2,3,4,11,15,13,9,8]))




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值