This tutorial will demonstrate how to check how long your server is taking to generate content to the user's web browser. By running this on your website, you will be able to see which pages are taking longer than others to load - a must for any website developer!
In an unbelievably short amount of time, you will be running this script on any PHP enabled page in no time.
Let's get started then. Firstly, we need, as ever, to declare the start and opening of a PHP command. You need to do this at the very top of your page. You'll probably know how to do this, but just for the ease of copy and paste - this is the start of the script:
<?php
Now we've established that, let's move on. This script consists of using the microtime function. By using this, the script will return the current UNIX timestamp in microseconds. We use microseconds as you page should be generated in under one second. We then set $start to contain this.
If you've ever used microtime(); before, you'll know it consists of a space. We're going to remove that by using the explode(); function. All we're doing is telling the script to look for a space and assign the separate variables for each set. For example, the first value becomes $startarray[0] and the second becomes $startarray[1]. This completes the section of the code which needs to be at the top, so we can end the PHP tag too.
<?php
$start = microtime();
$startarray = explode(" ", $start);
$start = $startarray[1
] + $startarray[0
];
?>
Move onto page 2 for the second part, which needs to be placed at the bottom of your page.
So, we have first part. This is placed at the very top.
<?php
$start = microtime();
$startarray = explode(" ", $start);
$start = $startarray[1
] + $startarray[0
];
?>
Now, we're moving on to placing the following at the very bottom or as near as possible to still show the load time without breaking the layout.
Don't forget, we declare our PHP start tag.
<?php
As with the start, we need to find out the microtime(); again. We've already covered this, so let's go ahead and split it into an array too.
<?php
$end = microtime();
$endarray = explode(" ", $end);
$end = $endarray[1
] + $endarray[0
];
Everything looks familiar to so far. Let's calculate the difference. We have the start time, we have the end time. What would you do now? Calculate the difference between $end and $start, and then assign it to a variable. You knew that, right?
<?php
$end = microtime();
$endarray = explode(" ", $end);
$end = $endarray[1
] + $endarray[0
];
$total = $end - $start;
Great. So we have the difference now. This is our page load time. But hang on. Who wants page load with many digits after the decimal point? Maybe you do. If so, skip the part. I want to make sure my page load only shows two digits after the decimal point. By doing this, I keep the page tidier. This is simple. Just use the round(); function of PHP.
<?php
$end = microtime();
$endarray = explode(" ", $end);
$end = $endarray[1
] + $endarray[0
];
$total = $end - $start;
$total = round($total,2
);
You can edit 2 to your chosen value, if you wish.
With our page load calculated, we want to echo this output. You've used echo(); before, haven't you? If not - no worries. It's straight forward! We echo our variable $total with some information telling us that the load time is in seconds. This is the end of the command, so let's close the PHP tag too.
<?php
$end = microtime();
$endarray = explode(" ", $end);
$end = $endarray[1
] + $endarray[0
];
$total = $end - $start;
$total = round($total,2
);
echo $total." seconds</a>";
?>
Remember, the above code needs to be placed at the bottom. Move onto page 3 for a complete overview.
We've finished now. All that's left to you is to paste the following code at the TOP of your page.
<?php
$start = microtime();
$startarray = explode(" ", $start);
$start = $startarray[1
] + $startarray[0
];
?>
Don't forget to paste the following code at the BOTTOM of your page.
<?php
$end = microtime();
$endarray = explode(" ", $end);
$end = $endarray[1
] + $endarray[0
];
$total = $end - $start;
$total = round($total,2
);
echo $total." seconds</a>";
?>
This tutorial is up to date as of 24 August 09. While you're here, why not check out more tutorials? You could also subscribe to our RSS Feed, or even check out our Twitter - follow us for the latest updates.
For video tutorials, subscribe to our YouTube channel. Feel free to check out our blog for more updates.