5 PHP MUST WANTED CODE SNIPPETS FOR EVERY WEB DEVELOPERS NEED

Every web developer should keep useful code snippets in a personal library for future reference.In this post we will show you some useful code snippets of PHP which will help you for your future projects.

TOP 5 PHP MUST WANTED CODE SNIPPETS FOR EVERY WEB DEVELOPERS NEED

Show Number Of People Who Liked Your Facebook Page

function fb_fan_count($facebook_name)
{
    $data = json_decode(file_get_contents("https://graph.facebook.com/".$facebook_name));
    $likes = $data->likes;
    return $likes;
}

SyntaX

<?php
$page = "smartwebcare";
$count = fb_fan_count($page);
echo $count;
?>

Sending an Email With MANDRILL

For the below function you would have to put a file “Mandrill.php” in the same folder as the PHP file you would be using to send emails.

function send_email($to_email,$subject,$message1)
{
	require_once 'Mandrill.php';
$apikey = 'XXXXXXXXXX'; //specify your api key here
$mandrill = new Mandrill($apikey);
 
$message = new stdClass();
$message->html = $message1;
$message->text = $message1;
$message->subject = $subject;
$message->from_email = "blog@smartwebcare.com";//Sender Email
$message->from_name  = "Smart Web Care";//Sender Name
$message->to = array(array("email" => $to_email));
$message->track_opens = true;
 
$response = $mandrill->messages->send($message);
}

In the above code you would have to specify your own api key that you get from your Mandrill account.

Syntax

<?php
$to = "abc@smartwebcare.com";
$subject = "This is a test email";
$message = "Hello World!";
send_email($to,$subject,$message);
?>

Zipping a File

function create_zip($files = array(),$destination = '',$overwrite = false) {  
    //if the zip file already exists and overwrite is false, return false  
    if(file_exists($destination) && !$overwrite) { return false; }  
    //vars  
    $valid_files = array();  
    //if files were passed in...  
    if(is_array($files)) {  
        //cycle through each file  
        foreach($files as $file) {  
            //make sure the file exists  
            if(file_exists($file)) {  
                $valid_files[] = $file;  
            }  
        }  
    }  
    //if we have good files...  
    if(count($valid_files)) {  
        //create the archive  
        $zip = new ZipArchive();  
        if($zip->open($destination,$overwrite ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE) !== true) {  
            return false;  
        }  
        //add the files  
        foreach($valid_files as $file) {  
            $zip->addFile($file,$file);  
        }  
        //debug  
        //echo 'The zip archive contains ',$zip->numFiles,' files with a status of ',$zip->status;  
          
        //close the zip -- done!  
        $zip->close();  
          
        //check to make sure the file exists  
        return file_exists($destination);  
    }  
    else  
    {  
        return false;  
    }  
}

Syntax

<?php
$files=array('file1.jpg', 'file2.jpg', 'file3.gif');  
create_zip($files, 'myzipfile.zip', true); 
?>

PHP Snippets For Directory Listing

Using the below PHP code snippet you would be able to list all files and folders in a directory.

function list_files($dir)
{
    if(is_dir($dir))
    {
        if($handle = opendir($dir))
        {
            while(($file = readdir($handle)) !== false)
            {
                if($file != "." &amp;&amp; $file != ".." &amp;&amp; $file != "Thumbs.db"/*pesky windows, images..*/)
                {
                    echo '<a target="_blank" href="'.$dir.$file.'">'.$file.'</a><br>'."n";
                }
            }
            closedir($handle);
        }
    }
}

Syntax

<?php
    list_files("images/"); //This will list all files of images folder
?>

Creating  A CSV File From PHP ARRAY

function generateCsv($data, $delimiter = ',', $enclosure = '"') {
   $handle = fopen('php://temp', 'r+');
   foreach ($data as $line) {
		   fputcsv($handle, $line, $delimiter, $enclosure);
   }
   rewind($handle);
   while (!feof($handle)) {
		   $contents .= fread($handle, 8192);
   }
   fclose($handle);
   return $contents;
}

Syntax

<?php
$data[0] = "apple";
$data[1] = "oranges";
generateCsv($data, $delimiter = ',', $enclosure = '"');
?>

 

Published by

Smart Web Care

Smart Web Care offers a wide range of IT Solutions ranging from Website Development, E-Commerce Solutions, Multimedia, Brand Promotions, Graphic design, Domain Registration, Android/iOS App developer,Web Designer, Software Application Company, Web hosting company, IT support and services, Internet Marketing Service, Marketing Consultant Servers, Web Hosting and Tanning provider.