Explain the difference between the following (2) timing functions/scripts

84 views Asked by At

Why does the PHP complete and return a resulting load time before the Javascript?

So PHP is returning much quicker than the Javascript, and I'm sure this is is a quickie for most of you. Let me know.

<?php
$time = microtime();
$time = explode(' ', $time);
$time = $time[1] + $time[0];
$start = $time;
?>

<!doctype html>
<html>
<head>
<script>
    var d = new Date();
    var starttime = d.getTime();
</script>
</head>
<body>
<p id="loadtime">Write your custom message here</p>
<script>
    for(i=0; i<1000000000;i++){}
</script>
<script>
    var d2 = new Date();
    var endtime = d2.getTime();
    var totaltime = (endtime - starttime)/1000;
    var result = totaltime;
    document.getElementById("loadtime").innerHTML = "Page loaded in: " + result + " seconds";
</script>
</body>
</html>
<?php
$time = microtime();
$time = explode(' ', $time);
$time = $time[1] + $time[0];
$finish = $time;
$total_time = round(($finish - $start), 8);
echo 'Page generated in '.$total_time.' seconds.';
?>
1

There are 1 answers

0
thefourtheye On BEST ANSWER

Because PHP doesn't execute the javascript, your browser does.

PHP code is executed in the server, it prepares the javascript and simply sends it to the browser. Its the job of the browser to execute the javascript.