PHP Include
The include
statement in PHP is used to include the content of one PHP file into another PHP file. This is useful for reusing code and keeping files organized.
Example of Using Include
<?php
include 'header.php';
?>
Explanation: This example includes the contents of the header.php
file at the point where the include
statement is called.
Using Include with Conditional Statements
<?php
if (file_exists('header.php')) {
include 'header.php';
} else {
echo "Header file not found.";
}
?>
Explanation: This example checks if the header.php
file exists before including it, preventing errors if the file is missing.