Broad Network


The PHP include Construct

PHP Web Application - Part 1

Foreword: In this part of the series, I explain how the PHP include construct can be used to include a PHP program file.

By: Chrysanthus Date Published: 30 Jan 2014

Introduction

This is part 1 of my tutorial series, PHP Web Application. In this part of the series, I explain how the PHP include construct can be used to include a PHP program file.

The outputs of the code samples in this series are to the browser.

Note: in this article, if you cannot see any text or piece of code or if you think something (e.g. an image) is missing or link does not operate, just contact me at forchatrans@yahoo.com .

Pre-Knowledge
This series is part of my PHP Course volume. This series is of the advanced course. So you should have completed all the series before this one, in the volume. This series is a continuation in the volume. To know what you should have already covered, just click the link, “PHP Course” below.

The include() Construct
You can join separate related PHP files into one file to form one long file. In the one long file the code segments in the different files are related. To do this, you need one main file. The other files are fitted by the include() construct at different lines in the main file. When a file is fitted, it turns the line it is fitted into, into a series of statements that it has brought. There are some restrictions on the behavior of the code brought in; I will explain that in a different series.

You fit in a file, using the include() statement (construct). In other words, you include the fitted file. To demonstrate this, save the following two files in the working directory, with the names: incl.php and mainfile.php, respectively:

<?php

    $color = 'purple';
    $fruit = 'apple';

?>

<?php

    include 'incl.php';

    echo "A $color $fruit";

?>

Try the code at the browser by running the mainfile.php file. When you do that, your output will be:

    A purple apple

The first file has two initialized variables. The first statement in the second file is the include construct. It begins with the reserved word, include. This is followed by the name of the file in single quotes; then a semi colon. So all the code of the file, “incl.php” replaces the line where you have the include construct (parentheses are optional for any PHP construct).

Next in the main file (mainfile.php) is an echo construct, whose argument is a string in double quotes. There are two variables within the double quotes. Any variable within a double-quoted string is expanded (replaced by its value). So at the output, you see the values of the variables instead of the variables. This means the two files have been joined through the include construct. The inclusion takes effect, when the main file is run.

With inclusion and generally speaking, the code below the include file line in the main file, will be able to see the code of the included file.

The main interest in this tutorial is to see the effects of the file namespace in the resulting file of included files, and to see related features.

Scoping
Now, this is quotation from the PHP manual: “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. However, all functions and classes defined in the included file have the global scope.”

The calling file is the main file. The called file is the included file. There are two scopes in PHP: the function scope and the global scope. So, if the include construct is in the global scope of the main file, the included file fits into the global scope; if the include construct is in the function scope, the included file fits into that function scope.

The include_once() Construct
Imagine that you have a main file, with two include() constructs including two different files. There may be common code in the two included files. For example, in the resulting file, the same function may be defined twice because of the inclusion of the two files. Also, a variable may undergo unwanted reassignment because of inclusion of the two files. Such problems are not allowed especially with functions. So, if you use the include_once() construct instead of the include() construct, to include the second file, only code that has not already been included in the first included file, will be included from the second file. You code the include_once() construct in the same way that you code the include() construct. They are constructs and not functions, so the parentheses for the argument are optional.  

Directory of File
The include() argument should be typed in single quotes. If you type only a file name, PHP will look for the file in the current directory, where the main file resides. If you use a relative path such as,

        'dirA\dirB\filename.ext'

PHP will look for the file below the current directory in the dirA\dirB\ path. If you type the absolute path e.g.

    'c:\dir1\dir2\filename.ext'

PHP will look for the file using the absolute path.

Executing PHP File in Remote Server
Assume that there are two servers: the local server, which has the main PHP script and a remote server that has PHP code (script) ending with the return statement. The return statement in the remote script, e.g.

    return $bar;

is not part of a function; it is at the end of the script. It is possible for the main script at the localhost to have executed, the script at the remote server and return a value ($bar) to the main script. This is done using the include() construct in the main file. However, here the syntax is a bit different. It is:

    $receiver = include('URL');

This syntax is for the main script (file), where $receiver is of the main script and it receives the return value ($bar).

In this situation, if functions are defined in the remote file, only the functions will be included in the main file (not the rest of the code). A hypothetical URL for the syntax is:

    'http://www.example.com/dir1/file.php?var1=val1&var2=val2'

where “?var1=val1&var2=val2” is optional and is a set of variable/value pairs sent by the main script to the remote script. The coding is the same as that for the HTTP GET method. The remote script would use this data together with other data from the server to produce the return value. So, the include statement in the main file can be something like:

    $receiver = include('http://www.example.com/dir1/file.php?var1=val1&var2=val2');

That is it for this part of the series. We stop here and continue in the next part.

Chrys

Related Links

Basics of PHP
PHP Directory Function Basics
Understanding PHP Reference
PHP Function Arguments
Understanding Variable Scope in PHP
Object Oriented Programming in PHP
PHP Data Types Simplified
Exception Handling in PHP
PHP Regular Expressions
Sending Email with PHP
PHP Strings
Date and Time in PHP
PHP String Functions for Website Design
Advanced Course
PHP Variable Scope
Advanced Features of PHP Function
Array in PHP
PHP Two Dimensional Arrays
Understanding Object Oriented Programming in PHP
Advanced PHP Regular Expressions
Some features of PHP Entities
PHP Namespace
PHP Web Application

More Related Links

Major in Website Design
Web Development Course
HTML Course
CSS Course
ECMAScript Course
PHP Course

NEXT

Comments

Become the Writer's Fan
Send the Writer a Message