The include() function is used to include another file in the current file. If you have set variables in one file i.e. config.php and you would like to include it in another file i.e. index.php you would use the include() function.
I will show you a example of this. The following file is config.php where i will set the $vars;

<?php
$name = 'John';
$location = 'London';
$age = '26';
?>

Now save that file as config.php and create a file called index.php. The code follows for index.php;

<?php
include(config.php); // use of including config.php
echo ‘My name is:’ . $name . ‘Location:’ . $location . ‘Age:’ . $age;
?>

Save that as index.php. As you can see i have displayed the variables from config.php in the index.php file, this is done using the include() function which will include everything in config.php in the index.php file.

You can use the include() function to neaten your coding by placing codes in diffrent files and only including them when you need to use a function or variable from it.

Leave a Reply

You must be logged in to post a comment.