To import / join code in PHP, there are the functions:
require 'caminho/para/seu/arguivo.php'
require_once 'caminho/para/seu/arquivo.php'
include 'caminho/para/seu/arquivo.php'
include_once 'caminho/para/seu/arquivo.php'
All these functions serve to import code. The only difference between " include
" and " require
" is that they handle possible errors differently.
In a scenario where the file you want to import does not exist, for example:
In the include function, the code will continue to run, but an alert will appear for the error on your site.
In the require function, a fatal error will be generated and the execution of the code will stop.
The two variants of the functions, "require_once" and "include_once", mean that PHP will check if the file has already been added, and if so, it will not be included again.
Example:
<?php
require 'meuarquivo.php' // Inclui o arquivo
require_once 'meuarquivo.php' // Não inclui o arquivo de novo, pois ele já fui incluído acima.
?>