Home  >  Article  >  Backend Development  >  Reference and return of php functions

Reference and return of php functions

蓝冰凌z
蓝冰凌zOriginal
2018-05-04 11:49:452154browse

The references of php functions are the same as the variable references in php, so today we will take a look at some examples of function returns. Friends in need can refer to them. Let’s take a look together.

Look at the code first:

<?php
function &test() {
    static $b=0;//申明一个静态变量
    $b=$b+1;
    echo $b;
    return $b;
}
$a=test();//这条语句会输出 $b的值 为1
$a=5;
$a=test();//这条语句会输出 $b的值 为2
$a=&test();//这条语句会输出 $b的值 为3
$a=5;
$a=test();//这条语句会输出 $b的值 为6
?>

Explain below:

In this way, $a=test(); actually gets It is not a reference return of a function. It is no different from an ordinary function call. As for the reason: this is a regulation of PHP. PHP stipulates that what is obtained through $a=&test(); is a reference return of a function. As for what is a reference return? (The PHP manual says: Reference return is used when you want to use a function to find which variable the reference should be bound to.) This nonsense made me unable to understand it for a long time.

Using the above example to explain, calling a function in the

$a=test() method only assigns the value of the function to $a, and any changes to $a will not occur. It affects $b in the function, and the function is called through $a=&test(). Its function is to point the memory address of the $b variable in return $b and the memory address of the $a variable to the same place. , which produces an effect equivalent to "$a=&$b;" So changing the value of $a also changes the value of $b, so after executing

$a=&test();
$a=5;

, the value of $b became 5.

Static variables are used here to let everyone understand the reference return of the function. In fact, the reference return of the function is mostly used in objects.

Attached is an official PHP example:

Calling a function using $a=test() only assigns the value of the function to $a. Any changes to $a will not affect $b in the function. Calling a function using $a=&test() Well, its function is to point the memory address of the $b variable in return $b and the memory address of the $a variable to the same place, which produces an effect equivalent to "$a=&$b;" So Changing the value of $a also changes the value of $b, so after executing

$a=&test();
$a=5;

, the value of $b becomes 5.

Static variables are used here to let everyone understand the reference return of the function. In fact, the reference return of the function is mostly used in objects

Attached is an official PHP example:

//This is the way how we use pointer to access variable inside the class.
<?php
class talker{
    private $data = &#39;Hi&#39;;
    public function & get(){
        return $this->data;
    }
    public function out(){
        echo $this->data;
    }  
}
$aa = new talker();
$d = &$aa->get();
$aa->out();
$d = 'How';
$aa->out();
$d = 'Are';
$aa->out();
$d = 'You';
$aa->out();
?>
//the output is "HiHowAreYou"

I believe that after reading the case in this article, you have mastered the method of function reference. For more exciting information, please pay attention to other related articles on the PHP Chinese website!

Related articles:

Reference parameters and number of parameters of php function

##PHP——Function_Study notes References of php functions php delay function php sorting function

In-depth analysis of php execution principle (2): function compilation php encryption function php function reference php delay function

The above is the detailed content of Reference and return of php functions. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn