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 badges1 Answer
Reset to default 1It’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