Advanced PHP Tips & Tricks

  • home Advanced PHP Tips And Tricks That Can Make Developers Life Easier
WebdesignR April 20, 2021

Advanced PHP Tips And Tricks That Can Make Developers Life Easier

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.


1
2
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:


1
$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:


1
$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 database and start using PDO. A PHP extension that helps you to connect with different other managers databases.

A very important feature of MySQL driver is MySQL Connector/Python that supports almost all features provided by MySQL version 5.7. Designed specifically to MySQL, MySQL Connector/Python lets you translate the parameter’s value between Python and MySQL data types. 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:


1
2
3
4
5
6
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.


1
2
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!


1
2
3
4
5
6
$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.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
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:


1
2
3
4
5
6
7
$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.


1
$enc_pass = password_hash ( $submitted_pass , PASSWORD_DEFAULT ) ;

To check if password is correct or not:


1
2
3
4
if ( password_verify ( $submitted_pass , $stored_pass ) )
// User successfully authenticated
}


Source: https://www.arpatech.com/blog/php-tips-and-tricks-for-developers/