Advance PHP Tricks and Tips Part One

Advanced PHP Tips And Tricks That Can Make Developer’s Life Easier.
Advanced PHP tips & tricks
514 Views
Advance PHP Tricks and Tips
Advanced PHP Tricks and Tips

Advanced PHP tricks can save so much of your time while developing an application

1. Alternative of “require, require_once, include or include_once”

We use various external files while developing an application in PHP & we use “require, require_once, include or inclucde_once”, but PHP has an alternative method to access external files without using mentioned methods. That is something like this:

Secure your PHP application: read more.

Generally used methods:

require('inclides/Configure.php');
include_once('inclides/Database.php');
require_once('class/general_functions.php');

Here is the alternate method:

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

Execution is something like this:

include_class('Configure');
include_class('Database');

2. Check for the alphabetic character(s) in a string in PHP Development

$var = "dfhskdfjhskdjsdwerywerhwekrh";
if(ctype_alpha($var)){
    echo "Alphabetic";
}else{
    echo "None Alphabetic";
} 

3. PHP array_chunk function

PHP array_chunk function splits an array into chunks of new arrays. Let’s say you have a large number of arrays and you want to break it into 5 in every chunk, just follow the below tricks:

$array = array('321321321s5d1s3d5a1sd',
                'asda1s32d1a32s1d3a21d3a',
                '4as3da4s2d13a21sd3da21d',
                '5a6sd5as65da3s2d545sd4a',
                'as4d534a3s5d4a3s5da35sd',
                'a3sd4a35s4d35a4sd3a4ds3',
                '45as4d3a54s3d5a43sd4ad3',
                '43a5s4d35a4sd35a4s5a4s3',
                '435a4sd35a4s3d5a4s3d5d4',
                '3a5s4d35a4s3d5a4s3d543s',
                '4a35s4d3a5s4d35a4s3d5d4',
                '43as54d3a5s43d5a4s3d543');

    echo '<pre>';
    foreach(array_chunk($aarray, 5) as $token){
      print_r($token);
    }
    echo '</pre>';

4. Switch case instead of if-else

Use Switch instead of repetitive If conditions. The advantage of this, 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.

if-else condition:

if($color == 'yellow'){
	echo "The color is yellow";
}
elseif($color == 'black'){
	echo "The color is black";
}
elseif($color == 'red'){
	echo "The color is red";
}
elseif($color == 'pink'){
	echo "Color is pink";
}else{
	echo "Color is white";
}

Switch case:

switch ($color ) {
	case 'yellow' :
	echo "The color is yellow" ;
break ;
	case 'black' :
	echo "The color is black" ;
break ;
	case 'red' :
	echo "The color is red" ;
break ;
	case 'pink' :
	echo "Color is pink" ;
break ;
default:
	echo "Color is white";
}

5. Use the HTTP request POST method instead of the GET method

HTTP works as a request-response protocol between a client and a server. A client submits an HTTP request to the server, and the server returns a response to the client. The response contains information about the requested content. There are two types of methods:

GET – Requests data from a specified resource.

POST – Submits data to be processed to a specified resource.

Always validate the POST method like this:

if the field is required:

if(!empty($_POST['email']) && isset($_POST['email'])){
	$email = $_POST['email']
}

or

if(isset($_POST['email'])){
	$email = $_POST['email']
}

6. PHP Ternary oparartors 

Ternary operator in PHP development is an alternative to simple IF conditions. This would help your code to get lighter and can be nested without problems.
These methods will save you time and less your code and make your habit with best practices.

$phone = (!empty ( $_POST['phone'] ) ? $_POST['phone'] : 9111111111);

So that’s all for today. Will discuss more advanced tricks and tips in the next part.

Total
0
Shares
Previous Post
Disabled ufw on AWS console

Disable UFW on AWS EC2 using AWS Console

Next Post
How the future can be changed with Metaverse and Artificial Intelligence

Metaverse and Artificial Intelligence

Related Posts