dsfg3241 2011-06-07 15:57
浏览 96
已采纳

如何在不使用echo的情况下显示片段的输出

The documentation on snippets in modx: http://rtfm.modx.com/display/revolution20/Snippets

Near the top of the doc it says: Note how we returned the code rather than echo'ed the content out. Never use echo in a Snippet - always return the output.

This doesn't display anything:

return $product_attribute $product_info[0][$column_name];

This does display:

echo $product_attribute $product_info[0][$column_name];

If I can't echo the content how do I get it to print in the html page?

  • 写回答

3条回答 默认 最新

  • dongya1875 2011-06-07 16:06
    关注

    It basically means that you can echo the returned value rather than echo the value in the function itself. In OOP programming, echoing (or printing) to the screen is strictly monitored.

    For example I have this function

    function testExample($var) {
        return $var*2;
    }
    

    So when I need to echo it, I just need to

    echo testExample(5);
    

    Instead of this (bad practice)

    function testExample($var) {
        echo $var*2;
    }
    

    The reason is that when you print value in the function, you can only use that function for printing the value, which isn't reusable at all. But by returning it, you can now use it for printing, or assigning to another variable or re-calculating on it.

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

报告相同问题?

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改