Page Load Time In PHP

Page Load Time In PHP

FINAL OUTPUT:

Many times we want to calculate page rendering time in php. So we can understand page load time, server and resources load time in our php code.

Using microtime() php function, we can calculate bit level of consume time by resources in php. So to achieve this task just follow these steps.

 

Step 1.

Create a php file pageload.php and write the following code as it is

[php]
<?php
//page start
$time = microtime();
// split total time in two part minutes and seconds.
$time = explode(‘ ‘, $time);
// combine minutes and seconds
$time = $time[1] + $time[0];
// save in to other variable
$start = $time;
// Do all other stuff like database query and load content from database and whatever you like.
//page end section
$time = microtime();
$time = explode(‘ ‘, $time);
$time = $time[1] + $time[0];
$finish = $time;
//calculate total time to render this page.
$total_time = round(($finish – $start), 4);
//print page load time.
echo ‘Page loaded in ‘.$total_time.’ seconds.’;
[/php]

 

So your complete code should look like this..

[php]
<?php //page start $time = microtime(); // splite total time in two part minutes and seconds. $time = explode(‘ ‘, $time); // combine minutes and seconds $time = $time[1] + $time[0]; // save in to other variable $start = $time; // database query $link = mysqli_connect(‘localhost’,’database_user’,’database_password’,’database_name’); $sql = "SELECT * FROM table"; try{ $result = mysqli_query($link, $sql) or die(mysqli_error($link)); } catch(Exception $e){ echo $e->getMessage();
}
while($row = mysqli_fetch_array($result)){
echo $row[‘table_field_name’];
}
$time = microtime();
$time = explode(‘ ‘, $time);
$time = $time[1] + $time[0];
$finish = $time;
//calculate total time to render this page.
$total_time = round(($finish – $start), 4);
//print page loaded time.
echo ‘Page loaded in ‘.$total_time.’ seconds.’;
//page finish
[/php]

Don’t forget to share your doubts in the comment box and also share this post on social media and with your friends becaus“You share, I share, let’s make the world aware”.

You may want to take a look at the following related posts:

Also for more awesome tutorials, please don’t forget to like our facebook page Meul Tech .

Bonus: We also give training on following topics:

                      1. Codeigniter Training  in Mumbai .

2. Web Designing Training in Mumbai.

3. Bootstrap Training Course in Mumbai.

4. UI / UX Training.

5. IOS Training Institute in Mumbai.