Skip to content
PHP Logo

In what situations should PHP developers use require_once instead of include for file inclusions in their code?

When developers need to include a file in their PHP code, they should use require_once instead of include if the file being included is essential for the script to run correctly. Using require_once will ensure that if the file cannot be included for any reason, such as if it doesn't exist or contains errors, a fatal error will be triggered, halting the script execution. This can help prevent issues with missing or faulty files causing unexpected behavior in the application.

php
<?php
require_once 'config.php';
require_once 'functions.php';

// Rest of the PHP code here
?>