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

黄舟
黄舟Original
2017-06-25 10:15:172693browse

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();
 
?>


二、

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

3.

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

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!

Statement:
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