You are using a browser which is not compatible with CSS (for more information, see Tara's tutorial). Because of this, it is possible that our website may not appear correctly in your browser. We apologise for the inconvenience, and recommend you upgrade your browser to one which is compatible with CSS. For more information, please visit our Browser Upgrade page.

4WebHelp

PHP Coding Tips by Daniel

Last updated: 02/05/2012
Biography: Daniel has been interested in web design since 1999, when he first started designing a personal website. Since then, he has learnt much, and is interested in new web standards like XHTML and CSS, and in PHP.

Daniel is responsible for managing the 4WebHelp Forums, along with the forum moderators. He should be contacted for any modifications to your forum account (which you cannot do through the phpBB interface) and for any issues you may have with moderation.
See 9 more tutorials by Daniel

In this article, I will try to present to you some tips which should help make your PHP code easier to read by you, and by others. Due to PHP's acceptance of many different types of spacing and habits, you can pretty much code the way you want. But in order for other people to be able to understand your code, you have to discipline yourself.

Indentation and Spacing

This is one of the most crucial steps in making your code redeable? After all, how easy is it to read this?

<?php if(is_dir($directory)){function();} ?>

Let's be honest, wouldn't you prefer reading this?

<?php

if ( is_dir($directory) )
{
  function ();
}

?>

It is up to you whether you use tabs or spaces to indent your code. Tabs are often preferred because they can be set to appear as long or as short as you want them. Also, you'll have to decide how many tabs or spaces to use for each "level" of indentation. But whatever you choose, stick to it.

Variable Naming

This is again mainly up to you, but keep the same style forever. Here are a few different types of variable naming:

  • $variable_name (probably the most common)
  • $Variable_Name (having both capitals and underscore is a little redundant)
  • $VariableName (capitals replace the role of the underscore)

Variable names I do not recommend are ones like these:

  • $Variable_name (one capital one small letter)
  • $Variablename (the break between the words cannot be seen)
  • $variablename (same as above)

Also, when naming variables, think of something descriptive, yet concise. You don't want a 30 character name, but then you don't want to be left wondering what a certain variable is.

Function Naming

You can probably use the exact same rules for functions as for variable naming. Good function names are get_text() or GetText() (but don't combine both styles).

Also, when setting up a function, give the arguments descriptive names, as for variable naming. This is terrible:

function get_text ($1, $2, $3)
{
  bla bla bla
}

Don't you agree that finding out what each argument is will be difficult?

Magic Numbers

Simply don't use them. Here's an example of a magic number: if ($status == 3). So what's the alternative? Constants. In this case, you'd define the constant like so: define ('DELETED', '3'); and then replace the magic number with the constant's name (be careful - no $ sign): if ($status == DELETED). Another thing, while I'm talking about constants: always name them in capitals; it'll help them stand out.

Comments

Comments can often be left to your own appreciation. If you think a comment is needed, add it. But don't get too obsessed with comments: no need to add them where it's obvious. When you declare a function, try and summarise it's use, and describe each argument.

When you're coding, try to look at your code and think "If I was someone else, would I be able to understand this?" If the answer is no then you need a comment.

Single or double quotes?

My rule of thumb is the following: "If you don't need double quotes, use single quotes." Many people use double and single quotes carelessly, but there is a difference between the two: any variables found in between single quotes will be ignored, whereas variables found between double quotes will be processed. So if you have the following code: print ('Hello World!);, double quotes are not needed, as there is no variable. In fact using double quotes would slow down parsing (OK, so in this case it would be minimal, but still, every little counts!). If you can't be bothered to write print ('Hello '.$world); then you'll have to use print ("Hello $world");. print ('Hello $world'); would print out "Hello $world" and not "Hello" + the value of $world.

Brackets

I know it's easier to leave them out, I know it saves time, but I also know that it's a pain to read any code which doesn't use them. All these are bad:

if (whatever) do_this();
while (whatever) do_this();
for (whatever) do_this();

You really have to force yourself to use the brackets if you don't already do so. Again, give them some space.

if (whatever) {
  do_stuff();
}
while (whatever) {
  do_stuff();
}
for (whatever) {
  do_stuff();
}

Some people like to have all brackets on their own line, but that's up to you.

if (whatever)
{
  do_stuff();
}
while (whatever)
{
  do_stuff();
}
for (whatever)
{
  do_stuff();
}

© 4WebHelp and Daniel

Latest comments on this tutorial
Jsh14
how do you print a space in php
DdL
Hi, I have a problem with spacing, how can I make something like this:

1        4
2        5
3        6

7        9
8        10

can someone tell me? thanks before
Hed
Hi !
what about HTML code ?
Better to do this:
<?php
print ("<tr><td>$value1</td><td>$value2</td></tr>");
?>

OR

<tr><td>
<?php echo $value1;?>
</td>
<?php echo $value2;?>
</tr>

http://www.interdoc.ch
Cool
I need help with the lang stuff? i need the codes and where to put the files...
fauzan
good!!
AJIT KUNTE
its good  and it will be better if if it give

tips about embbeding HTML IN PHP CODE
ByteCode
RE: Magic Numbers

The point of 'magic numbers' or 'constants' as they are more commonly known is to improve maintainability:

1) they make code more readable, comparing a variable or the return value of a function to a constant is preferable to a 'hard-coded' number, which may appear quite arbitrary.


2) if comparing to a number or value repeatedly  throughout an application,  it's easier to change the value of a constant defined in a header or config file, than to go through a lot of code doing a search an replace.

Kmar
About constants. I have to admit to not using them in small php-projects, but in a larger Java project I had a few numbers used all over in the code. They were 90,180,270 and 360. So in stead of typing the digits each time, I used constants like HALF_CIRCE, FULL_CIRCLE etc. Less room for error and much more readble, you see.
cityrabbit
Pleas can your's teacher me, how I can to the PHP homepage
Kdawg
Its really usefull to do that, I know because when I started using php I didnt do this. Now I have to change and old site I did and its going to be a major pain.
Daniel, 4WebHelp Team Member
Well say you have a database with a table in it. This table contains a list of subscribers to your mailing list. There is a status column for each subscriber. If the subscriber hasn't been approved, the status would be 0. When you have an if statement or something, instead of putting:
if ($status==0)
you should probably put
if ($status==UNAPPROVED)
If some day you decide to completely change the table's format, you can change the value of UNAPPROVED, and that'll be it!
EqualHate
magic numbers?  I am not sure I understand the point there.  Is the point to never use an actual number when you are doing a comparison? and if so, wouldn't that (theoretically) slow down processing time?
You give an example of of a Magic Number but do not explain what it actually is.  I am not trying to sharpshoot this tute, I just don't know what it is.
Daniel, 4WebHelp Team Member
Yes, that is a very interesting set of coding standards. However for the average reader it may be a bit complicated Very Happy.
some guy
Users, you might also want to look at the coding standards (http://pear.php.net/manual/en/standards.php).

Add a new comment

This page is © Copyright 2002-2017, 4WebHelp. It may not be reproduced without 4WebHelp's prior permission.