theme development - Should we use ob_start() in WordPress short code

We have used our short code like bellowob_start();$rand = uniqid();?><div class="faq-item-sm panel panel-defa

We have used our short code like bellow

   ob_start();
    $rand = uniqid();


    ?>
    <div class="faq-item-sm panel panel-default">
        <div class="panel-heading" id="headingTwo">
            <h4 class="panel-title"><a data-toggle="collapse" href="#faq<?php echo $rand; ?>" data-parent="#accordionFaq" class="collapsed">  <?php echo $question; ?><span class="caret-toggle closed">–</span><span class="caret-toggle opened">+</span></a></h4>
        </div>
        <div id="faq<?php echo $rand; ?>" class="panel-collapse collapse <?php if($faq_active == true){ echo 'in';} ?> ">
            <div class="panel-body">
                 <?php echo $answer; ?>
            </div>
        </div>
    </div>
    <?php
    return ob_get_clean();

Is it best Practice to use multiple times echo and ob_start in php. Is this effect in Performance.

We have used our short code like bellow

   ob_start();
    $rand = uniqid();


    ?>
    <div class="faq-item-sm panel panel-default">
        <div class="panel-heading" id="headingTwo">
            <h4 class="panel-title"><a data-toggle="collapse" href="#faq<?php echo $rand; ?>" data-parent="#accordionFaq" class="collapsed">  <?php echo $question; ?><span class="caret-toggle closed">–</span><span class="caret-toggle opened">+</span></a></h4>
        </div>
        <div id="faq<?php echo $rand; ?>" class="panel-collapse collapse <?php if($faq_active == true){ echo 'in';} ?> ">
            <div class="panel-body">
                 <?php echo $answer; ?>
            </div>
        </div>
    </div>
    <?php
    return ob_get_clean();

Is it best Practice to use multiple times echo and ob_start in php. Is this effect in Performance.

Share Improve this question asked Aug 12, 2019 at 18:52 Arifur RahmanArifur Rahman 112 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

It’s one way to do it.

I like it, because it allows you to write HTML code pretty easily and it makes it pretty obvious and easy to maintain. But it’s just opinion based, I guess.

It doesn’t harm your performance to much. You shouldn’t use output buffering to generate whole content of the site, because you want to start sending response to the browser as soon as possible, but this is not the case here...

You can also:

  • prepare the HTML output by concatenating strings (it’s harder to read and easier to make some html errors, IMHO),
  • use some “view” pattern

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745248122a4618509.html

相关推荐

  • theme development - Should we use ob_start() in WordPress short code

    We have used our short code like bellowob_start();$rand = uniqid();?><div class="faq-item-sm panel panel-defa

    9小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信