View Full Version : How To : Improve Your PHP Programming

07-17-2005, 06:58 AM
Hope you enjoy it and learn a thing or two.

1 - Your PHP Tags
I know some of you prefer to use the short tags when writing PHP scripts <? ?> but this is not always the best way of doing it.
The standard tags <?php ?> are much better as they will work on every server that you write your PHP code on. You may move to a server some day that doesn't allow the short tags or the ASP-style tags and you will have to sit for an hour and update your PHP scripts.

2 - Debugging Your PHP Code
Some of us may run into a problem when programming a PHP script and don't know what's wrong with it. The error_reporting() function in PHP helps you out by telling every error you have on your page. To show all of the errors on the page that you're editing, put this on the second line :


3 - Debugging Your PHP Code (again)
When you finish editing your 1200-line PHP script, click onto it in your Internet browser, you see an error that says that is on line 561. Don't hit the panic-attack button quite yet, because there is an easy way to find out what line 561 is. Follow these easy steps :
- Open up Microsoft Notepad
- Paste your PHP script into it
- Go to 'Edit' >> 'Go To...' (or Control+G)
- Type in line #561 and hit the enter key
- Your cursor is taken to line #561.
- Look above and below line #561 to see if there is any kind of trouble.
- Fix the error, re-upload the script to your website, and most likely it will work. If there is another error, repeat the above steps.

4 - Using Comments
If you have a 1200-line PHP script, it may be quite hard to figure out what's going on all through-out it. The solution to figure out what you're doing is to add PHP-comments.
PHP-comments are different than the <!-- HTML Comments --> as they are not outputted to the user's page (meaning that they are not even going to see it in the source code).
There are three ways to make comments in PHP :

// The double-backslash is my personal favorite. I add another set after my code so that it looks even, though it is not necessary. //
# The hash-style comment is another way of making a comment.
/* And, this is the final way of making PHP-comments. You can use
at a time by using this style. */

You can decorate it however you like, you are the only one who may use them.

5 - Indenting Your PHP Codes
I don't personally like to indent my PHP codes, but it helps when reading it. When I do have to, I use the tab key to accomplish this. Example :

// Settings //
$var1 = "This";

// Showing Variables //
if($var1 == "This"){
echo"You said This";
echo"You said That";

6 - Improving your PHP-File Includes
I'm sure that most of us on here include a PHP file or two for our layouts. Well, what if your layout file was missing ? Wouldn't that look pretty unprofessional to the people on your website ?
In every PHP-script that I write, I make sure that the file exists before it is even included. Here's an example :

if(!file_exists("layout.inc.php")){exit("Error : LayOut File Missing");}else{include_once("layout.inc.php");}

I'm sure that a small error message will seem better than half a page that is all messed-up looking.

7 - Your MySQL Queries
Sometimes when you're writing a PHP script that includes connections to your MySQL database, you may run into a few problems. Most everyone that had MySQL problems ran a command like this one :

mysql_query("INSERT INTO tableName ('id','name') VALUES('1','Mike')");

..and they figure out that it's not inserting into their database. Here's the solution to this :

mysql_query("INSERT INTO tableName ('id','name') VALUES('1','Mike')") or exit("MySQL Error : " . mysql_error());

8 - Combining Alike If-Then Statements
You may have a register page, and want to make sure that everything has been filled-in. You may use many if-then statements like so :

if(!$_POST[name]){exit("Sorry, but you did not fill-in all of the requested fields.");}
if(!$_POST[email]){exit("Sorry, but you did not fill-in all of the requested fields.");}

You can combine these two lines into one by joining their if-then statements together :

if((!$_POST[name]) || (!$_POST[email])){exit("Sorry, but you did not fill-in all of the requested fields.");}

Simply, || is the same thing as OR and && is the same as AND.

9 - Using echo or print ?
Most of you may say 'echo is the same thing as print', in which I agree with you all. The echo command is much faster than the print command, and is one less character to type. The echo command came later than the print command (I believe), so you make the judement on which to use.

10 - Printing out a Huge Chunk of HTML at a Time
Well, I'm sure that many of us found a way to get around this, but I'd like to share with you a few of the ways you can do it.

1 - Break off your PHP-code, print the HTML, and start your PHP-code up again. (I don't prefer doing this as it looks pretty unprofessional to me).
2 - Adding backslashes to each HTML tag. (It works, but takes forever to do).
3 - Using the echo/print command, but without having to do much work. (I recommend) :

// Showing a huge chunk of HTML at a time //
<font face="Verdana" color="Orange" size="3">Large, Orange Text in Font Size 3</font>
More HTML down here..
<div align="Center">Centered text</div>

Well, I have many other things to tell about sprucing up your PHP-code, but I don't want to bore you.