Home > Backend Development > PHP Tutorial > Detailed introduction to the difference between php return and echo

Detailed introduction to the difference between php return and echo

黄舟
Release: 2023-03-11 11:38:02
Original
2694 people have browsed it

1. Using return, the output will be empty; but in the second example, if return is changed to echo, the result will be 1000; In the three examples, return

is also used, but the result is 1000. The specific reason is: echo is the output value; return is the return value but not output. If you want to have output, Use echo output again.

<?
class Human{  
private $money = 1000;  
public function showMoney(){    
return $this->money; }}
$money = new Human();
$money->showMoney();
 
?>
Copy after login


二、

<?
class Human{  
private $money = 1000;  
public function showMoney(){    
echo $this->money; }}
$money = new Human(); 
$money->showMoney();
?>
Copy after login

3.

<?
class Human{  
private $money = 1000;  
public function showMoney(){    
return $this->money; }}
$money = new Human();
echo $money->showMoney();
 
?>
Copy after login

The difference between echo and return:

——>echo is outputStringTo the browser, return terminates the execution of the following code and returns a value.

PHP=>PHP uses return, PHP=>browser and javascriptuse echo

The above is the detailed content of Detailed introduction to the difference between php return and echo. 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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template