4 min readTop 5 PHP Tricks and Tips

Share with others
PHP Tricks and Tips

In a previous blog, we talked about 5 Effective HTML Tricks and Tips. In 2023, we’ll look at the Top 5 PHP Tricks and Tips.

PHP is the most handy language for developing websites and blogs because it has many functions that can be leveraged to derive even more customized functions.

For beginners and even developers, we’ve listed the most helpful PHP tricks and tips. These PHP tricks increase the efficiency and productivity of your coding.

5 Useful PHP Tricks and Tips List 

1. Dont use require , include , require_once or include_once

We agree with all of the php tricks recommendations that writers should avoid using *_once language constructs in their code.

According to the theory, these statements should assist conserve server resources, which is true for simpler programmes, however these constructions frequently result in worse efficiency for high-load and mission-critical servers.

The majority of advanced PHP tutorials advise using relative paths rather than the conventional require()/include() procedures.

Your script may include other files like class libraries, files for utility and assistance functions, etc. as seen in the example below:

require_once('lib/Database.php');
require_once('lib/Number.php');

require_once('helpers/utitlity_functions.php');

The code must be more flexible. To make things easier to include, write up auxiliary functions.

Consider the following example:

function load_class($class_name)
{
    //path to the class file
    $path = ROOT . '/lib/' . $class_name . '.php');
    require_once( $path ); 
}

load_class('Database');
load_class('Number');

Do you notice a difference? You have to. There is no need for more elaboration.
If you want to make this even better, do this:

function load_class($class_name)
{
    //path to the class file
    $path = ROOT . '/lib/' . $class_name . '.php');
    
    if(file_exists($path))
    {
        require_once( $path ); 
    }
}

It is simple to change the directory containing class files without causing any code breaks.
Use comparable functions to load files containing HTML text, utility functions, etc.

2. Anonymous Classes

If you require a one-time class and its object doesn’t make sense, advanced php theories say that an anonymous class can be formed from a new class.

Such PHP programming techniques are covered in advanced php tutorials and aid in resource management.

3. Use json_encode when echoing javascript

There are times when javascript code is dynamically created from php.

$images = array(
 'iftikhar.png' , 'keval.png' , 'rafi.png'
);

$js_code = '';

foreach($images as $image)
{
$js_code .= "'$image' ,";
}

$js_code = 'var images = [' . $js_code . ']; ';

echo $js_code;

//Output is var images = ['iftikhar.png' ,'keval.png' ,'rafi.png' ,];

Be smart. use json_encode :

$images = array(
 'iftikhar.png' , 'keval.png' , 'rafi.png'
);

$js_code = 'var images = ' . json_encode($images);

echo $js_code;

//Output is : var images = ["iftikhar.png","keval.png","rafi.png"]

That’s quite cool, don’t you think?

4. Enable Error Reporting

One of the beginner’s php tips and tricks that experts know about but undervalue. All lessons on advanced PHP programming should contain these two lines:

error_reporting ( E_ALL ) ;

ini_set ( ‘display_errors’ , 1 );

By simply include them at the beginning of your code, you can save time and work.

5. Use .htaccess for SEO

Clean URLs are great for SEO, therefore don’t be afraid to use the .htaccess file to make them better:

RewriteEngine On

RewriteRule ^([a-zA-Z0-9]+)$ index.php?page=$1

The method was inspired by advanced php books. This would enhance the effectiveness of redirection.

Bonus PHP Tricks and Tips

  • Use echo rather than print.
  • Unless you really need preg replace, use str replace instead.
  • Avoid using short tags.
  • For basic strings, single quotes should be used instead of double quotations.
  • Never forget to do an exit after a header redirect.
  • Never include a function call in the control line of a for loop.
  • isset is faster than strlen.
  • Format your code consistently and properly.
  • Do not remove the brackets from if-else or loop statements.
  • Use a text editor that supports code highlighting. Code highlighting makes mistakes easier to spot. check this Top 15 Free Online HTML Editors

I hope you’ll find our PHP Tricks and Tips informative. for more follow us on Twitter.

Share with others
Iftikhar Qureshi
Iftikhar Qureshi

IFTIKHAR is a Website & SEO Developer, who also blogging addict, WordPress, HTML, PHP fanatic. Check out his social profile to contact him and view his past work.

Articles: 40

Leave a Reply

Your email address will not be published.