Quakenet/#php Tutorial

Note: If you opened this page from an external URL pay attention that all chapters are linked together. Be sure you also read all prior chapters of this tutorial, otherwise you will miss relevant content explained before.

Include

  1. Putting php code in other files
  2. Include
  3. Include with directories
  4. Return value of include

1. Putting php code in other files

If you start writing your php scripts they get longer and longer. If you write other php scripts which have the similar layout you get a new php script which gets longer and longer. It would make more sense if you can put this code somewhere else and just say load the code xyz. One way would be to put the code inside a function, but this must be written in the php script and the same function may be written and use in other php scripts, too. So its better to use include. This language construct is given a filenamen and the php script loads this file as php code.

2. Include

If you use the language construct include the following actions take place in background.

  1. Leave the php modus (?>).

  2. Load and execute the file.

  3. Reenter php modus (<?php).

The execution is the same as the file contents is copy&pasted at the place where the include statement was.

<p>
    HTML Code in a file called <code>file.html</code>.
</p>
<?php
// content of a php file called index.php which is opened by an user

echo 'Before the include';

include 
'file.html';

echo 
'After the include';
?>

You will get the following html output.

Before the include<p>
    HTML Code in a file called <code>file.html</code>.
</p>
After the include

Now you see why include is leaving the php code. This way you can easily output html files. Internally it would looks like this.

<?php
// content of a php file called index.php which is opened by an user

echo 'Before the include';

?><p>
    HTML Code in a file called <code>file.html</code>.
</p>
<?php

echo 'After the include';
?>

That means if you want to load php files as php scripts you must still use <?php and ?> in your files.

<?php
// File: foobar.php
echo "A second line\n";
?>
<?php
// File: index.php, opened by an user
echo "First line\n";
include 
'foobar.php';
echo 
"Third line\n";
?>

Generates the following output.

First line
A second line
Thrid line

PHP has done something like this.

<?php
// File: index.php, opened by an user
echo "First line\n";
?><?php
// File: foobar.php
echo "A second line\n";
?><?php
echo "Third line\n";
?>

If we delete this ?<>?php lines we see the complete php code we wants.

<?php
// File: index.php, opened by an user
echo "First line\n";
// File: foobar.php
echo "A second line\n";
echo 
"Third line\n";
?>

For this reason we also can use variables which are used in another file.

<?php
// File: config.php
$surname 'John';
$lastname 'Doe';
?>
<?php
// file: index.php, opened by the user
include 'config.php';

echo 
'I am '.$surname.' '.$lastname.', welcome to my homepage';
?>

3. Include with directories

If you include a file inside a directory and this file also includes a file inside this directory php is looking at two positions for this file. The first position is relative to the file which wants to load the file. The second position is relative to the start script which is loaded in the browser. Here is an example how it works.

<?php
// index.php (opened by the user)
include 'inc/config.php';
echo 
'My name is "'.$name.'"';
?>
<?php
// config.php (inside of the directory inc/)
include 'functions.php';
include 
'variables.php';
echo 
"Config loaded\n";
?>
<?php
// functions.php (same directory as the file index.php)
function foobar() {
    
// ...
}
echo 
"Functions loaded\n";
?>
<?php
// variables.php (inside of the directory inc/)
$name 'John Doe';
echo 
"Variables loaded\n";
?>

The directory and files looks like followed.

index.php
functions.php
inc/config.php
inc/variables.php

If you open the index.php file you will get the following output.

Functions loaded
Variables loaded
Config loaded
My name is "John Doe"

It doesn't matter somehow where the files are which to load (in this case functions.php and inc/variables.php). Internally the following happends.

This can be a problem as there are two locations for searching a file (and even more with include_path). You can disable this if the path to the file starts with ./ or ../ (for the current and parent directory). If this is used the path is always relative to the start script and not relative to an other include anymore.

For developers this whole thing means that you you simply can use include without care about the paths where are other files are located or the current file is located. So you write just include 'variables.php'; instead of statements like include dirname(__FILE__).'/variables.php';.

4. Return value of include

Include files can use return like functions. This way you can use a include like a function and save the value in a variable.

<?php
// file.php
return "text";
?>
<?php
// index.php
$var = include 'file.php';
echo 
$var// echoes 'text'
?>

The include returns the integer value 1 by default. This is a common error for beginners which try to load a php script with sth. like echo include 'news.php' and wonders where this stupid 1 at the end is coming from. So this hint: echo include '...'; doesn't make sense in most of the cases.

Questions about the chapter

No questions

Back to Next to
Copyright © to the OPs of #php/QuakeNet Valid XHTML 1.0 Strict Valid CSS!