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:
includeemits a warning when the file is missing and the script continues — right for optional fragments (a promo banner, a footer).requireraises a fatal error and stops the script — right for files the page cannot work without (configuration, database credentials).include_once/require_onceremember 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
- Start Apache from the XAMPP Control Panel.
- Save the snippet as
p09_include_require.phpinC:\xampp\htdocs\wbplab. - Run
http://localhost/wbplab/p09_include_require.phporphp p09_include_require.php. - Remove the
@beforeinclude "missing_page.php"to see the warning, then change thatincludetorequireand watch the script die with a fatal error instead of continuing. - Flip
$isLoggedIntofalseto trigger theexit()branch, and$dbConnectedtotrueto let the final line run.
Explanation of the Code
- The script first writes its own helper pages into
sys_get_temp_dir()withfile_put_contents(), so the demo is self-contained:header.phpdefinesshowHeader(), andconfig.phpdefines the constantsSTUDENTandEMAIL. include $headerFile;pulls in the function definition; callingshowHeader()afterwards proves the included code is now part of this script.@include "missing_page.php";fails silently (@mutes the warning) and the nextechodemonstrates that execution continued — the defining property ofinclude.require $configFile;loads the mandatory constants, then prints them; had the file been missing, nothing after this line would run.exit()is guarded byif (!$isLoggedIn)— since$isLoggedInistrue, the script skips termination and prints that fact.- Finally,
$dbConnected or die("...")fires because$dbConnectedisfalse: the message prints and the script stops, so the lastechonever 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
includevsrequireon a missing file?includewarns and continues;requirethrows a fatal error and stops.- 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. - Is there any difference between
die()andexit()? No —dieis a language alias ofexit; the choice is purely stylistic. - How does
$dbConnected or die("msg")work?orshort-circuits: the right operand runs only when the left is falsy, sodie()fires exactly on failure. - What does the
@operator do? Suppresses the error message the following expression would emit — convenient in demos, discouraged in production because it hides problems. - 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