note 72464 added to function.require-once
| From: | sneskid at hotmail dot com | Date: | Fri, 19 Jan 2007 20:00:45 +0000 |
| Subject: | note 72464 added to function.require-once | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-121926@lists.php.net to get a copy of this message | ||
<?php
function & rel($r, &$f) {return file_exists( ( $f = ( dirname($r).'/'.$f ) ) );}
function & relf($r, $f) {return rel($r,$f) ? file_get_contents($f) : null;}
function & reli($r, $f) {return rel($r,$f) ? include($f) : null;}
function & relr($r, $f) {return rel($r,$f) ? require($f) : null;}
function & relio($r, $f) {return rel($r,$f) ? include_once($f) : null;}
function & relro($r, $f) {return rel($r,$f) ? require_once($f) : null;}
?>
I found it useful to have a function that can load a file relative to the calling script and return
null if the file did not exist, without raising errors.
<?php
/*
Load file contents or return blank if it's not there.
Relative to the file calling the function.
*/
echo relf(__FILE__, 'some.file');
?>
It was easy to modify and just as useful for require/include.
<?php
/*
Require the file once.
It's like suppressing error messages with @ but only when the file does not exist.
Still shows compile errors/warning, unless you use @relro().
Relative to the file calling the function.
*/
relro(__FILE__, 'stats.php');
?>
If you work with a deep php file structure and a barrage of includes/requires/file-loads this works
well.
----
Server IP: 69.147.83.197
Probable Submitter: 74.118.123.241
----
Manual Page -- http://www.php.net/manual/en/function.require-once.php
Edit -- https://master.php.net/note/edit/72464
Del: integrated -- https://master.php.net/note/delete/72464/integrated
Del: useless -- https://master.php.net/note/delete/72464/useless
Del: bad code -- https://master.php.net/note/delete/72464/bad+code
Del: spam -- https://master.php.net/note/delete/72464/spam
Del: non-english -- https://master.php.net/note/delete/72464/non-english
Del: in docs -- https://master.php.net/note/delete/72464/in+docs
Del: other reasons-- https://master.php.net/note/delete/72464
Reject -- https://master.php.net/note/reject/72464
Search -- https://master.php.net/manage/user-notes.php