duanke9540 2013-01-14 19:18
浏览 40
已采纳

PHP返回vs echo

I have a series of functions that is suppose to, in the end create a form. they reside in a class such as the following example:

class ExampleForm{
    protected $_html = '';

    protected $_element;

    public function __construct($element){
        $this->_element = $element;   

    protected function _open_form(){
       echo $this->_html .= '<form>';
    }

    public function create_form(){
       $this->_open_form()
       $this->_html .= $this->_element;
       echo "some content";
       $this->_close_form()
    }

    protected function _close_form(){
        echo $this->_html .= '</form>'
    }

    public function __toString(){
        return $this->_html;
}

Instantiated as:

$element = 'some element';
new ExampleForm($element);

A more comprehensive example can be seen on Github on line 86 this content gets echoed out, How ever its echoed out side the form.

If you want to see a live action demo of this Then you'll deff wan't me. at the end of the day if you inspect the form on the web page, you'll see above the form opening tag two hidden fields that need to be inside the form tags.

Now one would think this would create a form such as:

<form>
    element
    some conent
</form>

How ever, what I get is:

some content
<form>
    element
</form>

So can some one tell me, based on what I have above why I am getting my "some content" echoed out side the form tag even though I have echoed the opening form tag?

even if I do not echo the opening form tag the "some content" still appears out side the opening form tag.

  • 写回答

2条回答 默认 最新

  • doudun2565 2013-01-14 19:26
    关注

    Return will simply return the variable without echoing it, where as echo will echo it out when it is called. So if you return a variable and then echo something. So in your function create_form(), instead of echo "some content"; just do $this->_html .= "some content";

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料