Detecting Device Type in a web application: Mobile, Tablet or Desktop

To check whether the request is coming from mobile, tablet or desktop.

function detectDevice() {
	$tablet_browser = 0;
	$mobile_browser = 0;
	if (preg_match('/(tablet|ipad|playbook)|(android(?!.*(mobi|opera mini)))/i', strtolower($_SERVER['HTTP_USER_AGENT']))) {
	if (preg_match('/(up.browser||mmp|symbian|smartphone|midp|wap|phone|android|iemobile)/i', strtolower($_SERVER['HTTP_USER_AGENT']))) {
	if ((strpos(strtolower($_SERVER['HTTP_ACCEPT']),'application/vnd.wap.xhtml+xml') > 0) or ((isset($_SERVER['HTTP_X_WAP_PROFILE']) or isset($_SERVER['HTTP_PROFILE'])))) {
	$mobile_ua = strtolower(substr($_SERVER['HTTP_USER_AGENT'], 0, 4));
	$mobile_agents = array(
		'w3c ','acs-','alav','alca','amoi','audi','avan','benq','bird','blac',
		'wapr','webc','winw','winw','xda ','xda-');
	if (in_array($mobile_ua,$mobile_agents)) {

	if (strpos(strtolower($_SERVER['HTTP_USER_AGENT']),'opera mini') > 0) {
		//Check for tablets on opera mini alternative headers
		if (preg_match('/(tablet|ipad|playbook)|(android(?!.*mobile))/i', $stock_ua)) {
	if ($tablet_browser > 0) {
	   $device = 'tablet';
	} else if ($mobile_browser > 0) {
	   $device = 'mobile';
	} else {
	   $device = 'desktop';
	return $device;

Now call this function wherever you need it.

echo detectDevice();

Generate Random Number in PHP

function refrence_id()
    $refrence_id = 'DD'.mt_rand(100000, 999999);
    // check if number already exists in database
    $ref = mysql_query("select count(*) from registration where refrence_id='".$refrence_id."'");
    $refer_no = mysql_fetch_array($ref);
    if($refer_no[0] > 0)
    return $refrence_id;

$tmpRefID = refrence_id();

PHP Function to Calculate Age From Date of Birth

There are several times when you need to display the age of the user on your website. There are two ways to do that:

  1. Add Date of Birth column as well as age column.
  2. Display age from the Date of Birth.

Taking both the field from the user doesn’t seems to be good enough, so what to do ?

Just add the following function and you get the age of the user from the Date of Birth.

function get_age($birth_date)
    return floor((time() - strtotime($birth_date))/31556926);
echo "I am ".get_age("1989-10-21") ." years old";


I am 23 years old.

You need to write Date of Birth in yyyy-mm-dd format which is the default database date format.

What is Polymorphism in PHP? How to use Polymorphism?

Polymorphism is derived from two Greek words. Poly (meaning many) and morph (meaning forms). Polymorphism means many forms. In C you have two methods with the same name that have different function signatures and hence by passing the correct function signature you can invoke the correct method.

This is how polymorphism is achieved in languages like C where in a function sum(int, int) differs from sum(float, float). Therefore the method sum() has many forms depending on the parameters being passed to it.

The meaning with Object Oriented languages changes. With Object Oriented language polymorphism happens:
When the decision to invoke a function call is made by inspecting the object at runtime it is called Polymorphism

Why method polymorphism cannot be achieved
The reason why polymorphism for methods is not possible in PHP is because you can have a method that accepts two parameters and call it by passing three parameters. 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.
Because PHP is not type strict and allows variable arguments, this is why method polymorphism is not possible.

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.
Lets look at an example: Continue reading

Difference between Abstract Class and Interface In PHP

Abstract Classes

  • An abstract class can provide some functionality and leave the rest for derived class.
  • The derived class may or may not override the concrete functions defined in base class.
  • The child class extended from an abstract class should logically be related.


  • An interface cannot contain any functionality. It only contains definitions of the methods.
  • We can have a class extend from more than one Interface.
  • The derived class must provide code for all the methods defined in the interface.
  • Completely different and non-related classes can be logically grouped together using an interface.