Thursday 17 March 2016

PHP/HTML output: echo vs return, output buffering, and syntax highlighting challenges

I prefer to write html outside of php tags, so eclipse can display the html with proper syntax highlighting. The concatenation method is harder to write and doesn't highlight (see the two examples below).




I apologize for the length. These examples are very simple, so it should be an easy read.



I DON'T like this, too many 'quotes' and $o's, and no syntax highlighting!:




display($something){
$o = '';
$o .= '
';

$o .= $something;
$o .= '
';
return $o;
}

// I want to be able to do this:
echo display(display('something'));


This gives a function the chance to finish the closing or even add additional html afterwards. The above example is functionally what I'm looking to do, but for these reasons ('quotes', $o's, and syntax highlighting) I haven't created a system like this.




The following example is how I prefer to write html, but I can't nest output, because it doesn't return!




function display($something){ ?>





// I'd like to do this, but I can't
display(display('this doesn't return anything to the first function call...'));


This is where output buffering comes in, I'll get back to that in a second...



What I'm envisioning:



I'd like to be able to use func_get_args() to accomplish something like this (note, this will apply to OOP objects, just keeping it simple here):





some_panel( title_style_1('Sample Header'),
panel_content(you_tube($vid_id)),
small_img_frame($img_src) );


You'd basically be able to take any of these output functions and nest them any way you like. Just like you can put any

inside any

and vice versa. Only problem is, you have to make sure you close the tags... And, in this case, you could add any markup at the end or in between children.




This is where the output buffering comes in




function display($something){
ob_start(); // <---- Start buffer ?>




}

// Now I can do this!!!
echo display(display('this should work!'));


And, drum roll please.... THE QUESTION:



If I'm repeatedly buffering potentially hundreds or even thousands of times per request, is this going to be a performance hit? I've read posts that warn against output buffering due to:




Reliability: If somewhere else a buffer was started, from what I read, it sounds like these nest and can potentially conflict.



Maintainability: If a buffer is started, you have to guarantee it will be stopped.



For my system, if output buffering is started and stopped in the same function call, these things seem to be OK. It's the excessive iteration of potentially 1000's of items that each start/stop output buffering for a single

  • that I'm worried about.



    Also, if anyone knows of any frameworks or better ways to do what I'm trying to do, any suggestions would be appreciated!

  • No comments:

    Post a Comment

    c++ - Does curly brackets matter for empty constructor?

    Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...