require The
require statement requires and evaluates the specified file.
Usually this file is stored in the include_path which is set in the php.ini, however, it can be in any other path as long as known.
If a filename begins with ../, the parser will look in the parent directory to find the requested file.
This file is subject to all PHP rules, this implies check in the calling script own directory and the current working directory before failing.
If the provided path, absolute or relative to the current directory, the include_path will be ignored altogether.
The require construct will emit an E_COMPILE_ERROR if it cannot find a file which causes the script to be stopped.
In general, their behavior is equal to the include.
When a file is required, the code it contains inherits the variable scope of the line on which the require occurs.
Any variables available at that line in the calling file will be available within the called file, from that point forward.
However, all functions and classes defined in the required file have the global scope.
<?php
require
This file is intended to contain
all the elements necessary
for the current programming environment.
This involves:
functions, objects, user constants, etc.
In short, all the elements
that can be constantly used
in this environment.
?>
<?php
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
inc01.php
Included in system include_path
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
define("CONST01", 48484);
define('CONST02', "This is constant number 2");
function add2vals($v01, $v02)
{
$sum = ($v01 + $v02);
echo $v01 . ' + ' . $v02 . ' = ' . $sum;
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - -
This is only a simple example
- - - - - - - - - - - - - - - - - - - - - - - - - - - */
?>
<?php
// pathvalues.php
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Some examples of constants to be used as a
path to directories in the next exercises
in this tutorial and which should be used as
a suggestion.
You are free to choose your own paths to
such directories.
All DIRECTORY used in the define statement MUST EXIST
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
define ('P2ADDN', 'H://WEB/adds/');
define ('P2ADDW', 'file://H://WEB/adds/');
define ('P2TMPN', 'H://WEB/temp/');
define ('P2TMPW', 'file://H://WEB/temp/');
define ('P2SSLN', 'H://WEB/SSL/');
define ('P2SSLW', 'file://H://WEB/SSL/');
define ('P2INCN', 'H://WEB/includes/');
define ('P2INCW', 'file://H://WEB/includes/');
define ('P2GNCN', 'H://WEB/');
define ('P2GNCW', 'file://H://WEB/');
define ('P2ANCN', 'H://WEB/public_htm/');
define ('P2ANCW', 'file://H://WEB/public_html/');
?>
EXERCISE
<?php
require('inc01.php');
require('pathvalues.php');
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
inc01.php
pathvalues.php
Included in system include_path
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
echo 'This is the CONST01 value: ' . CONST01;
echo '<br><br>This is the CONST02 value: "' . CONST02 . '"<br><br>';
add2vals(333, 888);
?>
EXERCISE
<?php
require('code/inc02.php');
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
inc02.php
Included in code/ path
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
echo 'This is the CONST03 value: ' . CONST03 . '<br><br>';
add2vars(333, 888);
?>
RESULT
This is the CONST03 value: 48484
333 + 888 = 1221