QUORA: How should I structure PHP files to output nicely formatted HTML?

Curtis Oden added this answer.

Start by putting <?php on the first line and ?> on the last line. You’ll now have to either explicitly ‘echo’ the HTML to the screen, or close PHP ( ?> ) before your HTML, and then reopen it after.

Here’s a rough example:

<?php
//This is my PHP application

//The whitepace above won’t go to the browser
//START awesome conditional code.

$helloWorld = ‘Hello World’;

//END awesome conditional code.

?><?php
//DO NOT LET ANY WHITESPACE APPEAR BEFORE THE DOCTYPE OR THINGS WILL BREAK!
?>
Hello Wild World

Sometimes I just like to shout: “.”

And sometimes i like to whisper: “
<?php
echo ($helloWorld);
?>
And sometimes ….
<?php
echo (‘I feel fancy and feel like singing”‘ . $helloWorld . ‘.”‘);
?>

<?php
//This is the end…
?>

And there are a dozen other ways to do this. Just remember: what happens in PHP, stays in PHP.

See question on Quora

Read more…

Leave a Reply