Tag Archives: PHP5

What is difference between assigning a variable to NULL and unset?

Difference between assigning a variable to NULL and unset.

[php]<?php $var = NULL; ?>[/php]

A variable is considered to be NULL if

It has been assigned the constant NULL.
It has not been set to any value yet.
It has been unset().

Setting a variable to NULL means assigns the value NULL to the variable exactly as the variable is set to a value and value is the special value NULL.
That means memory needs to be allocated where the NULL value is stored and an entry into the symbol table needs to be made.
The special NULL value represents a variable with no value. NULL is the only possible value of type NULL.


[php]<?php unset($var); ?>[/php]


unset() destroys the specified variables.
The behavior of unset() inside of a function can vary depending on what type of variable you are attempting to destroy.

unset() means the variable is no longer set, it doesn’t have a value and basically the variable doesn’t exist anymore.
Unsetting a variable removes the entry for the variable from the symbol table.



$var = NULL;
var_dump($var); -> NULL

var_dump($var); -> NOTICE: Undefined variable var on line 21



Casting to NULL
Casting a variable to NULL using (unset) $var will not remove the variable. It only return a NULL value.


unset does not force immediate memory freeing but leaves it for the Garbage Collector.
$var = NULL; however forces immediate memory release.


So,It totally depends on the situation you have, both are good to use,but setting the value to NULL is faster.


PHP: What is the difference between split and explode php?

Difference between split and explode php

Do you know what is the main difference between split() and explode()?

No worries ūüôā , the main difference in split() and explode is the way, it uses to splitting a large string.


Both the functions are used to Split a string. However, Split is used to split a string using a regular expression. On the other hand, Explode is used to split a string using another string.


The split() function splits the string into an array using a regular expression and returns an array.
Ex: split (:India:Nepal:Srilanka); returns an array that contains India, Nepal, Srilanka.


The explode () function splits the string by string.
Ex: explode (and India and Nepal and Srilanka); returns an array that contains India, Nepal, Srilanka.


Have a look by below example.

[php]split (":", "i:am:reading:scriptarticle.com");[/php]

returns an array that contains i,am,reading,scriptarticle.com.



The explode () function splits the string by string.

[php]explode ("I", "I am reading scriptarticle.com");[/php]

returns an array that contains

array (
0 => ”,
1 => ‘ am reading scriptarticle.com’,

Note: split () function has been DEPRECATED as of PHP 5.3.0.


It’s been deprecated because


  • explode() is substantially faster because it doesn’t split based on a regular expression, so the string doesn’t have to be analyzed by the regex parser
  • preg_split() is faster and uses PCRE regular expressions for regex splits


join() and implode() are aliases of each other and therefore don’t have any differences.


Hope it helps!!

How to achieve and use Polymorphism in PHP

Polymorphism – the concept


Polymorphism is a Greek word that means “many forms”. In object-oriented programming, polymorphism is a mechanism by which objects of different types can process data through a single interface. Using a typical illustrative example, a program might define a type of object called Animal, containing a function called talk() and, through inheritance, derive object sub-types such as Cat, Dog, etc. that each have their own specific implementation of the talk() function. The mechanism of polymorphism allows such a program to call the talk() function without actually knowing whether it is calling it for a Cat, Dog, or any other derivative Animal, while executing the correct function for the specific type of Animal.


Why method polymorphism cannot be achieved in PHP?
In languages like Java where in a function sum (int, int) differs from sum (float, float), the method sum() has many forms depending on the parameters being passed to it.

But in PHP it is not possible because you can have a method that accepts two parameters and call it by passing three parameters (allows variable arguments). This is because PHP is not strict and contains methods like func_num_args() and func_get_arg() to find the number of arguments passed and get a particular parameter.


Polymorphism is not the same as method overloading or method overriding (in OOP, a method is a function that belongs to a class, while the class variables are referred to as its members) and should not be confused with these.


Method overloading is where a class has two or more functions of the same name, but accepting a different number and/or data types of parameters.


Method overriding is where a child class (one derived through inheritance) defines a function with the same name and parameters as a function in its parent class, but provides a different implementation of the function. This is not the same as polymorphism, as it does not necessarily involve late-binding, however overriding is part of the mechanism by which polymorphism is accomplished.



PHP 5 & Polymorphism
Since PHP 5 introduces the concept of Type Hinting, polymorphism is possible with class methods. The basis of polymorphism is Inheritance and overridden methods.



class BaseClass {
public function myMethod() {
echo "BaseClass method called";

class DerivedClass extends BaseClass {
public function myMethod() {
echo "DerivedClass method called";

function processClass(BaseClass $c) {

$c = new DerivedClass();





DerivedClass method called


In the above example, object $c of class DerievedClass is executed and passed to the processClass() method. The parameter accepted in processClass() is that of BaseClass. Within the processClass() the method myMethod() is being called. Since the method is being called on the class variable of BaseClass, it would not be wrong to assume that myMethod() of class BaseClass will be called. But, as per the definition “When the decision to invoke a function call is made by inspecting the object at runtime it is called Polymorphism”, myMethod() will be called on object DerievedClass. The reason why this happens is because the object of DerievedClass is being passed and hence the method myMethod() of DerievedClass will be called.


I hope the above makes sense and hasn’t been too confusing as you know this is difficult to explain in clear English ūüôĀ


Cropping, rotating, scaling image sizes, and flipping the images vertically or horizontally in wordpress

Cropping, rotating, scaling image sizes, and flipping the images vertically or horizontally in WordPress


Generally we have lot of images in our website and we need different sized images, but generally in WordPress we have 3 sized images called as Small, Middle and Actual or Full size.


We can also scale down an image to fit a particular size and save a new copy of the image in our directory.


WordPress Function

image_resize( $file, $max_w, $max_h, $crop, $suffix, $dest_path, $jpeg_quality );


Here in image_resize function first 3 parameter are required and rest are optional depends on your requirement.


$crop (boolean)                   Default: false
$suffix (string)                      Default: null
$dest_path (string)             Default: null
$jpeg_quality (int)              Default: 90


Note : The PNG transparency will be preserved using the function, as well as the image type. If the file going in is PNG, then the re sized image is going to be PNG.The only supported image types are PNG, GIF, and JPEG.


PHP script for getting latest Tweets of a user

There are so many different ways to get tweets to display on your blog or web page,¬†unfortunately¬†most of these use JavaScript which means that the tweets displayed are not made available to search engines ūüôĀ


Don’t worry; you can also display your latest Twitter tweets using¬†PHP. You¬†can achieve by following very simple functions.


function getLastestTweets fetches latest tweets

It require 2 parameters
$userid = User Id of the twitter account
$x         = number of tweets to be fetched


function getLastestTweets($userid,$x){
$url = "http://twitter.com/statuses/user_timeline/$userid.xml?count=$x&quot";
$xml = simplexml_load_file($url) or die(‘could not connect’);
echo ‘<ul>’;
foreach($xml->status as $status){
$text = twitterify( $status->text );
echo ‘<li>’.utf8_decode($text).'</li>’;

$time = dateDiffForTweet(date("Y-m-d H:i:s"),date("Y-m-d H:i:s",strtotime($status->created_at)));
if(!sizeof($time)){ $time[0]= ‘1 second’; }
echo " ".$time[0]." ago";
echo ‘</ul>’;


function twitterify automatically converts hashtags (like #scriptarticle) to tag links, @links to author links (like @mahesh), and autolinks URIs (like http://scriptarticle.com ) to links.


function twitterify($ret) {
$ret = preg_replace("#(^|[\n ])([\w]+?://[\w]+[^ \"\n\r\t< ]*)#", "\\1<a href=\"\\2\" >\\2</a>", $ret);
$ret = preg_replace("#(^|[\n ])((www|ftp)\.[^ \"\t\n\r< ]*)#", "\\1<a href=\"http://\\2\" >\\2</a>", $ret);
$ret = preg_replace("/@(\w+)/", "<a href=\"http://www.twitter.com/\\1\" >@\\1</a>", $ret);
$ret = preg_replace("/#(\w+)/", "<a href=\"http://search.twitter.com/search?q=\\1\" >#\\1</a>", $ret);
return $ret;


function dateDiffForTweet is used for showing time difference between tweet posted and current, like 2 mins ago,2 days ago


function dateDiffForTweet($time1, $time2, $precision = 6) {
// If not numeric then convert texts to unix timestamps
if (!is_int($time1)) {
$time1 = strtotime($time1);
if (!is_int($time2)) {
$time2 = strtotime($time2);

// If time1 is bigger than time2
// Then swap time1 and time2
if ($time1 > $time2) {
$ttime = $time1;
$time1 = $time2;
$time2 = $ttime;

// Set up intervals and diffs arrays
$intervals = array(‘year’,’month’,’day’,’hour’,’minute’,’second’);
$diffs = array();

// Loop thru all intervals
foreach ($intervals as $interval) {
// Set default diff to 0
$diffs[$interval] = 0;
// Create temp time from time1 and interval
$ttime = strtotime("+1 " . $interval, $time1);
// Loop until temp time is smaller than time2
while ($time2 >= $ttime) {
$time1 = $ttime;
// Create new temp time from time1 and interval
$ttime = strtotime("+1 " . $interval, $time1);

$count = 0;
$times = array();
// Loop thru all diffs
foreach ($diffs as $interval => $value) {
// Break if we have needed precission
if ($count >= $precision) {
// Add value and interval
// if value is bigger than 0
if ($value > 0) {
// Add s if value is not 1
if ($value != 1) {
$interval .= "s";
// Add value and interval to times array
$times[] = $value . " " . $interval;

return $times;
// Return string with times
//return $times;
// return implode(", ", $times);


Here is the way; you can display your tweets


//my userid scriptarticle


If you face any issue to implement the code just left a comments below the post.

Use of Environment variables

Environment variables windows


Environment variables are a set of dynamic named values that can affect the way running processes will behave on a computer.


You can say, It is a dynamic “object” that stores a value, which in turn can be referenced by one or more software programs in Windows (OS). Environment variables help programs know what directory to install files in, where to store temporary files, where to find user profile settings, and many other things.


Variable names are NOT case sensitive in Windows OS.


Environment variables are dynamic because they can change. The values stored can be changed to match the current system’s setup and design (environment). They can also differ between computer systems because each computer can have a different setup.


There are a number of environment variables that get referenced by programs and can come in handy for a computer user to find needed information about their computer environment.

Below is the list of some common and important environment variables.




You can get some common information using environment variable quickly.


You can quickly access any of the above folders by entering the environment variable in the Windows Run box or Windows Search Box.
e.g: To get into the Application Data folder type %appdata% and then press Enter in the Run box.


The %appdata% environment variable contains the directory path to the Application Data folder for your user profile. This folder stores settings and logs, among other things, for various software programs. The settings and logs stored there are specific to your user profile.


The %commonprogramfiles% environment variable contains the directory path to the Common Files folder, within the main Program Files folder. This folder contains various files for common programs and utilities on a computer, mostly system and services related. The default directory path this variable points to is c:\Program Files\Common Files.


The %local% environment variable points to where the security policies & rules are located for the user’s account, Windows in general, Windows Firewall, Network, and various software programs on the computer. This environment variable is native to Windows 7.


The %localappdata% environment variable contains the directory path to where programs store their temporary files. Common temporary files to be stored here are Desktop Themes, Windows Error Reporting, program caching and Internet browser profiles. This environment variable is native to Windows Vista & Windows 7.


The %programfiles% environment variable contains the directory path to where programs are installed. This directory contains sub-directories for each program, which contain the primary files needed by each program in order to run on a computer. The default directory path this variable points to is c:\Program Files.


The %temp% environment variable contains the directory path to where temporary files stored. These temp files are often Internet temporary files and other user application temporary files (Microsoft Word, Excel, Outlook, etc.).


The %userprofile% environment variable points to the current logged in user’s profile and the directory where user profile data is stored. It is in this directory that a user can find the following folders: My Documents, My Music, My Pictures, Desktop, and Favorites (Internet Explorer bookmarks).


The %windir% environment variable points to the Windows directory, where Windows system files are located.The default directory path for most versions of Windows is c:\Windows (for Windows NT 4 and 2000, it is c:\WinNT).