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']))) {
		$tablet_browser++;
	}
	
	if (preg_match('/(up.browser|up.link|mmp|symbian|smartphone|midp|wap|phone|android|iemobile)/i', strtolower($_SERVER['HTTP_USER_AGENT']))) {
		$mobile_browser++;
	}
	
	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_browser++;
	}
	
	$mobile_ua = strtolower(substr($_SERVER['HTTP_USER_AGENT'], 0, 4));
	$mobile_agents = array(
		'w3c ','acs-','alav','alca','amoi','audi','avan','benq','bird','blac',
		'blaz','brew','cell','cldc','cmd-','dang','doco','eric','hipt','inno',
		'ipaq','java','jigs','kddi','keji','leno','lg-c','lg-d','lg-g','lge-',
		'maui','maxo','midp','mits','mmef','mobi','mot-','moto','mwbp','nec-',
		'newt','noki','palm','pana','pant','phil','play','port','prox',
		'qwap','sage','sams','sany','sch-','sec-','send','seri','sgh-','shar',
		'sie-','siem','smal','smar','sony','sph-','symb','t-mo','teli','tim-',
		'tosh','tsm-','upg1','upsi','vk-v','voda','wap-','wapa','wapi','wapp',
		'wapr','webc','winw','winw','xda ','xda-');
	 
	if (in_array($mobile_ua,$mobile_agents)) {
		$mobile_browser++;
	}

	if (strpos(strtolower($_SERVER['HTTP_USER_AGENT']),'opera mini') > 0) {
		$mobile_browser++;
		//Check for tablets on opera mini alternative headers
		$stock_ua = strtolower(isset($_SERVER['HTTP_X_OPERAMINI_PHONE_UA']) ? $_SERVER['HTTP_X_OPERAMINI_PHONE_UA'] : (isset($_SERVER['HTTP_DEVICE_STOCK_UA']) ? $_SERVER['HTTP_DEVICE_STOCK_UA'] : ''));
		if (preg_match('/(tablet|ipad|playbook)|(android(?!.*mobile))/i', $stock_ua)) {
			$tablet_browser++;
		}
	}
	 
	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();

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.

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

Output:

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.

Interface

  • 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.