PHP Tips to Improve You’re Programming

After the introduction of PHP in 1995, PHP has become the hot cake for every web developers. In this tutorial, you'll learn about advanced PHP tricks and techniques to improve your programming. These tips are:

Using a SQL Injection Cheatsheet


You might be a wonder why I am talking about database. The reason is that almost every PHP application requires a database to store data. So, what is SQL Injection, SQL injection is a method of hacking in which hackers put some code to your database for monitoring the data flow? Here is the complete Cheat Sheet.

Knowing the difference b/w comparison operators


One thing about programmers is that every programmer doesn't know the difference b/w comparison operators. Following are the complete list of comparison operators:

 

 For example, "==" and "===" looks similar in one view. But there is a huge difference, "===" use for checking the identity and "==" use for checking the similarity. Further, all the details about comparison operators are available on the PHP official website.

Shortcut the "else" statement


Making the code simple, clean and easy to read is the good programming practice. For example, see this example.


If, we want to make the variable '$x' by default to '10'. So, why are we using an extra 'else' statement? Correct code is in the following snapshot.

 

Dropping the brackets


In the third tip, we see how we are using brackets. This worked fine, but not a good programming practice. The best way for brackets is something like that:

Use str_replace () instead of ereg_replace () and preg_relpace () functions


Sometimes in php, we start using ereg_replace () and preg_replace () functions for string manipulation. Purpose of telling this is that, these functions do not work for string manipulation, but efficiency of code decreases. Use str_replace () function instead of above. Those functions are best suited for regular expressions work.

Use of ternary operator

 

Ternary the operator is a type of comparison operator and syntax for this is something like selected are in the following snapshot.

 If we are using, small conditions i-e two, then using of ternary operator is the best way instead of if and else, because ternary operator avoid extra spacing in the code.

 

Memcached


 In the beginning, Memcached will look difficult, but this is the best caching application for speeding up the website. It generates the copy of your pages into simple HTML and store it, when every time a visitor will come to this page, then instead of again processing the page, the server will show this HTML page.

Using a framework

 

If you have some knowledge about PHP, then PHP frameworks will be the best way for creating web applications. Most popular frameworks are Code Igniter, Zend, and CakePhp.

 

Using the error controlling operator correctly

 

Error controlling operators start with @ (at sign) and this operator only works in expressions. It generates the error when any expression is not working fine. Best usage of this operator is given in the following screenshot.

Using isset instead of strlen

 When you want to calculate the number of characters in a string, then 'strlen' comes to your mind. But strlen is not an efficient way, use of 'isset' will generate sixty-five per cent quicker results. Following is the Detail explanation about this strategy.

No comments: