PHP Classes

File: example1.php

Recommend this page to a friend!
  Classes of Alessandro Quintiliani   Log Delta Time   example1.php   Download  
File: example1.php
Role: Example script
Content type: text/plain
Description: PHP script included in index.php and used as an example to show one type of log file (delta time between two consecutive statements)
Class: Log Delta Time
Log the time difference between PHP statements
Author: By
Last change:
Date: 8 years ago
Size: 4,069 bytes
 

Contents

Class file image Download
<?php
/*
 * ***************************************************************************************************
 *
 * File name: example1.php
 *
 * Copyright © 2015 Alessandro Quintiliani
 *
 * This file is part of LogDeltaTime.
 *
 * LogDeltaTime is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * LogDeltaTime is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with LogDeltaTime. If not, see <http://www.gnu.org/licenses/>.
 *
 * ***************************************************************************************************
 */

// DEBUG OF example1.php HAVING THE MINIMUM NUMBER OF THE REQUIRED METHODS (invoked methods: wlog, end)

const MYVAR_VALUE = 10;
const
MAX_FACTORIAL = 14;

include (
"Class.LogDeltaTime.php");

$LOGDIR = 'log';
$LOGFILE = 'logexample1.txt';
$log = new LogDeltaTime ( $LOGDIR, $LOGFILE, 1 ); // replace the value of the third parameter with 2 to append the statements to logexample1.txt after each run of example1.php

$log->wlog ( "first statement" );

$log->wlog ( "initializing myvar and c variables" );
$myvar = rand ( 7, MYVAR_VALUE );
$c = 0;

$log->wlog ( "check if myvar=" . MYVAR_VALUE . ": if so, a block code with a sum instruction is executed" );
$log->wlog ( "begin 'if' block having myvar as a control variable and executed only if myvar=" . MYVAR_VALUE );
$log->wlog ( "value of myvar: $myvar" );
if (
$myvar == MYVAR_VALUE) {
   
$log->wlog ( "entered the block code having myvar=$myvar" );
   
   
$log->wlog ( "first log message in the 'if' block having myvar=$myvar" );
   
$log->wlog ( "set values of a and b" );
   
$a = rand ( -5, 9 );
   
$b = rand ( -5, 9 );
   
   
$log->wlog ( "executing simple sum between a=$a and b=$b and the result placed in c" );
   
$c = $a + $b;
   
$log->wlog ( " c (= a + b) equals $c" );
   
   
$log->wlog ( "last log message in the 'if' block having myvar=$myvar as a control variable" );
}
$log->wlog ( "end 'if' block having myvar as a control variable and executed only if myvar=" . MYVAR_VALUE );

$log->wlog ( "begin new if block having c as a control variable used as input to calculate its factorial number (allowed values of c: [0-" . MAX_FACTORIAL . "])" );
$log->wlog ( "value of c: $c" );
if (
$c < 0) {
   
$log->wlog ( "first log message in the 'if' block having c as a control variable" );
   
$log->wlog ( " c=$c: cannot calculate the factorial of a negative number" );
   
$factorial = "undefined";
} elseif (
$c > MAX_FACTORIAL) {
   
$log->wlog ( "first log message in the if block having c>" . MAX_FACTORIAL );
   
$log->wlog ( " c=$c: " . MAX_FACTORIAL . " is the maximum allowed value for c" );
} else {
     
$log->wlog ( "first log message in the if block having c>=0 and c<=" . MAX_FACTORIAL );
       
$log->wlog ( " c=$c (c > 1 and c <= " . MAX_FACTORIAL . "): its factorial value can be calculated" );
        if (
$c <= 1) {
         
$log->wlog ( "c=$c --> c!=1 " );
         
$factorial = 1;
       } else {
           
$factorial = $c;
           
$log->wlog ( "begin for loop to calculate $c! in an iterative way" );
            for(
$i = $c; $i > 2; $i --) {
               
$stepfact = $c - $i + 1;
               
$log->wlog ( "execution factorial step $stepfact" );
               
$factorial *= ($i - 1);
               
$log->wlog ( "intermediate factorial value: $factorial" );
            }
           
$log->wlog ( "end for loop with factorial=$factorial" );
      }
}
$log->wlog ( "end new if block code having c as a control variable used as input to calculate its factorial number (allowed values of c: [0-" . MAX_FACTORIAL . "])" );

$log->wlog ( "at this point, c=$c, $c!=$factorial" );

$log->wlog ( "last statement" );

$log->end ();
unset (
$log );
?>