Example analysis of return usage in PHP _php tutorial

Source: Internet
Author: User

Example analysis of return usage in PHP


This article mainly introduces the use of return in PHP, example analysis of the function of return in PHP and the common use of skills, with a certain reference value, the need for friends can refer to the following

This example describes the use of return in PHP. Share to everyone for your reference. The specific analysis is as follows:

First, it means to return, and return () is a language structure, not a function, and it needs to be enclosed in parentheses only if the argument contains an expression. When a variable is returned, usually without parentheses, this can reduce the burden on PHP.

Basic usage:

A), return expression//returns an expression result
b), return (expr)//function expression
c), return//return directly, or return null value

Note: It is best not to use return ($val) this way.

1, if the return is executed, then the content after the return statement will not be executed;

?

1

2

3

4

5

6

function add ($a, $b) {

return $a + $b;

return $a * $b;

}

$c = Add (5,3);//The $c value obtained can be used elsewhere in the program!

Echo $c;

Output: 8, execute only $a+ $b, $a * $b not executed.

2, return can be a function return value, you can also return a null value, depending on the use, for example:

?

1

2

3

4

5

6

7

8

9

10

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 an any number, it returns a string, and the code:

?

1

2

3

$b = 45;

$c = $b-$a;

Echo $c;

Will never be executed.

3, the call about return.

a.php as follows:

?

1

2

3

4

Include ("b.php");

echo "a";

?>

b.php as follows:

?

1

2

3

4

echo "B";

Return

?>

Output result: BA.

a.php as follows:

?

1

2

3

4

Include ("b.php");

echo "a";

?>

b.php as follows:

?

1

2

3

4

echo "B";

Exit Ends the entire current script

?>

Output result: A.

4. One important function of return: return value

Example 1:

?

1

2

3

4

5

6

function Test () {

$a =array ();

return $a;

}

$b =test ();

Print_r ($b);

Example 2:

?

1

2

3

4

5

function Test () {

$a =array ();

}

$b =test ();

Print_r ($b);

Output Result: Example 1 normal output, example 2 output is empty. Why is it? Because if you do not return a value with return in the function rest (), there is only a procedure inside the function, and no result is given to rest (), and of course there is no value output when the function is called. Of course, we can also write Print_r ($b) into the test (), print directly in the function, but many times, we will call the operation outside the function, so return a value with return to the outside.

I hope this article is helpful to everyone's PHP programming.

http://www.bkjia.com/PHPjc/962407.html www.bkjia.com true http://www.bkjia.com/PHPjc/962407.html techarticle example analysis of return usage in PHP This article mainly introduces the use of return in PHP, examples of the function of return in PHP and the common use of skills, with a certain reference value, need ...

  • Related Article

    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.