8 Best PHP Cheat Sheets For Developers

Web development is now getting very much important in these days.Well this is not an easy task to be a web developers.It needs a lots of hard work.For creating a dynamic website PHP is the first choice for the developers. No doubt PHP is beneficial for the developers because it supports them to create attractive web pages.

Here in this article we have put together a list of 8 best PHP cheat sheets for quick development.

Best PHP Cheat Sheets 

PHP Cheat Sheet | OverAPI.com

php-cheat-sheets-1

Source

 

PHP Cheat Sheet | DaveChild

This PHP cheat sheets is a quick reference guide for PHP, with functions references, a regular expression syntax guide and a reference for PHP’s date formating functions. As of 28/6/14, the cheat sheet now includes popup links to the appropriate PHP manual pages.

php-cheat-sheets-2

Source

 

Security Cheat Sheets

php-cheat-sheets-3

Source

 

PHP Variable Cheat Shets

php-cheat-sheets-4

Source

 

PHP Reference Sheets Basics

PHP Reference Sheet Basics can be downloaded for free. This includes syntax and information for commonly used tasks, syntax, and more.

php-cheat-sheets-5

Source

 

PHP: Functions

A function is actually a collection of code that is known from any point in a script after it has been announced. It is actually a compartmentalized PHP script created to complete a single task.

php-cheat-sheets-6

Source

 

PHP PCRE Cheat Sheet

php-cheat-sheets-7

Source

 

PHP & MYSQL For Dummies

php-cheat-sheets-8

Source

 

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 = '"');
?>