9 Advanced PHP Hacks That Only Expert PHP Developers Know About

PHP

Sameer, one of our PHP developers, read a question on the PHP forum where a novice developer had asked advanced PHP tips to learn PHP web development.

To answer the question, Sameer has listed 9 useful advanced PHP tips and techniques that can be used to enhance and learn PHP code.

We thought to share it here on the blog as well.

Are you ready to explore it?

2

 

  1. Php background process code

    Index.php

    $url = 'backgroundScript.php';
    $postfields = ['company_name' => "spaceo", 'developer_name' => "sameer"];
    $ch = curl_init($url);
    curl_setopt_array($ch, array(CURLOPT_RETURNTRANSFER => true, CURLOPT_NOSIGNAL => 1, CURLOPT_TIMEOUT_MS => 50, CURLOPT_VERBOSE => 1, CURLOPT_POSTFIELDS => http_build_query($postfields), CURLOPT_HEADER => 1, CURLOPT_FRESH_CONNECT => true,));
    curl_exec($ch);
    curl_close($ch);
    

    backgroundScript.php

    set_time_limit(0);
    header("Connection: close");
    ignore_user_abort(true);
    // write your background execute code
    

     

  2. Delete whole folder with it’s content

    $dir = 'put your folder url path here'; // ex: '../uploads/media/1
    if (is_dir($dir)) {
    array_map('unlink', glob($dir . '/*'));
    rmdir($dir);
    }
    

     

  3. Print debug log in particular location

    index.php

    require_once(errorLog.php');
    cloud_error_log('put your error result here'); // error result like text, array etc.
    

    errorLog.php

    function cloud_error_log($errordata) {
    $handle = fopen('errorLogFile.txt', 'a+');
    $logtext = "******************" . date('d-m-Y h:i:s') . "******************\n\n";
    $logtext .= print_r($errordata, true);
    $logtext .= "\n\n**********************************************************\n\n";
    $errorlog = fwrite($handle, $logtext);
    fclose($handle);
    chmod('errorLogFile.txt', 0777);
    return true;
    }
    

     

  4. Want to Develop a Web Application?

    Book your free consultation with web app experts.

  5. Getting list of dates from day name of particular year and month

    function getMonthDatefromDay($month, $year, $searchdayname) {
    $start_date = "01-".$month."-".$year;
    $start_time = strtotime($start_date);
    $end_time = strtotime("+1 month", $start_time);
    for($i=$start_time; $i<$end_time; $i+=86400)
    {
    if(strtolower(date('D', $i)) == $searchdayname)
    $list[] = date('Y-m-d', $i);
    }
    return $list;
    }
    print_r(getMonthDatefromDay(09, 2016, 'mon'));
    

     

  6. Encrypt and Decrypt password

    function encrypt_decrypt($string, $action='encrypt') {
    $encrypt_method = "AES-256-CBC";
    $secret_key = 'AA74CDCC2BBRT935136HH7B63C27'; // user define private key
    $secret_iv = '5fgf5HJ5g27'; // user define secret key
    $key = hash('sha256', $secret_key);
    $iv = substr(hash('sha256', $secret_iv), 0, 16); // sha256 is hash_hmac_algo
    if( $action == 'encrypt' ) {
    $output = openssl_encrypt($string, $encrypt_method, $key, 0, $iv);
    $output = base64_encode($output);
    }
    else if( $action == 'decrypt' ){
    $output = openssl_decrypt(base64_decode($string), $encrypt_method, $key, 0, $iv);
    }
    return $output;
    }
    
    echo "Your Encrypted password is => ". $pwd = encrypt_decrypt('spaceo', 'encrypt');
    echo '<br/>';
    echo "Your Decrypted password is => ". encrypt_decrypt($pwd, 'decrypt');
    

     

  7. Create random Unique Key

    echo md5(uniqid(time()));
    
  8. Display users with suffix. (ex: Facebook users like 10K)

    function formatWithSuffix($input)
    {
    $suffixes = array('', 'K', 'M', 'B', 'T');
    $suffixIndex = 0;
    while(abs($input) >= 1000 && $suffixIndex < sizeof($suffixes)) {
    $suffixIndex++;
    $input /= 1000;
    }
    return ($input > 0 ? floor($input * 1000) / 1000 : ceil($input * 1000) / 1000). $suffixes[$suffixIndex];
    }
    echo formatWithSuffix('10000');
    

     

  9. Multiple array sorting

    $data = array(['id'=>1,'name'=>"sameer"],['id'=>2,'name'=>"yagnesh"],['id'=>3,'name'=>"chirag"]);
    foreach ($data as $key => $row) {
    $id[$key] = $row['id'];
    $name[$key] = $row['name'];
    }
    array_multisort($id, SORT_DESC, $name, SORT_ASC, $data);
    print_r($id);
    print_r($name);
    

     

  10. Get size of the specified URL or file

    echo filesize("test.txt");
    

     

Plus, bonus tip:

Never use for($i=0; $i<count($arrayName); $i++){…} instead of this use $cnt = count($arrayName); for($i=0; $i<$cnt; $i++){…}.

In the above example, the first method will call count function on each iteration for loop, while in the second iteration, count function is being called only once.

Hope, these tips will help you in your next PHP web development project.

Anything left? Do you know some exceptional PHP coding tips? Share it in the comment section.

In case, if you have any PHP development project Idea in your mind, then contact Space-O Technologies and hire PHP developer to develop feature-rich PHP website.

Also Read:

This page was last edited on December 28th, 2020, at 6:09.

Author Bio

Jigar Mistry

Jigar Mistry

Designation: Vice President (Technologies)

Jigar Mistry is Vice President (Technologies) at Space-O Technologies. He has 14+ years of experience in the web and mobile app development industry. He has expertise in different mobile app categories like health and fitness, eCommerce, and on-demand.

 
 

Have an App Idea?

Get your free consultation now

Leave a Reply

Your email address will not be published. Required fields are marked *