Challenge 3 — Solution Task: Write out (as comments describing a 3-file structure) why using require_once instead of require would prevent a "cannot redeclare function" fatal error if both header.php and sidebar.php each try to require the same functions.php file from within index.php. // ---- header.php ---- // Store"; // ?> // ---- sidebar.php ---- // Cart total: " . formatPrice(24.5) . ""; // ?> // ---- index.php ---- // // Why require_once (not plain require) matters here: // Both header.php and sidebar.php independently try to load // functions.php. If each one used a plain "require 'functions.php';", // the function formatPrice() would be defined once when header.php // runs, and PHP would then try to define it AGAIN when sidebar.php // runs its own require - triggering a fatal // "Cannot redeclare formatPrice()" error, since PHP does not allow // the same function name to be defined twice. // // Because both files use require_once instead, PHP remembers that // functions.php was already loaded the first time (via header.php) // and simply skips loading it again the second time (via // sidebar.php) - formatPrice() ends up defined exactly once, and // both header.php and sidebar.php can still safely call it. // A working single-file demonstration of the underlying idea: function formatPrice($amount) { return "£" . number_format($amount, 2); } echo formatPrice(24.5); ?> Output: £24.50 Notes: - The failure mode being avoided here is specific to files that DEFINE something (functions, classes) rather than files that only run top-level code - a file that just echoes HTML can safely be require'd twice with no error, but a file that defines a function cannot. - require_once and include_once both track, per script run, exactly which files have already been loaded, and silently skip any repeat request for a file already on that list - regardless of which including file made the earlier request. - This is exactly why a shared function/class library that might be pulled in from multiple different included files (as header.php and sidebar.php both do here) should almost always use require_once rather than a plain require.