Let's talk about the difference between the return statement and echo (detailed explanation and examples)

慕斯
Release: 2023-03-10 10:34:02
Original
2494 people have browsed it

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! ! !

Let's talk about the difference between the return statement and echo (detailed explanation and examples)

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);
Copy after login

Lets talk about the difference between the return statement and echo (detailed explanation and examples)

Lets talk about the difference between the return statement and echo (detailed explanation and examples)

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)$strin 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(); ?>
Copy after login

The execution result is as follows:

Lets talk about the difference between the return statement and echo (detailed explanation and examples)

When we need to get the return value,

'; $str .= ' 思君令人老,岁月忽已晚。
'; $str .= ' 人生如逆旅,我亦是行人。
'; $str .= ' 片云天共远,永夜月同孤。
'; $str .= ' 君生我未生,我生君已老。
'; $str .= ' 一川烟草,满城风絮,梅子黄时雨。
'; $str .= ' 山中何事?松花酿酒,春水煎茶。
'; return $str; } $return = demo1(); echo $return; ?>
Copy after login

The running results are as follows:

Lets talk about the difference between the return statement and echo (detailed explanation and examples)

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
Why is "this" defined as undefined in Vue 3 function Please look at the following simple component example in Vue3: