The best way to Write Better PHP Computer code – These 7 Techniques

2

You don’t need to remember functions, lessons and specific solutions to be considered a good programmer. They are online, and accessible at any time. And like a PHP developer you are less likely to do much important perform without being connected to the internet. And then what makes a good programmer? To me, a good PHP programmer implies someone who is efficient: who can solve problems and produce quality web-based software swiftly and in a way that allows less difficult maintenance and extending of the plan.

So being a good PHP programmer means mostly publishing good code with significantly less effort and time spent. And here is just what that means – various main things:

– Computer that is short. I have a tendency mean putting everything in one line like some Perl coders do, but producing less waste, reusing the computer, and keeping it lift-up
– Code that is an easy task to maintain and extend: this again means lift-up, but also a well-commented PHP code.
– Code that overloads the server: you shouldn’t get obsessed at this time but it’s important to keep the storage space overload low

How to compose such code? There isn’t a certain recipe – every designer has their own style regardless of the point that many use frameworks or perhaps follow specific guidelines. Thus I’m not going to supply the recipe. Instead of that listed here are seven of the best practices I subscribe to write better PHP (and not only PHP) code. If you utilize them too, you can considerably improve your efficiency as a designer.

1 . Use alternative PHP syntax in templates:

I am hoping you use templates, to begin with. Should you be messing the HTML outcome directly into your scripts, you have to work on this first. Even if you follow the concept to separate your own design from the program reasoning, you are either using some theme engine (usually a dead finish reducing productivity) or placing bit of PHP code within the templates (loops, if/else claims, etc). You can add some more cleanness to your views by using the option PHP syntax instead of the regular one with ” ” and ” “. Using foreach: or end for each; if: endif; with regard to endfor, etc . keeps the actual PHP code on much fewer lines in the view, assists knowing when a loop is actually opened and closed and generally looks much better.

You can learn more about the alternative PHP syntax on the official PHP site – just seek out “Alternative syntax for command structures”.

2 . Everything capsulated:

You know about the DRY, would you? Don’t Repeat Yourself. No longer copy-paste code. Always use characteristics and classes that will encapsulate often executed tasks. I realize this is ABC of coding, but are you really really doing the work? If one SQL research or code block is usually repeating itself 3 or higher times in the application, subsequently it should be a method or purpose. If it is repeating itself using slight variations, it should be a technique or function as well (one that takes arguments). This is what encapsulation gives you:

– much less code, less writing
–  ability to make changes over the entire app with a solitary code change
– thoroughly clean and understandable code

These types of three things really create a better code. Let’s obtain a very simple example: which do you believe is better – formatting the actual date in the SQL predicament or in the PHP program code? Both are fine if you encapsulate the format. If you use the conventional PHP date() function almost everywhere or the SQL date format function, passing the file format that the client requires, with each call, what will happen if the buyer wants a change? You’ll have to alter it everywhere. To avoid that, your own function to style the date or just that formatting string (the “Y/m/d H: i A” issue for example) is frequent, so you can change it at any time.

3. Use a DB object:

There are several ways to handle this, which include ODBC, PDO, and others. Whatever you decide to do, don’t put mysql_query() or mssql_query() (or whatever) directly in your code. Not necessarily that you are going to change the DB engine ten times from the project – in fact throughout eight years in web development I had to change the DB engine of an existing task just a couple of times. It’s once again about keeping the code brief, readable and better. For instance, I have a DB item with methods that come back a single value, single variety, or multiple arrays from the DB query. This way rather than writing:

$sql=”SELECT * THROUGH some_table”;
$result=mysql_query($sql);

and then utilizing $result in some construction such as while($row=mysql_fetch_array($result)), I just write:

$sql=”SELECT * FROM some_table”;
$some_things=$DB-> aq($sql);

And I have the cause $some_things. (Note: don’t use this kind of when retrieving thousands of documents at once, it will exhaust the actual server memory).

4. Utilize CRUD Functions:

Just in case you how to start, CRUD comes from Create, Change, Delete. Create functions as well as object methods that will accomplish this work for you or use the renowned ActiveRecord. Do this instead of producing long SQL queries having tens of fields listed. This can be going to save you a lot of time. It is going to work automatically when you bring or remove a field inside the HTML form. Isn’t this great?

5. Debugging is the best best friend:

OK, this point just isn’t directly related to writing a greater code – it’s a lot more related to being a better coder, however. If something fails, you are not likely to fix it by simply thinking hard or searching hundreds of lines regarding code. It’s not going to happen simply by swearing either. It’s going to take place by debugging.

Debugging will be the action of going back to pursuing the logic of your program and also finding the place where functions are wrong or don’t work. No matter whether you use a debugger or perhaps print_r() and echo() in several places of your code. The biggest thing is to trace backward. Begin with the current place – will there be something wrong with it? If yes, return back few lines before the output/result happens. Is it still drastically wrong?

If yes, keep going a few wrinkles back. If not, then you definitely know where exactly is the wrong piece of code instructions after this current line in addition to before the line when your hottest “is it wrong? micron test returned true. I could sound bold, but Factors . say that this is the most important proficiency in programming (and definitely not only) ever: to be able to revisit and trace the route with the problem. If you learn to accomplish this, you will be able to solve any solvable problem.

6. Mind what they are called:

A code that functions meaningful variable names can be so much better – at any time an individual read it you know what is going on – is there a product at the moment modified, is it an array of consumers, is it the ID in the logged in, or can it be anything else? I am not referring to Hungarian notation – merely use variable names which usually correspond to the subject they stand for and show whether it’s in single or plural form. Avoid the use of variable names like $rows, $row, $varArr, etc.

As opposed to that use $products when you are working together with products, use $user while you are working with a single user, and make use of $is_logged or $isLogged when you want a boolean variable demonstrating whether the user is logged in the system. Use titles that matter and be reliable in that. You’ll thank yourself later for writing this code. Other developers will appreciate it too.

7. Reduce DB queries:

It’s common sense, although how many developers really do the item? How many of them spend a long time improving some unimportant matter like moving a sizeof() out of a loop and also sending a DB doubt in the very same loop? DB queries are the most web server power-consuming task in a great many web applications. Here are various ways to reduce the number of requests in your code:

– Work with joins and left ties together.
– Use inner prefers when joins and eventually left joins won’t do the job.
– Use views and momentary tables.
– Sometimes you will have to check something for information and the previous three remedies won’t work. Instead of operating a query each time, isn’t that possible to run one question for collection A, one particular for collection B, and after that use a for each loop inside PHP to check the condition? Usually, it is. Be creative.

Lowering the number of queries is vital regarding web applications that are going to be taken by many users in addition. Sometimes you may need to sacrifice computer shortness for that. This will definitely not make your code worse instructions you should give the things all their correct priority.

Read also: The Best Way To Shop For Satellite TV Receivers In Addition To Dishes