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.
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.
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.
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.
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?
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 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.
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
.
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
1 4
2 5
3 6
7 9
8 10
can someone tell me? thanks before
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
tips about embbeding HTML IN PHP CODE
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.
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.
Add a new comment