Including External Files in PHP

Server Side Includes

A way of including multiple files in a single php file, before its sent to the browser. Theres only one http request.


  • Break up large sections of code.
  • Keep output files of separate from process files.
  • Organise your website into different files, taking reuseable part and templatizing them

Include Functions

// if file does not exist include will give warrning or non terminating error
include 'file-name.php';
require 'file-name.php';
// require give us compile time error, which stop the program from running
// these can be pretty much any file extension

// require_once will include only file one time, the file we are trying to include already exists require_once will return true and will not add that file again.
require_once 'functions.php';

Overriding Functions

you can change variables basically whenever you want except for contants, you can not do this for functions and for good reason.


creating a separate scope for a set of functions or classes.

learningphp.php and index.php are in same folder

// learningphp.php

namespace learningPHP;
function double($a){
    return $a * 2;

// index.php
include 'learningphp.php'
function double($a){
    return $a + 2;

echo '<p>' . double(5) . '</p>';
echo '<p>' . learningPHP\double(5) . '</p>';