Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the return value of the PHP method

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the knowledge of "what is the return value of the PHP method". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

The return value of the PHP method

Not only PHP, but most programming language functions, or methods, can use return to define the return value of a method. From the point of view of the term function, it is itself a computational operation, so there is always a result of calculation, and if you process the result in the body of the method, such as persisting, then the function does not have to return anything. The result of the calculation is to be used externally, so it is time to return the result.

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 will not be executed later.

}

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

Not using return or directly return; will return NULL,return, which will block the execution of subsequent code in the method body. If you want to return multiple values, you can only use an array to assemble the 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 relatively easy to understand. Here's the big story, and the return value declaration is a very eye-catching sight in the new features of PHP7.

Function testE ($a, $b): bool

{

If ($astatb = = 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 example above, if the returned value is not of type bool, the error of TypeError will be reported directly.

So what are the benefits of defining a return value type declaration? We've covered the benefits of type declarations in the case of PHP method parameters, so I won't dwell on them here, whether it's a parameter type declaration or a return value type declaration.

Function testF ($a, $b): array

{

Return [

$a + $b

$a * $b

]

}

Var_dump (testF (1,2)); / / [3,2]

Interface iA {

}

Class An implements iA

{}

Class B extends A

{

Public $b = 'call me financing'

}

Function testG (): a

{

Return new B ()

}

Function testH (): B

{

Return new B ()

}

Function testI (): iA

{

Return new B ()

}

Var_dump (testG ()); / / instance of B

Var_dump (testH ()); / / instance of B

Var_dump (testI ()); / / instance of B

Similarly, arrays and class types can be declared and defined. In addition, however, the return value declaration can also define the void. In fact, its function is to declare that the return value is NULL, not directly write: NULL, but can only be declared with: void.

Function testJ (): void

{

Echo "testJ"

/ / return 1

}

Var_dump (testJ ())

At this point, if you try to make any return returns, 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 excellent features from 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 document: https://www.php.net/manual/zh/functions.returning-values.php

This is the end of the content of "what is the return value of the PHP method". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report