Siksha Sarovar

Siksha Sarovar (sikshasarovar.com) is a free educational web application that helps students in India learn programming and prepare for academic and competitive exams. The platform offers structured coding courses (C, C++, Python, Java, HTML, CSS, PHP, Power BI, AI, Machine Learning, Data Science), complete university curriculum notes for BCA/MCA students with previous year question papers, Class 10 and Class 12 CBSE/HBSE school notes, and dedicated preparation material for SSC, UPSC, Banking, Railway and other government exams. Browsing the site is completely free and requires no account. Users may optionally sign in with Google solely to save their learning progress, quiz scores and personal preferences across devices.

Privacy Policy | Terms of Service | Contact Siksha Sarovar | About Siksha Sarovar

v4.0.9 · PWA
Siksha Sarovar logo
Siksha Sarovar
Your Learning Universe

Siksha Sarovar is a free e-learning platform for coding courses, BCA university notes and competitive exam preparation. Optional Google sign-in saves your learning progress across devices.

Initializing knowledge base…
Compiling modules 0%

14. Include Functions

Lesson 11 of 29 in the free PHP Programming notes on Siksha Sarovar, written by Rohit Jangra.

Include Functions in PHP

Include functions are used to insert the content of one PHP file into another PHP file. They help in code reusability, easy maintenance, and modular programming.

Why Include Functions are Needed?

Without include:

  • Same header/footer code repeated
  • Difficult maintenance

With include:

  • One common file
  • Changes reflect everywhere
Exam Definition: Include functions allow reuse of PHP code by inserting external files into the current script.

---

1. include() Function

The include() function includes a file and continues execution even if the file is not found.

Syntax

include("filename.php");

Example

include("header.php");
echo "Main Content";

If File is Missing

  • Warning is shown
  • Script continues execution
Exam Point: include() generates a warning but does not stop script execution.

---

2. include_once() Function

  • Includes a file only once
  • Prevents duplicate inclusion

Syntax

include_once("filename.php");

Example

include_once("config.php");
include_once("config.php");   // Will not be included again
Use Case: Database connection files.

---

3. require() Function

The require() function includes a file and stops execution if the file is not found.

Syntax

require("filename.php");

Example

require("db.php");
echo "Connected";

If File is Missing

  • Fatal error occurs
  • Script stops execution
Exam Line: require() is used for critical files.

---

4. require_once() Function

  • Includes a file only once
  • Stops execution if file is missing

Syntax

require_once("filename.php");

Example

require_once("config.php");

---

Difference Between Include and Require

Featureincluderequire
Error TypeWarningFatal Error
Script ExecutionContinuesStops
File ImportanceOptionalMandatory

Difference Between *_once Versions

FunctionPurpose
include_onceAvoid multiple includes
require_onceAvoid multiple requires
Exam Tip: Use require_once for database configuration files.