【问题标题】:content displaying out side of html tags for return and echo显示在 html 标记之外的内容以用于返回和回显
【发布时间】:2013-01-14 22:33:25
【问题描述】:

我知道 echo 会回显内容并 return 将返回内容以供进一步处理。然而,我已经说过函数:

class Content{

    protected $_html = '';

    public function display_content(){
        $this->_html = 'content';
    }

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

然后我有以下几点:

$content = new Content();
<p><?php $content->disaply_content(); ?></p>

我明白了:

<p></p>
content

作为页面的源代码。不管我是回显还是返回都没关系,无论哪种方式,它都会显示在标签的外面。

想法?

【问题讨论】:

  • 显示页面时还有其他代码吗? 'content' 不应该出现在提供的代码中。

标签: php html return echo


【解决方案1】:

我不确定第二个“内容”来自哪里,但请注意:

<?php $content->display_content();?>

&lt;p&gt; 标记之间不会显示任何内容。你应该使用:

<?php echo $content->display_content();?>

(我认为 disaply_content() 是问题中的错字,而不是代码中的错字)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-10-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-15
    • 1970-01-01
    相关资源
    最近更新 更多