Programmers Must Know These PHP Coding Tricks

Image Source: Unsplash. Ilustrasi.

Currently, PHP is becoming one of the most popular programming languages used among web developers. Because of its fame, web developers can not help but continue to develop their knowledge and skills in PHP coding techniques.

Here are some tricks in PHP coding that you may not know about, ie the use of root path and require_once in the script, quoted from BinaryTides. These tricks will help your code more responsive and work more optimally.

PHP Coding Tricks: The Use of Root Path and Require_once


coding PHP
Image Source: Unsplash. Illustration.

Do not use relative path

In PHP coding, you may often find this kind of line:


This approach has many disadvantages, one of them is checking too many directories. First, it does a search directory that has been specified in the PHP path, then the current directory.

Also, while the script is running from cron, it probably does not have a working parent directory. For that, you should use an absolute path, such as:

define('ROOT' , '/var/www/project/');
require_once(ROOT . '../../lib/some_class.php');
//rest of the code

Absolute path like that will always stay constant. However, we can improve it. The /var/www/project directory can be changed. However, we do not have to change it too. Just use __FILE__ constants, like this:

//suppose your script is /var/www/project/index.php
//Then __FILE__ will always have that full path.
define('ROOT' , pathinfo(__FILE__, PATHINFO_DIRNAME));
require_once(ROOT . '../../lib/some_class.php');
//rest of the code

So if you shift your project to a different directory, such as moving it to an online server, the same code will still run without any changes.

Check Out:What Is A Full Stack Developer? Learn More Here 

Do not use require, include, require_once, or include_once

Your script may have various files on top, such as library classes, files for utility functions, and helper functions, such as:


This is, can be called, out of date. This code should be more flexible. Write down the helper functions to make element input easier. See the following example:

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

See the difference. This code no longer requires explanation. You can also improve it again.

Check Out: 5 JavaScript Tools To Make Website Faster

So those are some PHP coding tricks that you can apply, especially for those of you who are still in the stage of learning more about PHP. Click PHP Trick to find out more about PHP coding tricks for your work efficiency.

PHP Programmer Vacancy


coding PHP

If you have expertise in programming techniques, including PHP development, Logique Digital Indonesia is currently opening web developer vacancies for you. At Logique, we will help you dig in your potency so you can improve yourself with an international company like us.

Not only in Jakarta, we are also opening web developer vacancies for you who are staying in Yogyakarta. If you have special needs and are not able to work in our office, we also accept employees to work with a remote system.

Sign yourself now and send your CV to this link. For more information about other vacancies at Logique, please visit Logique Digital Indonesia career page.

Related Posts