Home  >  Article  >  Backend Development  >  State mode, return no return value problem

State mode, return no return value problem

WBOY
WBOYOriginal
2016-08-08 09:06:371144browse

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>

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>

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