Return value of PHP method

Time:2022-1-12

Not only PHP, but also most programming language functions or methods can use return to define the return value of the method. From the name of function, it is a calculation operation itself. Therefore, the calculation will always have a result. If you process the result in the method body, such as persistent storage, the function does not need to return anything. The calculation result is for external use. At this time, the calculation result should be returned.

 

Return keyword

function testA($a, $b)
{
    echo $a + $b;
}

var_dump(testA(1, 2)); // NULL

function testB($a, $b)
{
    return $a + $b;
}

var_dump(testB(1, 2)); // 3

function testC($a, $b)
{
    return;
    echo $a + $b; //  It won't be executed later
}

var_dump(testC(1, 2)); // NULL

Don’t return or return directly; Will return null, and return will block the execution of subsequent code in the method body. If you want to return multiple values, you can only use arrays to assemble data.

function testD($a, $b)
{
    return [
        $a + $b,
        $a * $b,
    ];
}

var_dump(testD(1, 2)); // [3, 2]

 

Return value type declaration

The return value is easy to understand. The following is the highlight. In the new features of php7, the return value declaration is a very eye-catching scenery.

function testE($a, $b) : bool
{
    if($a+$b == 3){
        return TRUE;
    }else{
        return NULL;
    }
}

var_dump(testE(1, 2)); // true
var_dump(testE(1.1, 2.2)); //TypeError: Return value of testE() must be of the type bool, null returned

As shown in the above example, if the return value is not of bool type, the error of typeerror will be reported directly.

So what are the benefits of defining a return value type declaration? We areAbout PHP method parametersI have introduced the benefits of type declaration, but I will not repeat it here. Whether it is parameter type declaration or return value type declaration, it is the same.

function testF($a, $b): array
{
    return [
        $a + $b,
        $a * $b,
    ];
}
var_dump(testF(1, 2)); // [3, 2]

interface iA{

}
class A implements iA
{}
class B extends A
{
    public $b = 'call me B!';
}

function testG(): A
{
    return new B();
}

function testH(): B
{
    return new B();
}

function testI(): iA
{
    return new B();
}

var_ dump(testG()); //  Example of B
var_ dump(testH()); //  Example of B
var_ dump(testI()); //  Example of B

Similarly, array and class types can be declared and defined. However, in addition, the return value declaration can also define void. Its function is to declare that the return value is null. You can’t write: null directly, but you can only declare it with: void.

function testJ(): void
{
    echo "testJ";
    // return 1;
}
var_dump(testJ());

At this time, if you try to return any value, you will directly report an error: fatal error: a void function must not return a value.

 

summary

We can see that PHP has been absorbing the excellent features of other languages in its continuous development. Obviously, the purpose of adding these type declarations is to prepare for future compilers. This is also an important feature of php8. Let’s wait and see!

Test code:
https://github.com/zhangyue0503/dev-blog/blob/master/php/201911/source/PHP%E6%96%B9%E6%B3%95%E7%9A%84%E8%BF%94%E5%9B%9E%E5%80%BC.php

Reference documents:
[https://www.php.net/manual/zh/functions.returning-values.php][https://www.php.net/manual/zh/functions.returning-values.php]

===============

Official account: hard core project manager

Add wechat / QQ friends: [xiaoyuezigonggong / 149844827] get free PHP and project management learning materials

Tiktok, official account, voice, headline search, hard core project manager.

Station B ID: 482780532

Recommended Today

Vue event handling guide – vue3 update

By Fernando doglioTranslator: front end XiaozhiSource: medium Dream, dry goods, wechat search[move to the world]Pay attention to this dish washing wisdom who is still washing dishes in the early morning. This article GitHub https://github.com/qq449245884/xiaozhi It has been included. There are complete test sites, materials and my series of articles for the interview of front-line large […]