Create a Primitive Error Handler

March 6, 2008

PHP’s die() function is mostly used as a primitive error-handling mechanism.

In the event of a fatal error, die() can be used to terminate script processing

with an explanatory user-specified error message indicating the reason why.

The fopen(), fwrite(), and fread() functions are all binary-safe, which means you can use them on binary files without worrying about damage to the file contents.

Another way to do this is with the file_get_contents() function, new

in PHP 4.3.0 and PHP 5.0, which reads the entire file into a string.

 

Example Code:

 

<?php

// set file to read

$file = ‘/home/web/projects.txt’;

// read file into string

$data = file_get_contents($file) or die(’Could not read file!’);

// print contents

echo $data;

?>

Comments »

The URI to TrackBack this entry is: http://valkyrie.blogsome.com/2008/03/06/create-a-primitive-error-handler/trackback/

No comments yet.

RSS feed for comments on this post.

Leave a comment

Line and paragraph breaks automatic, e-mail address never displayed, HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>



Anti-spam measure: please retype the above text into the box provided.