Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > PHP FAQs
| Search | Today's Posts | Mark Forums Read
PHP FAQs This moderated forum is where our PHP experts answer some of the questions they see asked most frequently in the other PHP forums. You cannot post other messages here, use the other PHP forums for that.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the PHP FAQs section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 5th, 2004, 08:23 PM
richard.york's Avatar
Wrox Author
Points: 5,506, Level: 31
Points: 5,506, Level: 31 Points: 5,506, Level: 31 Points: 5,506, Level: 31
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Camby, IN, USA.
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default Code Writing Conventions

The following is intended to outline some common conventions used by programmers to enhance both the readability and understandablity of code. If you've been referred to this page its likely because *someone* couldn't read what the hell you were posting! Please take the following to heart, as its information that will save us and you headaches.

Take for instance the following block of sloppy, unreadable code:

Code:
if ( ! $_POST['submit'] )
   showform();
else {
mysql_connect("localhost", "...", "...") or die("Could not connect to database.");
mysql_select_db("...") or die("Could not use database");
$query = "select id from referee where id=" . $_POST['id'];
$result = mysql_query($query) or die("Error in the query: $query");
 if (mysql_num_rows($result) == 0) {
   echo("Invalid Login");
   showform();
 }
 else {
   echo("Logged In");
   if ( ! $_POST['submitted'] ) {
     login(); ?>
     <form action="index.php" TARGET="_top" method="post">
     <input type="submit" name="submitted" value="Logout">
     </form>
   <?php
   }
   else
     logout();
 }
}
This is a real user-submitted snippet of code BTW, if you're sitting there thinking to yourself; WTF? You're not the only one. Deviations from HTML standards not-with-standing, posting a mess like this makes it difficult for us to answer your questions quickly and easily.

Here's the same code written for the PEAR convention, for the sake of completeness I've also adjusted non-standard HTML and enhanced the logic using isset() and empty():

Code:
if (!isset($_POST['submit'])) {

   showform();

} else {

    mysql_connect('localhost', '...', '...') or die('Could not connect to database.');
    mysql_select_db('...') or die('Could not use database');

    $query = "SELECT `field` FROM `table` WHERE `id`=". $_POST['id'];
    $result = mysql_query($query) or die('Error in the query: '.mysql_error());

    if (mysql_num_rows($result) == 0) {

        echo "<span style=\"color: red;\">Invalid Login</span><br />\n";
        showform();

    } else {

        echo "<span style=\"color: white;\">Logged In</span><br />\n";

        if (!isset($_POST['submitted']) || empty($_POST['submitted'])) {

            login();

            echo "
            <form action=\"index.php\" TARGET=\"_top\" method=\"post\">
                 <input type=\"submit\" name=\"submitted\" value=\"Logout\" />
            </form>\n";

        } else {

            logout();
        }
    }
}
You can find the full PHP PEAR Group's coding convention documented here:
http://pear.php.net/manual/en/standards.php

And then the other popular method, and my personal favorite, is the one true brace convention:
Code:
if (!isset($_POST['submit']))
{
   showform();
}
else
{
    mysql_connect('localhost', '...', '...') or die('Could not connect to database.');
    mysql_select_db('...') or die('Could not use database');

    $query = "SELECT `field` FROM `table` WHERE `id`=". $_POST['id'];
    $result = mysql_query($query) or die('Error in the query: '.mysql_error());

    if (mysql_num_rows($result) == 0)
    {
        echo "<span style=\"color: red;\">Invalid Login</span><br />\n";
        showform();
    }
    else
    {
        echo "<span style=\"color: white;\">Logged In</span><br />\n";

        if (!isset($_POST['submitted']) || empty($_POST['submitted']))
        {
            login();

            echo "
            <form action=\"index.php\" TARGET=\"_top\" method=\"post\">
                 <input type=\"submit\" name=\"submitted\" value=\"Logout\" />
            </form>\n";
        }
        else
        {
            logout();
        }
    }
}
Can you see why we're much more likely to respond to people who post code that is indented and easy to read? We're all busy folks, and no one wants to take the time to reformat your code just so it can be read. Personally, I would also avoid opening and closing PHP blocks, IMO, that just clutters the code more.




Similar Threads
Thread Thread Starter Forum Replies Last Post
recordsets- displaying and conventions jerryjet Dreamweaver (all versions) 6 February 23rd, 2008 05:43 AM
Variables in the four naming conventions FJInfante SQL Server 2000 3 May 1st, 2006 01:06 PM
Help me writing code jumpin_hunk Assembly Language 0 November 16th, 2005 11:48 AM
Writing Client Side Script from Code-Behind code sajid_pk Classic ASP Databases 1 January 18th, 2005 12:53 AM
Naming conventions jara VS.NET 2002/2003 3 June 22nd, 2003 04:21 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.