Skip navigation.
Home

Generating nice HTML with PHP... forget the PHP.

Bad Architecture

Jumping in and out of HTML in PHP is a fundamental part of the language but what if you didn't know that? What if you wanted PHP to generate some nicely formatted HTML? Props to Thiemo Mättig for sending in this WTF.

This is probably the most 'clever' way of adding in tabs and new lines to format HTML.

<?php
$n
= chr(13);
$t = chr(9);
$nt = $n.$t;
$ntt = $n.$t.$t;
$nttt = $n.$t.$t.$t;
$ntttt = $n.$t.$t.$t.$t;
$nttttt = $n.$t.$t.$t.$t.$t;
?>

and a little furthur...

<?php
$html
.= $nttt.'<table>';
$html .= $ntttt.'<tr>';
$html .= $nttttt.'<th>...</th>';
$html .= $nttttt.'<th>...</th>';
$html .= $ntttt.'</tr>';
$html .= $ntttt.'<tr>';
$html .= $nttttt.'<td>...</td>';
$html .= $nttttt.'<td>...</td>';
$html .= $ntttt.'</tr>';
$html .= $nttt.'</table>';
?>

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

I don't see any problem with the $html block other than the use of the space and tab variables, which are pretty useless--especially given the fact that there are no newline breaks added to the ends of the lines, so it'll be one long line of HTML code with these spaces and tabs embedded in it. It'd be easier to just add the white space in the single quotation marks if they want to output neatly formatted HTML.

I often save my HTML to variables rather than having blocks of HTML with PHP embedded in it. It makes the code easier to read. However, the best choice is to completely separate your HTML from your [logic] PHP with some kind of templating system (the templating system may, of course, be HTML with embedded PHP).

Ascii 13 is newline. Ascii 32 is space. God know why he did it that way and not the usual "\n" "\t".

the best way is object-oriented separation. Templating is a clever halfway implementation of that for non-OO languages, but PHP has been suffieciently one for some time.

Saving your page to a var then dumping it at the end is dumb for a couple reasons:

  1. PHP's output buffering can do this for you.
  2. If you've already called ob_start(), you're wasting memory - the content goes into $html and the output buffer.
  3. It's bad for interactivity, since there can be a delay while the script runs before any content is sent to the client. If you output everything immediately, portions of the page will load and display in the browser while any time-consuming processing occurs.

What's wrong with
$html='


...
...


...
...

';

but I'm all html/xhtml...

~sigh~

>

$html = '
<table>
<tr>
<th>...</th>
<th>...</th>
</tr>
<tr>
<td>...</td>
<td>...</td>
lt;/tr>
</table>';

but like i said, im the kind of person who can read an html/xhtml page... without linebreaks or spaces...

argh... it had spaces...

oh forget it, plz delete these 3 comments.

and maybe you should put in red something so I'd know this isn't your average comment box...

taking my spaces and codes... ~sigh~

to me this seems like a nice, concise solution to the problem that usual php print_()ing htlm statements just render illlegible code with unhuman linelengths. So I think this is a nice solution. What is the WTF here?

Seems ugly to me, but regardless of that, there are some things i would like to clarify here.

chr(10) aka LF (line feed)
chr(13) aka CR (carriage return)

Windows line separator is CR, LF
Unix/Linux - LF
Mac - CR (afaik)

So is this Mac code or an error?..

Bahahaha ... thats good....

ohh correct url, my bad

anonymous, you shouldn't echo html from php, either. PHP is a HTML-embedded scripting language. It makes no sense to incur the overhead of PHP at all for static HTML. Obviously the case is different if you're pulling content from a database.

You can use something like Savant to help you keep your logic/data code away from your presentation.

Actualy, buffering via ob_start() is faster than $html .=

I think I actually stopped breathing for a second when I read that

omfg. I cant believe yall ac

omfg. I cant believe yall actually commented seriuosly on that.

Post new comment




*

  • Web and e-mail addresses are automatically converted into links.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <pre> <p> <br /> <br>