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!