Hello Everyone...!!!
To become a PHP Developers is easy but to become a better PHP developer is difficult. As a
web development company we have done a lot of projects and on the basis of that we have found some tips that can improve your code
readability and maintainability and make you more organized
PHP developer.
Tips to become better PHP Programmer
1. Separate Configuration Files :
Instead of adding configuration settings to your
script file, create a new file and include that file at the
top of your script. because there is no reason to add configuration settings to your
script file.Including a file makes it easy to each and
every page because of a little configuration change
2. Use Comments as your Friend :
Sometimes we find it very hard to understand our own code that has been written a while ago. So use comments to make your code easy to understand for every programmer.
3. Use Indentation and Spacing :
A well indented code
with the right spacing immediately says all about you as a programmer. every programmer should be able to immediately figure out that whether a loop
has ended or not by just quickly looking through your code.
4. Naming Conventions (Give your Variables Meaningful Names) :
Always give meaningful names to your variable. that means that variable name suggest the details about what type of data
they store.
5. Initialize your Variables :
I know PHP automatically creates a variable at the moment you try to assign a value to it. But it is always good practice to initialize your variables before using
them. This will make your code much cleaner and make sure your variables
are always the type you want them to be.
6. Using Quotes when accessing Array Keys
While accessing arrays, you might find that these both lines work:
|
$sName = $aNames['marc'];
$sName = $aNames[marc];
|
Have a look at second line, here PHP is actually first trying to find a constant called
“marc” and then converting “marc” to a string once no constant is
found. So you should avoid using no quotes. "if you
ever create a constant with the same name, your script will stop
working".
7. Stop Repeating Code :
If you have a few lines that do the exact same
thing, instead of repeating them turn them into one function or an
object. This will help you when your application gets bigger and bigger.
Example :
1
2
3
4
5
6
7
8
|
$a = 1;
while ($a < 10) {
$b = $a * 10;
$c = $b * 2;
$d = $c * $b;
echo $d;
$a++;
}
|
You could write this like this:
1
2
3
4
5
6
7
8
9
10
11
12
|
function multiply($iVar, $iVar2) {
return ($iVar*$iVar2);
}
$a = 1;
while ($a < 10) {
$b = multiply($a, 10);
$c = multiply($b, 2);
$d = multiply($c, $b);
echo $d;
$a++;
}
|
8. Use Method Chaining :
If you’re doing multiple object method
calls that set variables instead your object, use method
chaining to make your code simpler. To do this, just end each method
with
return $this; as shown below:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
class User {
function setThis($iVar) {
....
return $this;
}
function setThis2($iVar2) {
....
return $this;
}
}
|
You will then be able to do method chaining:
1
2
3
|
$oUser = new User();
$oUser->setThis(...)
->setThis2();
|
We have found these things to become a better PHP developer.... "If you have more please add them also.... in comments".
You May also like :