10 PHP Tips Every Web Developer Should Know

If you are a web-designing enthusiast and questions like how websites are made and used, often crawl inside your brain, it is very likely that you would have had a few encounters with PHP.

For understanding any language, you first need to understand the basic details of that language such as variable declaration, data type, operators, statements controlling the flow of the code and others. Once you get familiar with the language, the syntax used, its benefits etc; then comes the more fundamental part of any programming language, the coding part. It only takes a very small point to differentiate a good PHP coder from an ordinary one. 

Below are 10 useful tips that you must follow while developing a PHP script:-

1. Comments are your new BFF’s

As people grow and gain experience as a coder, they tend to forget one of the most basic tips that they received in their early days of programming, to leave comments in the code. Posting comments on your code is a tip that goes for every programming language, including PHP. It makes updating, debugging, analysis and other post-programming activities really easy and efficient. Further, if you are working as a team, having comments in the code makes it convenient for the other members to understand your idea of the code.

2. The smaller, the better

Writing a 10,000 line code is not what makes you a great developer, but reducing a 10,000 line code to something smaller without changing the output is the trademark of a good PHP developer. Do not repeat code and use programming tools such as functions, objects and array to good effect.

3. Use single quotes instead of double quotes

Since your PHP code will most likely be an integration of the PHP script and the HTML script, it is always better to use the single quotes in echo statements as using the double quotes might be a problem when HTML tags and their corresponding syntax are involved.

4. Use quotes to access array elements

While trying to access a particular element from a string array, it is better to write the element inside quotes as it saves access time and processor indulgence. 


Right way:

$my_name = $all_name['Andy'];

Wrong Way:

$my_name = $all_name[Andy];

While using the second way, PHP first tries to find a constant “Andy” and once it fails to do so, only then it converts “Andy” to a string.

5. Use isset() instead of strlen()

Though this might seem a little odd, but using isset() is a better option than using strlen() when dealing with string array. The reason behind this is the difference in the cost in terms of time of the two entities. Isset() being a language construct is more cost efficient, if not necessarily faster; than using strlen() , which is a function.

6. Use the braces

PHP is probably one programming language that makes extensive use of the flow control loops and the ‘if else’ statements, therefore, it becomes an absolute necessity to maintain clarity and readability in the code. Using braces makes your code less congested and minimizes the possibilities of errors.


Wrong Way:

	if ($age<18)
		echo 'You are not an adult yet';
		$age=$age +1;
		echo  'Next Year you will be an adult';
		echo 'You are an adult';

In the above example, not using the curly braces forces the code to print ‘Next year you will be … years old’ at all times, while actually it should have only been printed when the if condition was true

Right Way:

	if ($age<20){
		echo 'You are not an adult yet';
		$age = $age + 1;
		echo  'Next Year you will be '.$age.' years old';
		echo 'You are not an adult';

7. Object Oriented Programming (OOP) is the way forward

Object oriented programming is far better than the traditional procedural approach and the use of classes and objects make the code faster, easier to understand and aids the debugging process by reducing the overhead time. Further, a lot of times, a section of your project will have an object oriented programming language like JAVA or C++ in operation and therefore, integrating OOP in your PHP code will bring the whole project in a state of strong cohesion and loose coupling.  

8. Use PHP’s inbuilt functions

PHP provides developers with highly useful inbuilt functions that can save both time and cost for the project. It is a good practice to use the PHP in built functions for the operations that otherwise take a lot of time and eat up a lot of lines due to the use of loops.


	$integers = array(10,12,4,87,55);

The values stored inside the array variable ‘integers’ are numeric in nature and the sort() function will arrange the values inside the array variable $integers in ascending numerical order.

9. Say no to $_REQUEST

The use of $_REQUEST is a very amateur and unsafe approach to get values for variable. $_REQUEST gets both the POST and GET variables and opens up the security concern of users manually writing variables in the URL.

$action = $_GET('action');
$name = $_POST('name');

10. Complacency hurts, Practice your art

Being a good PHP developer is an art, a skill,  and you must regularly practice writing codes in a PHP script to ensure that the art does not develop rust on top of it. It is amazing how much you can learn by coding and exploring the different dimensions of PHP. You can never learn if you are not willing to get your hands dirty and therefore, start coding the moment you read something new. Write the PHP code for something that you like and do not be afraid of failures. Only when you fail, you realise the true value of success. 

Online Technology Courses Stone River eLearning