Wednesday 22 February 2017

PHP Tips And Tricks That Can Make Developers Life Easier

Whenever it comes to web development, many developers want tips to improve the website. Web development in PHP is pretty easy for newbies. All you need is PHP tips and tricks for beginners and easiest way to optimize PHP website.
PHP is one of the most loved and widely used languages that developers loves to build their website from. The best part of PHP development is that developers can find Guide for PHP developers and best PHP tips and tricks on the official site of PHP. Another advantage of PHP is that as a PHP developer you can find good frameworks and CMSs to work on.
This blog post is all about the best PHP tips and tricks for Web development in PHP so that you can develop your website in PHP without any hassle and you can enjoy your professional life.

Develop with error reporting enabled

Error reporting feature, an important feature in PHP website, is one of the best Useful PHP trick for Developers. The best thing to cope with errors is to set the system to display errors. To set the settings you have to alter the php.ini file when you develop your site. Include these two lines in the beginning.
error_reporting ( E_ALL ) ;

ini_set ( 'display_errors' , 1 ) ;

The following code helps you to see both types of errors whether the fatal errors that produce the dreaded white screen as warnings and notices that may be bugs to fix.

Prevents SQL injection

Security holes are the biggest cause of the bad reputation of PHP but PHP tips for website development are here.
There are many ways to avoid SQL injection but the simplest one escapes any variable that we will use in the database. The code should look like this:
$query_result = mysql_query ( "SELECT * FROM WHERE Ex_table ex_field = \" " . mysql_real_escape_string( $ ex_field ) . " \ " " ) ;

Use the _once() function with caution
PHP developers prefer using include() or function require() to call other files, libraries or classes, but using the  include_eleven() and require_eleven(), are the  PHP tricks for web developer.  Both the functions have the same functionality but the advantage is that it prevent the files, classes or loaded libraries to be loaded again which cause duplication and undesired states in the code.

Learn to handle ternary operators

The best PHP tips and trick for good performance is to use ternary operators as an alternative to simple IF constructions. For instance:

$age = ( !empty ( $ _ GET [ 'age ] ) ? $ _ GET [ 'age' ] : 58 ) ;

This would help your code to get lighter and can be nested without problems.
Retire the MySQL driver
This is 2017 and the technology that we are using is advanced now this is the time for PHP7. A high time to retire your MySQL driver and start using PDO. A PHP extension that helps you to connect with different other managers databases.
Obviously, PHP works other than MySQL too. As a PHP web developer, this tip is one of the best PHP tips and tricks for me for website development with PHP. The code to connect with databases is:
try {
$conn = new PDO ( "mysql: host = localhost; dbname = database ' , $ user , $ pass ) ;
$conn -> setAttribute ( PDO :: ATTR_ERRMODE , PDO :: ERRMODE_EXCEPTION ) ;
} Catch ( PDOException $e ) {
Echo "ERROR:" . $e -> getMessage ( ) ;

Use single quotes rather than double

Are you a speedy programmer who can type code fastly?
Well here’s a trick for you! Just use single quote (“) other than double (” “). Trust me! it saves you a lot of time and the performance of your server. Best PHP tips and tricks code! ha?

Clean URLs quickly with .htaccess

The file .htaccess – the best yet simple way to make the URLs more simpler for human eye as well as for SEO.
A hidden file that serves a lot with Apache directives. The services that this file provides are performing redirection and clean URLs do not cease to be redirected to after all.
One of the best tip and trick for PHP based application improvements.

RewriteEngine On
RewriteRule ^ ( [ a - zA - Z0 - 9 ] + ) $ index . Php? Page = $ 1

This code is the life saver for many PHP developers that can prevent horrible URLs and make it sleek and simple, phew!

Know all the Problems of isset()

One of the PHP tricks for web developer is to understand what isset() function do. Make sure that you know at what time isset() function returns false and on which time it answers true. This function returns True if the variable is not NULL. Yes you read it not NULL.
Just like so, if NULL can be a valid value of the variable,We surely have a problem sire!
$foo = null ;
if ( isset( $ foo ) ) // returns false
And the solution is: Use  get_defined_vars( );
$foo = NULL ;
$vars = get_defined_vars( );
if ( array_key_exists ( 'foo' , $ vars ) ) // returns True

Use a switch instead of stringing If Statements

The useful PHP trick for Developers- use Switch instead of crazy Ifs. Advantage? The code will execute faster thus performance is increased. The usage of Switch statements allows you to go beyond the use of if-else if-else chains.
switch ($color ) {
case 'white' :
echo "The color is White" ;
break ;
case 'blue' :
echo "The color is Blue" ;
break ;
case 'green' :
echo "The color is Green" ;
break ;
case 'black' :
echo "Color is black" ;
break ;
Take up cURL
The method to retrieve a file from another server is to use the powerful way cURL, instead of using file_get_contents() function which is easy to use but come on, it’s like having death on your computer. The following code is the example of the use cURL:
$c = curl_init ( ) ;
curl_setopt ( $c , CURLOPT_URL , $URL ) ;
curl_setopt ( $c , CURLOPT_TIMEOUT , 15 ) ;
curl_setopt ( $c , CURLOPT_RETURNTRANSFER , true ) ;
$content = curl_exec ( $c ) ;
$status = curl_getinfo ( $c , CURLINFO_HTTP_CODE ) ;
curl_close ( $c ) ;

Password Encryption

Often developers ask Tips to improve the website, well here is one, PHP is always on your side and it encrypts your passwords for you. I am talking about the PHP 5.5+ versions, Just store the passwords in your database as.

$enc_pass = password_hash ( $submitted_pass , PASSWORD_DEFAULT ) ;
To check if password is correct or not:

if ( password_verify ( $submitted_pass , $stored_pass ) )
// User successfully authenticated

Wrapping things Up!

One of the best programming language to develop websites is PHP, which is an open-source. Although it is easy to use but it’s normal when it takes the time to learn but if you know all the PHP tips and tricks for good performance and for Website development in PHP then you are a champ!
Whatever it takes you to use PHP based frameworks and CMSs consider, use it over Core PHP, as it can give a better performance, a lightweight website and yes you can develop your website in PHP more faster.
These were the tips and tricks for Guide for PHP developers. Do you like any of them? or know any tip better than these? Let us know in the comment section below. Also, PHP beginners are much appreciated to ask.