Home > Article > Backend Development > State mode, return no return value problem
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>
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>