PHP- return

return,echo 区别:
echo 直接输出,在function 中使用echo 会在调用时直接输出。
return 返回值,返回一个值并终止之后的程序。要用变量接受

$arr = array('one','two','three');
//echo 例子
function returnOrEcho($arr){
    foreach($arr as $a){
        echo $a;
    }
}
returnOrEcho($arr);//输出onetwothree
//return 例子
function returnOrEcho($arr){
    foreach($arr as $a){
        return $a;
    }
}
    $b = returnOrEcho($arr);
    echo $b;//输出 one 

PHP 中 return $this,的含义为:返回自身
用处:框架中数据的存取常用到。

class knowthis {
        private $one;
        private $two;
        function get($in){
            $this->one = $in;
            return $this;
        }
        function price($in){
            $this->two = $in;
            return $this;
        }
        function shell(){
            return $this->one.'==='.$this->two;
        }   
    }
    $know = new knowthis;
    //echo $know->get(11)->price(11)->shell();// 输出 11===11
    print_r($know->get(11));
    /*输出(两个不要同时输出,上面的echo 会对下面有影响) 
        knowthis Object ( 
            [one:knowthis:private] => 11 
            [two:knowthis:private] => 
        )
    */

js 对象中的this:

<script>
var people={
    name:'xm',
    sex:'man',
    birth:'1990',
    age:function(){
        var that = this;
        function getage(){
            var y = new Date().getFullYear();
            return y - this.birth;
        }
        return getage();
    }
}
people.age();
</script>

js中的函数有apply(call)方法,可以用来绑定this的指向。
function.apply(str,array)
str:this 指向的对象。
array:function 的参数。

function getage(){
            var y = new Date().getFullYear();
            return y - this.birth;
        }
var people={
    name:'xm',
    sex:'man',
    birth:'1990',
    age:getage
}
people.age();
getage.apply(people,[]);//this 指向polple, getage的参数为空

猜你喜欢

转载自blog.csdn.net/qq_36211859/article/details/82343781
今日推荐