To make sure that a file is included and to stop your program if it isn’t, use require and its counterpart, require_once. These are exactly the same as include and include_once except that they make sure that the file is present; otherwise, the PHP mscript’s execution is halted, which wouldn’t be a good thing! You should use require minstead of include if the file you’re including defines either critical functions that your script won’t be able to execute, or variable definitions, such as database connection
details .
For example, if you attempt to require a file that doesn’t exist, as follows:
you’d get this error:
details .
For example, if you attempt to require a file that doesn’t exist, as follows:
<?php
require_once('add_wrong.php');
echo add(2, 2);
?>
require_once('add_wrong.php');
echo add(2, 2);
?>
Warning: main(add_wrong.php): failed to open stream: No such
file or directory in/home/www/htmlkb/oreilly/ch5/require_once.php on line 2
Fatal error: main( ): Failed opening required 'add_wrong.php'
file or directory in/home/www/htmlkb/oreilly/ch5/require_once.php on line 2
Fatal error: main( ): Failed opening required 'add_wrong.php'