- A blog by SPETS1337 -

» What's the difference?

Not that much, actually.. The only difference is how require and include handle errors in different ways. While include will only display an error message, require will stop from executing the script any further which can be both a technical, and visual problem when executing your page.

Visual example:

» When do you use require and when do you use include?

If your page cannot operate correctly (or won't look good) without a certain file, use require("file.php");. If your page can continue without the file, use include("file.php");.

REQUIRE situation


# Main PHP content
echo "Welcome " . $authenticatedUser . "! Glad to have you back.";
In this case we use require because we need to get the content of $authenticatedUser from securityFeature.php to properly display the page.

INCLUDE situation
include("coolGimmick.php"); # Adds some funky, unnecessary music!

# Main PHP content
echo "Welcome to our site! Glad to have you back.";
In this case we use include because the page can still display properly without being disrupted because coolGimmick.php is not working. The reason why unnecessary is highlighted in the example above is because the file can still operate without coolGimmick.php!

204 0 0