Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > PHP/MySQL > PHP FAQs
Password Reminder
Register
Register | FAQ | Members List | Calendar | 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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Closed Thread
 
Thread Tools Display Modes
  #1 (permalink)  
Old April 5th, 2004, 09: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 5 Times in 5 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.
Closed Thread


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

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 02: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 05:21 AM



All times are GMT -4. The time now is 12:29 AM.


Powered by vBulletin®
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.