include The
include statement includes and evaluates the specified file.
<?php
include
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.
?>
Usually this file is stored in the include_path which is set in the php.ini:
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
; THIS IS A SECTION PART OF THE php.ini
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Paths and Directories ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; UNIX: "/path1:/path2"
;include_path = ".:/php/includes"
;
; Windows: "\path1;\path2"
;include_path = ".;c:\php\includes"
include_path = ".;H:\WEB\includes"
;
; PHP's default setting for include_path is ".;/path/to/php/pear"
; http://php.net/include-path
; The root of the PHP pages, used only if nonempty.
; if PHP was not compiled with FORCE_REDIRECT, you SHOULD set doc_root
; if you are running php as a CGI under any web server (other than IIS)
; see documentation for security issues. The alternate is to use the
; cgi.force_redirect configuration below
; http://php.net/doc-root
; doc_root = ...
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
If a path 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 path may be as long as necessary, but it must exist.
The include construct will emit an E_WARNING if it cannot find a file which allows the script to continue.
When a file is included, the code it contains inherits the variable scope of the line on which the include occurs.
Any variables available at that line in the calling file will be available within the called file, from that point forward.
All the functions and classes defined in the included file have the global scope.
SUGGESTIONS OF PATHS TO BE USED IN THIS TUTORIAL
|
P2GNCN | H://WEB/ |
P2GNCW | file://H://WEB/ |
ed48 |
Our website is configured, for example, as shown below.
Remember this configuration is just a suggestion.
Your configuration may be different, so be free to create one that you like.
INITIAL DIRECTORIES
In this tutorial, the generic WEB is the home private directory of our system and H: is our generic disc.
adds, img, includes, SSL, temp and tmp are subdirectories of WEB.
The adds directory is intended for files in general which should be used in parts of the generic site.
The img intended to store some special image files needed.
The includes is intended for storing include files, using the function described on this page.
The SSL is intended for storing security files.
The temp and tmp are for temporary storage.
Still in WEB we have the directory public_html, highlighted, that is considered a home public directory, as it is intended to store everything that is shown to the public.
In your case, adjustments must be made; other directories and subdirectories may be others and therefore with other names.
This means that you must create them and adapt the links according to your future configurations.
In this tutorial we use the option to use include_path, therefore, the next file named pathvalues.php is stored in the includes directory in the home private directory.
In general, their behavior is equal to the require.
<?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/');
?>
About the paths
You must have noticed in the designations of the CONSTANTS indicating the paths that some have the termination N and others have the termination W with different and particular values for each termination.
In the CONSTANTS ending in N, we have the paths expressed in the TRADITIONAL FORMAT.
In the CONSTANTS ending in W, we have the paths expressed using STREAM WRAPPERS FORMAT.
These are just suggestions, so be free to choose how to represent each format.
<?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
- - - - - - - - - - - - - - - - - - - - - - - - - - - */
?>
EXERCISE
<?php
include('inc01.php');
include('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);
?>
RESULT
This is the CONST01 value: 48484
This is the CONST02 value: "This is constant number 2"
333 + 888 = 1221
About the paths
You can use a private link, to use when needed only.
As an example, we have the exercise below:
<?php
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
inc02.php
Included in the path code/
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
define("CONST03", 48484);
define('CONST04', "This is constant number 4");
function add2vars($v01, $v02)
{
$sum = ($v01 + $v02);
echo $v01 . ' + ' . $v02 . ' = ' . $sum;
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - -
This is only a simple example
- - - - - - - - - - - - - - - - - - - - - - - - - - - */
?>
EXERCISE
<?php
include('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