Include and Require
Include Once Guard
include_once loads a file only the first time the program asks for it.
one-time load
Including a setup file twice with `include_once` avoids running the setup twice.
Include Once Guard
include_once_guard.php
Replay: real traced execution (multi-file project)
<?php
$base = 5;
$loadCount = 0;
include_once __DIR__ . "/counter_setup.inc";
include_once __DIR__ . "/counter_setup.inc";
$total = $base + $loadCount;
echo "loadCount=" . $loadCount . "\n";
echo "total=" . $total . "\n";
<?php
$base = 2;
$loadCount = 0;
include_once __DIR__ . "/counter_setup.inc";
include_once __DIR__ . "/counter_setup.inc";
$total = $base + $loadCount;
echo "loadCount=" . $loadCount . "\n";
echo "total=" . $total . "\n";
<?php
$base = 10;
$loadCount = 0;
include_once __DIR__ . "/counter_setup.inc";
include_once __DIR__ . "/counter_setup.inc";
$total = $base + $loadCount;
echo "loadCount=" . $loadCount . "\n";
echo "total=" . $total . "\n";
$base ← 5, $loadCount ← 0, $total ← 5
1<?php2$base→ 5 = 5; //@base=2, 103$loadCount→ 0 = 0;4include_once __DIR__ . "/counter_setup.inc";5include_once __DIR__ . "/counter_setup.inc";6$total→ 5 = $base5 + $loadCount0;78echo "loadCount=" . $loadCount0 . "\n";9echo "total=" . $total5 . "\n";outputloadCount=0 total=5
$base ← 2, $loadCount ← 0, $total ← 2
1<?php2$base→ 2 = 2;3$loadCount→ 0 = 0;4include_once __DIR__ . "/counter_setup.inc";5include_once __DIR__ . "/counter_setup.inc";6$total→ 2 = $base2 + $loadCount0;78echo "loadCount=" . $loadCount0 . "\n";9echo "total=" . $total2 . "\n";outputloadCount=0 total=2
$base ← 10, $loadCount ← 0, $total ← 10
1<?php2$base→ 10 = 10;3$loadCount→ 0 = 0;4include_once __DIR__ . "/counter_setup.inc";5include_once __DIR__ . "/counter_setup.inc";6$total→ 10 = $base10 + $loadCount0;78echo "loadCount=" . $loadCount0 . "\n";9echo "total=" . $total10 . "\n";outputloadCount=0 total=10