In PHP, we can create a log file and write to it, whenever needed. The following is the php file ‘logfile.php’.

class logfile{
function write($the_string )
if( $fh = @fopen( “/logfile.txt”, “a+” ) )
fputs( $fh, $the_string, strlen($the_string) );
fclose( $fh );
return( true );
return( false );

In the above example, the class ‘logfile’ has a method ‘write’. This method will write the messages to the log file ‘logfile.txt’. Note that this file should exists before writing to it.

The below file uses ‘logfile.php’ to write the messages to the log file.

require ‘logfile.php’;
$lf = new logfile();
$lf->write(“Writing log file”);
echo “Data written to file”;
$lf->write(“Appending to log file”);