Category:PHP code: Difference between revisions

From Robupixipedia
Jump to navigationJump to search
(added example code reason I disliked PHP code)
m (made it a list of things)
Line 25: Line 25:
<h1>title</h1>
<h1>title</h1>


<p>Here is the thing for today: <?php $thing = $DB->getThing(date("Y.m.d")); echo $thing; ?> I love it!</p>
<p>Here is a list of things for today: <?php echo "<ol>"; $list_of_things = array();  $list_of_things =  
$DB->getThings(date("Y.m.d")); foreach($list_of_things as $item) { echo "<li>" . $item . "</li>"; } echo
"</ol>"; ?> I love them!</p>


</body>
</body>
Line 57: Line 59:
<h1>title</h1>
<h1>title</h1>


<p>Here is the thing for today:  
<p>Here is a list of things for today:  
<?php  
<?php
     $thing = $DB->getThing(date("Y.m.d"));
    echo "<ol>";
     echo $thing;
     $list_of_things = array();
    $list_of_things = $DB->getThings(date("Y.m.d"));
     foreach($list_of_things as $item)
    {
        echo "<li>" . $item . "</li>";
    }
    echo "</ol>";
?>
?>
I love it!</p>
I love them!</p>


</body>
</body>

Revision as of 23:07, 12 December 2007

Not so long ago, I didn't like PHP because I didn't like having "code inside HTML." I preferred Perl, where the code was first, and the HTML was produced by the code. "HTML inside code"

At my job, I was basically required to use PHP, and... I have come to realize it is not actually all that bad. In fact, in several ways, it's pretty awesome.

I actually still believe that, and so here is what happened:

Basically, I think this type of coding is crap:

<source lang="php"> <?php //***********************************************// // // this code is crap and formatted as such // // // fantastic description of great program // // /***********************************************/

<html> <head> </head> <body>

title

Here is a list of things for today: <?php echo "

    "; $list_of_things = array(); $list_of_things = $DB->getThings(date("Y.m.d")); foreach($list_of_things as $item) { echo "
  1. " . $item . "
  2. "; } echo "

"; ?> I love them!

</body> </html> </source>

I would see all types of things written inline, and I couldn't grok the overview of the code for anything.

I finally decided on this type of rule. For simple echo statements, such as

<source lang="php"><?php echo "echo statements"; ?></source>

I would happily code them inline. But if there was actual programming logic, I want that pulled "out"

<source lang="php"> <?php //***********************************************// // // this code is crap, but formatted nicely // // // fantastic description of great program // // /***********************************************/

<html> <head> </head> <body>

title

Here is a list of things for today: <?php echo "

    "; $list_of_things = array(); $list_of_things = $DB->getThings(date("Y.m.d")); foreach($list_of_things as $item) { echo "
  1. " . $item . "
  2. "; } echo "

";

?>

I love them!

</body> </html> </source>

For me, this makes all the difference in the readability of the code.

Subcategories

This category has only the following subcategory.

Pages in category "PHP code"

The following 2 pages are in this category, out of 2 total.