Introduction
In this article I will explain some important Code Snippets in PHP. These snippets are very useful in projects. I explain them step-by-step in this article.
Example1
This example calculates age using date of birth. It is very useful when building communities or social media sites. This function is very fast and simple.
<?php
function age_from_dob($dob)
{
list($d,$m,$y) = explode('-', $dob);
if (($m = (date('m') - $m)) < 0)
{
$y++;
} elseif ($m == 0 && date('d') - $d < 0)
{
$y++;
}
return date('Y') - $y;
}
?>
Example2
In this example you must use the Twitter API and you can determine the friendship between two users. Both users however should be online for calculating friendship.
<?php
function make_request($url)
{
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
function get_match($regex,$content) {
preg_match($regex,$content,$matches);
return $matches[1];
}
$person1 = 'phpsnippets';
$person2 = 'catswhocode';
//send request to twitter
$url = 'https://api.twitter.com/1/friendships/exist';
$format = 'xml';


Join the conversation! Your thoughts help the community grow.