PHP Include File

You can insert the content of one PHP file into another PHP file before the server executes it, with the include() or require() function.

The two functions are identical in every way, except how they handle errors:
* include() generates a warning, but the script will continue execution
* require() generates a fatal error, and the script will stop

These two functions are used to create functions, headers, footers, or elements that will be reused on multiple pages.

PHP include() Function

The include() function takes all the content in a specified file and includes it in the current file.
If an error occurs, the include() function generates a warning, but the script will continue execution.

Consider below example, in the ‘test.php’ file just assigned one value into $var variable.

<?php
//test.php

$var = 'You can learn';

?>

In test1.php file include the ‘test.php’ file and just merge two variable. see below example,

<?php
//test1.php

include 'test.php';

$var1 = $var.' Web Development things here.';

?>

The output of the code above could be something like this:

You can learn Web Development things here.

PHP require() Function

The require() function is identical to include(), except that it handles errors differently.

If an error occurs, the include() function generates a warning, but the script will continue execution. The require() generates a fatal error, and the script will stop.

require(‘test.php’);

Posted in Php