Jump to content

Cleaning Up Code


shadowayex

Recommended Posts

And you said I can embed variables. You mean like {$variable} <---- that type. Or what?And I would use this for large chunks of output, but just echo little bits? Is this slower than echoing? Or is the difference so small it doesn't matter?
Heredoc is the same as a multi-lined double-quoted string. It's probably going to be slower since it has to check for variables inside the string. But the difference is so small, that no one even cares. You don't have to be picky on every little thing. PHP 5.3.0 has a nowdoc which is a multi-lined SINGLE-QUOTED string. Nowdoc syntax is like this:
echo <<<'END'END;

It's faster than Heredoc since it does not have to parse variables inside the String, but PHP 5.3 isn't released yet.For now, just use Heredoc. A small performance difference doesn't really matter with technology nowadays. It's better to have neat code than messy code that yields only a small performance gain.

Link to comment
Share on other sites

So, after reading through everything, would it be the most effective to build pages in this syntax?

<?php//all the php logic, from queries to setting result variablesecho <<< EOT<!---the whole HTML page and PHP variables used to show reesults of PHP scripts----->EOT;//closing scripts?>

And if that's right, the performance difference would be so small that no one would really notice or care right?

Link to comment
Share on other sites

If I'm outputting a large block of HTML, I normally just close the PHP.

<?php//all the php logic, from queries to setting result variables?><!---the whole HTML page and PHP variables used to show results of PHP scripts-----><?php//closing scripts?>

Link to comment
Share on other sites

The difference between HEREDOC and single quotes is minimal. However, how you choose to structure your code can have an influence as how fast the page appears to load from a user's point of view.I find it's best to do things like this:

<?php/* PHP logic needed before outputting anything. This includes headers, cookies, session handling and more */echo '<!-- Start outputting anything that can be outputted without further PHP.This often includes the header and menu.Whether you use HEREDOC, single or double quotes won't make much of a difference -->';//Send the output as soon as possibleob_flush();/* PHP logic needed to process the contents of the current page. This can often include connecting to a database and fetching the results. */echo '<!-- Output the results of whatever you were doing (DB manipulations?) appending them to the current markup. Also finish the page with </body></html> and all. -->';//Do the following only if there is more PHP that has to be done after the page has loadedob_flush();/* PHP logic that has to be done after the page has loaded. This can include creating log files, setting some sort of timers, etc. */?>

Link to comment
Share on other sites

Got the page rewritten again. I've run into one question though. Would it be better to display the last chunk of the page by closing PHP and opening it up for any variables, or using heredoc again. Here's what I mean:Closing off PHP:

else {?>    <div style="text-align: center;">        <p style="text-align: left;">Welcome to the Official O.R.O. Website. As of right now, there's not much to it.        But our goal is to become one big, happy, anime-loving family with members from all over the world!        Yaaaaaaay!!!!</p>        <img src="Images/paradas.jpg" />    </div>    <div style="float: left;">        <h2 style="text-align: center;">Latest Club Update/News</h2>        <div class="club">            <?php echo $clubupdate; ?>        </div>    </div>    <div>        <h2 style="text-align: center;">Latest Site Update/News</h2>        <div class="site">            <?php echo $siteupdate; ?>        </div>    </div>    <p style="text-align: center;"><a href="news.php">More Updates</a></p><?php}mysql_close($link);?>

Using Heredoc:

else { <<< EOT    <div style="text-align: center;">        <p style="text-align: left;">Welcome to the Official O.R.O. Website. As of right now, there's not much to it.        But our goal is to become one big, happy, anime-loving family with members from all over the world!        Yaaaaaaay!!!!</p>        <img src="Images/paradas.jpg" />    </div>    <div style="float: left;">        <h2 style="text-align: center;">Latest Club Update/News</h2>        <div class="club">            {$clubupdate}        </div>    </div>    <div>        <h2 style="text-align: center;">Latest Site Update/News</h2>        <div class="site">            {$siteupdate}        </div>    </div>    <p style="text-align: center;"><a href="news.php">More Updates</a></p>EOT;}mysql_close($link);?>

Link to comment
Share on other sites

O think I'll use the heredoc way. I'm going to use this page as kind of a template, and on some other pages there's a lot of variables, and {$variable} is easier to type :). I'm lazy like that.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...