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%

Practical 9: include, require, exit, die

Lesson 16 of 35 in the free Web Based Programming Lab notes on Siksha Sarovar, written by Rohit Jangra.

Aim

To write a program that passes control to another page using include and require, and terminates execution with exit() and die().

Theory

Real sites are assembled from parts — header, navigation, config, footer — pulled into each page with PHP's file-inclusion constructs. Both evaluate another PHP file as if its code were pasted at that point, so functions and constants it defines become available; the difference is failure behaviour:

  • include emits a warning when the file is missing and the script continues — right for optional fragments (a promo banner, a footer).
  • require raises a fatal error and stops the script — right for files the page cannot work without (configuration, database credentials).
  • include_once/require_once remember what has already been loaded and skip repeats, preventing "function already declared" fatals when shared files include each other.

Termination: exit() ends the script immediately; an optional string argument is printed first (an integer argument becomes the process exit code instead). die() is a pure alias of exit() — identical behaviour, conventionally used on failure paths. The classic idiom $conn or die("message") exploits short-circuit evaluation: if the left operand is truthy, or never evaluates the right side; if it is falsy, die() runs and the script stops. The @ operator seen before include suppresses the warning a missing file would print.

Requirements

  • XAMPP/WAMP with PHP 8.x, or PHP CLI
  • Code editor (VS Code); browser or terminal

Procedure

  1. Start Apache from the XAMPP Control Panel.
  2. Save the snippet as p09_include_require.php in C:\xampp\htdocs\wbplab.
  3. Run http://localhost/wbplab/p09_include_require.php or php p09_include_require.php.
  4. Remove the @ before include "missing_page.php" to see the warning, then change that include to require and watch the script die with a fatal error instead of continuing.
  5. Flip $isLoggedIn to false to trigger the exit() branch, and $dbConnected to true to let the final line run.

Explanation of the Code

  • The script first writes its own helper pages into sys_get_temp_dir() with file_put_contents(), so the demo is self-contained: header.php defines showHeader(), and config.php defines the constants STUDENT and EMAIL.
  • include $headerFile; pulls in the function definition; calling showHeader() afterwards proves the included code is now part of this script.
  • @include "missing_page.php"; fails silently (@ mutes the warning) and the next echo demonstrates that execution continued — the defining property of include.
  • require $configFile; loads the mandatory constants, then prints them; had the file been missing, nothing after this line would run.
  • exit() is guarded by if (!$isLoggedIn) — since $isLoggedIn is true, the script skips termination and prints that fact.
  • Finally, $dbConnected or die("...") fires because $dbConnected is false: the message prints and the script stops, so the last echo never executes.

Expected Output

The run prints: Welcome, Rohit! | rohit@example.com, then Script continues after failed include., then Student: Rohit Kumar | Email: rohit@example.com, then Rohit is logged in. exit() was skipped., then Connecting to database for Rohit... followed by DB connection failed for rohit@example.com. Script terminated. — and nothing more: the line This line will never execute. is unreachable because die() ended the script.

🎯 Viva Questions

  1. include vs require on a missing file? include warns and continues; require throws a fatal error and stops.
  2. When would you choose require_once? For shared files (config, function libraries) that might be pulled in from several places — it prevents duplicate-definition fatals.
  3. Is there any difference between die() and exit()? No — die is a language alias of exit; the choice is purely stylistic.
  4. How does $dbConnected or die("msg") work? or short-circuits: the right operand runs only when the left is falsy, so die() fires exactly on failure.
  5. What does the @ operator do? Suppresses the error message the following expression would emit — convenient in demos, discouraged in production because it hides problems.
  6. What happens to code defined in an included file? It joins the including script's scope — its functions, classes and constants become directly callable.

CO Mapping

CO1, CO2