Jump to content

how to calculate script parsing time in php<5


saeed_violinist

Recommended Posts

Dear Friends,

I am using php 5 on my local and I have finished a simple webdesign, now when I uploaded scripts, a class that calculate parsing time of a page is not working with my server that uses php 4, the class is as follow (I got it from php.net) :

[code]

<?php
   class stopwatch
   {
       private $round    = 3;
   
       function __construct ( )
       {
           $this->start = microtime();
       }
       
       function now ( )
       {
           $start    = $this->math($this->start);
           $now    = $this->math();
           return round($now - $start, $this->round);
       }

       function math ($time = FALSE)
       {
           if ( !$time ) $time = microtime();
           $temp = explode(' ', $time);
           return $temp[0] + $temp[1];
       }
       
   }
?>

usage:
$stopwatch = new stopwatch();

/* some code */

echo $stopwatch->now();

[/code]

this class works fine on the localhost (php5) but when I try ro run it on php4 runing server I get this error message :

Parse error: parse error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /home/sni/domains/sni.co.ir/public_html/data/cls.php on line 9
There are differences on how you call the constructor in PHP 4 and PHP 5, check manual for those differences, if you want to support PHP 4 and PHP 5, then use the PHP 4 way, which works with both versions.

[code]<?php

class stopwatch
{
var $round;
var $start;

function stopwatch ()
{
$this->start = microtime ();
$this->round = 3;
}
     
function now ()
{
$s = $this->math ( $this->start );

$e = $this->math ();

return round ( $e - $s, $this->round );
}

function math ( $t = false )
{
if ( ! $t )
{
$t = microtime ();
}

return array_sum ( explode ( ' ', $t ) );
}
}

?>[/code]


printf

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.