When defining a function, the parameter list in parentheses after the function name is the interface used by the user to pass data into the function when calling the function, and the return value of the function returns the result of the function execution to the caller. If a function does not return a value, it can only be counted as an execution process. It is not enough to just rely on functions to do something. Sometimes it is necessary to use the results of function execution in script programs. Due to the difference in the scope of variables, the script program calling the function cannot directly use the information in the function body. We have previously explained the three ways in which the parameters of php custom functions are passed between functions. Here we mainly explain the return value of php custom functions. Typically, the way a function passes its return value to the caller is by using the keyword return or return() function.
The function of return is to return the value of the function to the caller of the function, that is, to return program control to the caller's scope. If the return keyword is used in the global scope, the execution of the script will be terminated. When the return statement is used in the function body, it has the following two functions:
1. The return statement can return any value determined in the function body to the function caller.
2. Return program control to the caller's scope, that is, exit the function. If the return statement is executed in the function body, the statements following it will not be executed.
Use the table() function to change the function from simply outputting tables to the function of creating tables. As long as the table() function is called, it must output a table with the table name, number of rows, and number of columns specified by passing parameters. If you put all the output content in the function body into a string, and use the return statement to return the string containing the table data. Users can not only output the obtained string directly to the display form, but also store the obtained moral form in a database or file, or have other string processing methods.
The code is as follows:
<?php function table($table_name, $rows, $cols){ $str_table = ""; $str_table .= "<table align='center' border='1' width='600'>"; $str_table .= "<caption><h2>$table_name</h2></caption>"; for($i=0; $i<$rows; $i++){ $color = $i%2 == 0?"#ffffff":"#dddddd"; $str_table .= "<tr bgcolor='.$color.'>"; for($j=0; $j<$cols; $j++){ $str_table .= "<td>".($i*$cols+$j)."</td>"; } $str_table .= "</tr>"; } $str_table .= "</table>"; return $str_table; } $str = table("3行4列的表格",3,4); echo table("4行5列的表格",4,5); echo $str; ?>
The above will output the following picture:
Instructions: In the above example, all the output contents of the table() function are accumulated into a string $str_table, and the return statement is used at the end of the function to return $str_table. In this way, when the function table() is called, not only some data is passed to the inside of the function in the form of parameters, but also the function is executed, and the value returned by the return statement can also be used at the calling function, and the value returned from the function Values can be used in scripts like any other value. For example, assign it to a variable, output it directly, or participate in calculations, etc.
Note: The return statement can only return one operand, that is, it can only return one value, and cannot return multiple values at one time. If you want to return multiple values, you must define an array in the function, store the return value in the array, and return it.
【Recommended related tutorials】
1. "php.cn Dugu Jiujian (4)-php video tutorial"
2. php programming from entry to mastering a full set of video tutorials
3. php practical video tutorial
The above is the detailed content of Detailed explanation of the return value of PHP custom function. For more information, please follow other related articles on the PHP Chinese website!