Posted on November 12, 2009 by Atif
Filed Under PHP

A while ago I was experimenting on how long does it take for a plain page to load on my local server. The output I was receiving was so small that it was being displayed in exponential form rather than decimal something like this 2.8848648071289E-5.
Well, to be honest I hate numbers with exponents because they are so confusing and secondly because they look ugly.
A quick Google for converting exponents to decimal didn’t work out for me, so I started out writing my own function using regular expressions except for the information that PHP automatically converts numbers with high precision into exponential form. “Strings!” was the first thing that flicked my mind reading that ad the second was “Regular Expressions”
So I made my own function to convert exponential into decimal, and the good thing is.. it works. Here is the trick
$total_time = 2.8848648071289E-5;
echo exp2dec($total_time);
function exp2dec($number) {
preg_match('/(.*)E-(.*)/', str_replace(".", "", $number), $matches);
$num = "0.";
while ($matches[2] > 0) {
$num .= "0";
$matches[2]--;
}
return $num . $matches[1];
}
Posted on June 14, 2009 by Atif
Filed Under PHP, Twitter
Got over 1000 followers on twitter? And you want to flaunt your twitter follower count proudly on your blog. The following PHP code is a PHP cURL code implementation to pull out the twitter followers using REST API. It will get you the number of followers on twitter using the cURL library. Just change the $screen_name and the script is pretty self explanatory. The code is tested working, You are free to do anything with this code, I take no responsibility further however you can ask me for support. I will try to help you out if I can.
NOTE: The code won't work for an hour if you 100 API calls per hours are completed.
<?
// get the number of followers
$screen_name = "atif089";Â Â Â Â Â Â // dont forget to chagne your username
$html = process("http://twitter.com/users/show.xml?screen_name=$screen_name");
// Parse the number of followers
$pattern = '<followers_count>(.*)<\/followers_count>';
ereg($pattern, $html, $matches);
// change this HTML as per your requirement
echo "<a href=\"http://twitter.com/$screen_name\">Follow me on twitter: (".$matches[1]." followers)</a>";
/**
* process : This method open a URL via cURL library
*
* @param string $url (must) This is the URL that you would like to open
* @param string $postargs (optional) Any arguments that you would like to send via POST method
*
*/
function process($url,$postargs=false){
$ch = curl_init($url);
if($postargs !== false){
curl_setopt ($ch, CURLOPT_POST, true);
curl_setopt ($ch, CURLOPT_POSTFIELDS, $postargs);
}
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.11) Gecko/2009060215 Firefox/3.0.11");
curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
?>
Posted on May 30, 2009 by Atif
Filed Under PHP
Include Function
Including files bring more convenience in managing your code. As the name states, include is a function that will simply dump the contents of the included file into the current file. This is cool because some times you may need some code that should be available on all pages. Say a menu or a website header.
Including files saves a lot of efforts by writing your code multiple times on each page. Yeah I know you copy and paste but that requires some more efforts too comparatively. Suppose you suddenly add a page in menu. Then? You need to update all the files. Using include function we can just create one file and include it all over in other files. Hence editing only one file will modify code in your other files.
I hope I am clear enough in explaining this to you. Let us start with an example. Oh before that, the syntax for include function is
<?php include("filename.php"); ?>
So let us first create a file called menu.php The file content would be something like this.
<a href="#">Home</a> -
<a href="#">About Us</a> -
<a href="#">Contact Us</a> <br />
Now let us create an index.php file. The index.php file will contain some content as well as the menu file. So this is what the code will be in index.php file
<html>
<head>
<title>Testing Include File</title>
<head/>
<body>
<?php include("menu.php"); ?>
<p>This page is created to display the working of include function</p>
</body>
</html>
See the preview

Require Function
The require function is almost same as the include function. The only difference is in error handling. When the included file doesn’t exist, a warning is displayed and the execution is preceded.
Where as when a file included via require function does not exist, then a fatal error is displayed and the execution is halted at the same line. The syntax is
<?php require(“filename.php”); ?>
Posted on May 30, 2009 by Atif
Filed Under PHP
Hope you are well versed with conditional statements in PHP. This time we are going to see how to work with looping structures. Looping structure are basically those statements which are to be executed recursively until some condition is fulfilled.
PHP provides 3 types of looping structures
- While Loop
- Do While Loop
- For Loop
While Loop
While loops are the simplest form of looping structures in PHP. If you have already worked with C then these resemble same as the while loop in C. The basic syntax is
while (condition) {
… Statement to be executed
… Statement to be executed
}
The above code will check for the condition and keeps executing the statements inside the loop or block until the condition is returned false. Let us see this with an example.
<?php
// lets try to echo numbers 0 to 10
$a = 0;
while ($a <= 10) {
echo $a;
$a++;
}
?>
The above code is pretty self explanatory, we took a variable $a with value 0. The condition is to check if $a is equal or less then 10, Then print the value of a on the screen. Increment the value of a by 1. Check the condition again and so on until the value of a is 10. and when it exceeds 10, the loop is terminated.

Do While
The Do While loop is almost similar to while loop. The only thing it differs from a simple while loop is that here the statements are executed first and then the condition is checked. The syntax is
do {
… statement 1
… statement 2
}Â while (condition) ;
Confused? Let us see an example
<?php
$i = 0;
do {
echo ++$i;
} while ($i < 10);
?>
Here we will see that the number from 1 to 10 are printed even though our condition states that $i < 10. This is because when the value of $i is 9 the condition is true and since the statement gets printed first, the value of $i is printed 10 and then the loop gets terminated as per the condition. See the image and get an idea

For Loop
The for loop is the most complex loop in PHP. Behavior is same like the for loop in C. Let see the syntax before explaining it.
for (expr1; expr2; expr3) {
statement 1;
statement 2;
}
If you take a look, the expr1 is the expression that is executed at the starting of the loop for once. Then the statements are executed, and the condition is checked after each iteration. The condition is stated in expr2. The another thing that is added here is the expr3. The expr3 is a statement that will be executed after each iteration. Looks like I’m confusing you once again. Let’s see an example
<?php
for ($i = 0; $i < 10; $i++) {
echo $i;
}
?>
I hope the example makes you clear about the topic. Here expr1 is executed first and $i is initialized. The next thing is the echo line is executed. The iteration is over, now the condition is checked and if the condition is true then expr3 i.e $i++ is executed and it goes into the loop once again. The output

Posted on May 29, 2009 by irfan07
Filed Under PHP
This is one of the most important tutorial for php programming is dealing with Conditional Statements.
Let me first illustrate how does this work. For instance whenever you need to check some conditions like whether the value of ‘a’ is greater than ‘b’ or vice versa you will be in need to use statements as if, if else, elseif, switch each have their own specific use and definite purpose to be fulfilled.
$a = 10; $b = 5;
if ($a > $b)
{
echo " A is Greater ";
//The controller enters this block only if true
// which in turn executes all the statements in this block
}
We got 4 conditional statements in PHP and most of other programming languages to have the same operators even the syntax is alike few programming languages might differ but the working is same.
Let me give you the syntax for them
1. if
if(Condition)
{
}
Example
if ($passwd==$myvalue)
{
echo "Password correct ";
}
2. if else
if(Condition)
{
}
else
{
}
Example
if ($passwd==$value)
{
echo " Password correct";
}
else
{
echo " Password Incorrect";
}
3.elseif
if(Condition)
{
}
else if (Condition)
{
}
else if(Condition)
{
}
else
{
}
Example
if ($a>$b AND $a>$c)
{
echo "A is Greater";
}
else if ($b>$a && $b>$c)
{
echo "B is Greater";
}
else if ($c>$a && $c>$b)
{
echo "C is Greater";
}
else
{
echo " All Values are equal";
}
4. Switch
switch( expression )
{
case expression:
case expression:
case expression:
default:
}
Example
switch (add)
{
case add: $sum=$a+$b;
echo $sum;
case minus: $sub=$a-$b;
echo $sub;
default: echo " Enter valid options like add or minus";
}
By these example I think you probably got the idea how to use and when to use such type of conditional statements, well effective utilization of these statements can solve any many logical problems and help create better algorithms.
Posted on May 15, 2009 by Atif
Filed Under PHP
Comments are those line which are not executed by the compiler. Comments in PHP are similar to comment in C or C++ or JAVA. Comments are the best way to explain the logic of an application. PHP supports both single line a multiple line comments. Let use see how we can add them.
Single Line Comments
There are 2 ways of writing single line comments in PHP. One is the classy old way of using double backslash (\\) and the other one is using the hash (#) symbol. Here’s the example
<?php
echo "Comment Test!"; // Lets test the commenting feature
echo "The other way"; # echo "nothing";
// echo "Does this line print?";
# echo "You can sever see this";
?>

That’s out output. I hope you got the meaning of comments
Multiple Line Comments
When single lines are not sufficient enough to accommodate you comments and you need to put a # symbol on each line, then multi line comments come to your rescue. Using multi line comments is as simple as single line comments. Just start a multi line comment with /* (front slash followed by an asterisk) and stuff everything and then close it with */ (asterisk followed by a front slash).
The example is pretty self explanatory, so lets hit with an example and see how it works
<?php
/* this is a multiline comment
You can use as many lines you want
Just make sure you close it properly */
echo "a multi line comment";
?>
As expected, you get the following output for the above code

So I think that was all about using comments in PHP.