Tips For Avoiding Typical PHP Mistakes

One is not born as a coding aficionado and all of us, at some point in time, have committed some mistakes that border on downright inanity. Though, as a newbie, it’s nothing to lose your sleep over, but a little help helps. Here are the 5 common PHP mistakes you might identify with, and this is how you dodge them:

Adding Unnecessary load on Computer’s Memory

It is a well known fact that PHP is a heavy duty language that takes up a lot of memory’s resources whenever it is being executed. Thus, when you decide to use a pile of PHP code for executing your program, you are not leaving any stone unturned to overburden the RAM of your system. A PHP code like the below one is strongly advised against:

view sourceprint?

$names=array(‘name1.txt’,’name2.txt’,’name3.txt’);

foreach($namesa $name){

$Parser= newParser();

$name_contents=$parser->parse_name($name);

}

What this code does is that it calls an altogether new class every time the loop is being run. This further leads to an unnecessary increase in the number of objects that eat up a lot of otherwise useful memory. Steer clear of such coarse codes that cease to solve any purpose and only make your computer run slow and produce undesirable results.

Evading-Typical-PHP-Mistakes

Confusion with Single quotes and Double quotes

To begin with, there is nothing wrong with using double quotes whenever you are writing a program to concatenate two strings. There isn’t any discontent with the process, and instead, it gives you code an impressively cleaner look as you aren’t forced to use the escape characters. The clutter is efficiently cut using this approach. Thus, the question arises, why should you use single quotes instead? Well, you would find many coders concur to the fact that using single code has its own set of advantages that go onto supersede those offered by double quotes. The maze of complexity is considerably reduced as the whole procedure requires a reduced processing.

Take the following string for example:

# $hey = ‘friend’;

# $foo = ‘hello $hey’;

# $bar = “hello $hey”;

The $foo function gives us an output, “hello $hey” while $bar comes up with “hello friend”. Compared to using the double codes instead in the above code, one step is eliminated. Now you might argue it’s not much of a difference, however, when it comes to codes, even the diminutive of changes brings about a huge shift in the performance.

Not Localizing the Entire Text

The standard practice is coding dictates that every time you are in the process of building a software project, you save every piece of text in the form of code. Save for a flawless execution, you would time and again need to resort back to the codes whenever an issue occurs or whenever the client requests for certain changes in the final specifications. Now, whenever the project authority gives its nod on certain features, the next step in the cycle is localization of the text. And this is where the flaws take place. Whenever localization takes place, it should be done of the entire code, not selectively. This wavering in focus leads to issues that range from familiar to unfathomed.

Ignoring the Utility of Database Caching

Once your application comes into existence, you might be tempted to use database caching. However, you won’t be doing yourself a favor if you’re not caching the database with some tool like Memcached.

A Peculiar Fixation with isset()

The enthusiast coders who have just stepped into the big, bad world of coding would vouch for isset(). They are absolutely justified in doing so, as this is a highly resourceful function. But every tool has its set of limitations and so does the isset() function. And when you go on to use it for boolean checks you are doing nothing but inviting those limitations, and few troubles along with them. One of the reasons that can be explained for overusing this function is the tendency to use the $_GET variables, which necessitate using isset($_GET[‘id’]). This is how the novices develop this fixation with isset() and refuse to let it go, for it offers them en easy solution, masking the drawbacks associated with it.

view sourceprint?

$contents=file_get_contents(my_pet.txt’);

if(isset($contents)){

do_watever_with($contents);

}

If you are someone who is seeking PHP development services, it would serve you well if you instead hire an experienced PHP developer who is well-versed with all the nitty-gritty of PHP.

Missing Semicolon after a Break or a Continue

This is when you back to the leisurely loops of school, but not for a good reason. A misplaced semicolon does nothing but produce issues that are hard to get past by. Putting semicolon to close a “break” in your program will produce null as the output and there is not much you can do about that. What’s even worse? Semicolon is something quietly sits somewhere in a corner and it’s extremely arduous to point it out. If you don’t know what’s causing the error, how are you possibly going to troubleshoot it? As an alternative, braces would serve you well.

Dealing with the website issues can get a little galling, but with a painstaking attention to detail, you are better equipped to remove the cobwebs.

About Author: Maria is a qualified Offshore PHP development professional who is great at delivering focus in his writings. She works for the software giant, Xicom Technologies. Hire PHP Programmer from Xicom to get quality service at your fingertips.

2 Responses

  1. December 30, 2013

    […] One is not born as a coding aficionado and all of us, at some point in time, have committed some mistakes that border on downright inanity. Though, as a newbie, it’s nothing to lose your sleep over, but a little help helps.  […]

    [WORDPRESS HASHCASH] The comment’s server IP (89.30.105.121) doesn’t match the comment’s URL host IP (89.30.105.77) and so is spam.

  2. December 30, 2013

    […] Tips For Avoiding Typical PHP Mistakes | TheBigBuzzBlog […]

    [WORDPRESS HASHCASH] The comment’s server IP (93.184.35.43) doesn’t match the comment’s URL host IP (93.184.35.40) and so is spam.