State mode, return no return value problem

WBOY
Release: 2016-08-08 09:06:37
Original
1155 people have browsed it

The code is as follows:
When I use echo, it has a value. Why can’t I get the value when I use return?

<code>class GoodNight implements IState  
{ 
    public function WriteCode(Work $w)  
    {  
        if($w->hour<22)  
        {  
           return Yii::t('yii','Good night'); 
        }else{  
            $w->SetState(new GoodAtNight());  
            $w->WriteCode();  
        }   
    }  
} </code>
Copy after login
Copy after login

Reply content:

The code is as follows:
When I use echo, it has a value. Why can’t I get the value when I use return?

<code>class GoodNight implements IState  
{ 
    public function WriteCode(Work $w)  
    {  
        if($w->hour<22)  
        {  
           return Yii::t('yii','Good night'); 
        }else{  
            $w->SetState(new GoodAtNight());  
            $w->WriteCode();  
        }   
    }  
} </code>
Copy after login
Copy after login

Thanks for the invitation! Because you instantiate it and use it, it is return to return instead of output, so the value will not appear, which is normal! If you use echo directly, you can see the output value.

<code>class GoodNight implements IState  
{ 
    public function WriteCode(Work $w)  
    {  
        if($w->hour<22)  
        {  
           return Yii::t('yii','Good night'); 
        }else{  
            $w->SetState(new GoodAtNight());  
            echo $w->WriteCode();  // 在这里echo就有值了
        }   
    }  
} </code>
Copy after login
Related labels:
php
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