Php return usage instance analysis, phpreturn instance Analysis _ PHP Tutorial

Source: Internet
Author: User
Example analysis of return usage in php and phpreturn instance analysis. Php return usage example analysis, phpreturn instance analysis this article describes the use of php return. Share it with you for your reference. The specific analysis is as follows: first, it means the analysis of the return usage instance in php, and the analysis of the phpreturn instance.

This example describes the use of return in php. Share it with you for your reference. The specific analysis is as follows:

First, return () is a language structure rather than a function. it must be enclosed in parentheses only when the parameter contains an expression. Parentheses are usually not used when a variable is returned, which can reduce the burden on PHP.

Basic usage:

A) return expression // return an expression result
B), return (expr) // function expression
C), return // directly return, or return a null value

Note: it is best not to use return ($ val.

1. if return is executed, the content following the return statement will not be executed;

Function add ($ a, $ B) {return $ a + $ B; return $ a * $ B;} $ c = add (5, 3 ); // The obtained $ c value can be used elsewhere in the program! Echo $ c;

Output result: 8. only $ a + $ B is executed, and $ a * $ B is not executed.

2. return can be a function return value or a null value, depending on the specific usage, for example:

function test($a){if($a>10){ return "a>10";}else{ return "a<10";}$b=45;$c=$b-$a;echo $c;}

In this example, when you call this function and then give a number, it returns a string, and the code:

$b=45;$c=$b-$a;echo $c;

Will never be executed.

3. call return.

A. php is as follows:

<?phpinclude("b.php");echo "a";?>

B. php is as follows:

<?phpecho "b";return;?>

Output result: ba.

A. php is as follows:

<?phpinclude("b.php");echo "a";?>

B. php is as follows:

<? Phpecho "B"; exit; // end the entire current script?>

Output result:.

4. an important role of return: return value

Example 1:

function test(){ $a=array(1,2); return $a;}$b=test();print_r($b);

Example 2:

function test(){ $a=array(1,2);}$b=test();print_r($b);

Output result: Example 1 is output normally, and Example 2 is empty. Why? Because if the return value is not used in the rest () function, there is only a process in the function, but no result is given to rest (). Of course there will be no value output when calling this function. Of course, we can also write print_r ($ B) into test () and print it directly in the function. However, in many cases, we call operations outside the function, therefore, return a value to the outside with return.

I hope this article will help you with php programming.

Examples in this article describes the use of return in php. Share it with you for your reference. The specific analysis is as follows: first, it means...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.