Avatar ·

What is the difference between include, require, include_once and require_once in PHP?

Hello everyone! I just started learning PHP and I can't understand the difference between these four constructs for including files. They all seem to do the same thing — insert code from another file.

\n\n

I have a file config.php with database settings:

\n\n
<?php\n$host = 'localhost';\n$db = 'mydb';\n$user = 'root';\n$pass = '';\n?>
\n\n

And I include it in index.php in different ways:

\n\n
<?php\ninclude 'config.php';\n// or\nrequire 'config.php';\n// or\ninclude_once 'config.php';\n// or\nrequire_once 'config.php';\n?>
\n\n

All four options work, and I can see the variables from config.php. So what's the difference? When should I use which?

\n\n

Also, I noticed something strange: if I twice