php中echo和return的区别

来源:互联网 发布:草图大师连接不到网络 编辑:程序博客网 时间:2024/05/21 15:43

一、使用return,输出无内容;但是二实例中,将return改为echo,就会有结果1000;三实例中同样使用return

,结果却有了,同为1000;具体原因是:echo为输出值;return为返回值却不输出,若想有输出需要再次使用echo输出。

<?

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

 

?>

三、

<?

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


$money = new Human();
echo $money->showMoney();

 

?>

 

 

 

 

0 0
原创粉丝点击