A description of the return value of the PHP custom function

Source: Internet
Author: User
Tags php programming
When defining a function, the argument list in parentheses after the function name is the interface that the user uses to pass the data to the inside of the function when the function is called, and the return value of the function returns the result of the function execution to the caller. If the function does not return a value, it can only be counted as an execution procedure. It is not enough to rely on functions to do something, and sometimes it is more necessary to use the results of the function execution in the script. Because of the difference in scope of variables, the script that invokes the function cannot directly use the information inside the function body. The three ways in which the parameters of a PHP custom function are passed between functions are explained above, and here we mainly explain The return value of a PHP custom function。 Typically, the way a function passes a return value to the caller is by using the keyword returnOr return ()Function.

Return returns the value of the function to the caller of the function, returning the program control to the scope of the caller. If the return keyword is used within the global scope, execution of the script is terminated. When the return statement is used in the body of a function, there are two functions:

1. The return statement can return any determined value in the function body to the function caller.

2. Return the program control to the caller's scope, which is the exit function. If a return statement is executed in the body of the function, the statement following it will not be executed.

Using the table () function, the functions of the function's simple output table are changed to the function of creating the table. Whenever the table () function is called, it must output a table that specifies the table name, the number of rows, and the number of columns by passing parameters. If all the output in the function body is placed in a string, and the return statement is used to return the string that holds the tabular data. Not only can the user get the string directly out of the display table, but it can also store the obtained ethical table in a database or file, or have other string processing methods.

Its code is as follows:

<?phpfunction table ($table _name, $rows, $cols) {$str _table = ""; $str _table. = "<table align= ' center ' border= ' 1 ' width= ' >"; $str _table. = "<caption>

The above will output the following diagram:

Note: The above example adds the contents of all outputs in the table () function to a string $str _table, and returns the $str _table using the return statement at the end of the function. In this way, when the function table () is called, it not only passes some data to the inside of the function as a parameter, but also executes the function, and the value returned by the return statement can be used at the calling function, and the value returned from the function can be used in the script as if it were other values. For example, assign it to a variable, direct output, or participate in an operation.

Note: The return statement can only return one operand, that is, only one value can be returned, and multiple values cannot be returned at a time. If you want to return multiple values, define an array in the function and return the returned values in the array.

"Related tutorials Recommended"

1. "Php.cn lonely Nine Cheap (4)-php video Tutorial"

2. PHP programming from getting started to mastering the full set of video tutorials

3. PHP real-Combat video tutorial

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.