The last article introduced you to "Teach you step-by-step how to use PHP functions (selected)". This article continues to talk about the role of the return statement and the difference between echo (detailed explanation and examples) ), let’s go and have a look now! ! !
The role of the return statement:
A function with a return statement is a function with a return value
A function without a return statement is an execution function
Function 1:If there is a return statement in the function and the execution capability is statement, then the execution result of the function can be received by the variable;
Benefits:You can continue to use the result to do calculations or other operations
Function 2:If the return statement is executed during the execution of the function, then the subsequent code will not be executed.
Note:A function can have multiple return statements, but the program has only one;
The difference between echo and return:
If the result of the function execution needs to be used elsewhere, then the function needs to use the return statement. If not, then the function can Use echo output;
'; $str .= ' 思君令人老,岁月忽已晚。
'; $str .= ' 人生如逆旅,我亦是行人。
'; $str .= ' 片云天共远,永夜月同孤。
'; $str .= ' 君生我未生,我生君已老。
'; $str .= ' 一川烟草,满城风絮,梅子黄时雨。
'; $str .= ' 山中何事?松花酿酒,春水煎茶。
'; } //代码显示结果一: demo(); $result = demo(); var_dump($resurt);
Parsing code:
Call function method: function name Add (), which is the [demo();] of the above formula. At this time, we refresh but there is no content. The code display result is as shown above. Then we use return to accept to see if there is a return value. The result of refreshing is Null ( Empty), the code result is as above; it can be seen that when we call the function to return the value, we do not get the corresponding result, so we cannot (echo) the result;
So the above function is the execution process, In the above function, we are just declaring a string, so we will not get any results;
When we output(echo)$str
in the function; and then call the function, we will get The content of the function body,
code is as follows:
'; $str .= ' 思君令人老,岁月忽已晚。
'; $str .= ' 人生如逆旅,我亦是行人。
'; $str .= ' 片云天共远,永夜月同孤。
'; $str .= ' 君生我未生,我生君已老。
'; $str .= ' 一川烟草,满城风絮,梅子黄时雨。
'; $str .= ' 山中何事?松花酿酒,春水煎茶。
'; echo $str; } demo(); ?>
The execution result is as follows:
When we need to get the return value,
'; $str .= ' 思君令人老,岁月忽已晚。
'; $str .= ' 人生如逆旅,我亦是行人。
'; $str .= ' 片云天共远,永夜月同孤。
'; $str .= ' 君生我未生,我生君已老。
'; $str .= ' 一川烟草,满城风絮,梅子黄时雨。
'; $str .= ' 山中何事?松花酿酒,春水煎茶。
'; return $str; } $return = demo1(); echo $return; ?>
The running results are as follows:
Analysis:We change function demo into demo1(), Because function names cannot have the same name, we replace echo with return at this time. Then we start calling the function and call demo1. The code demonstration is as above. When we call the function, it is equivalent to assigning str to demo1, so when When we enter return, the string we wrote will be output.
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of Let's talk about the difference between the return statement and echo (detailed explanation and examples). For more information, please follow other related articles on the PHP Chinese website!