I know PHP allows the inclusion of files in both modes, but I would like to know the correct way to use these functions, or if both modes are correct.
I know PHP allows the inclusion of files in both modes, but I would like to know the correct way to use these functions, or if both modes are correct.
include
and require
are statements , not functions. Therefore, parentheses are not required. When you include them, the whole part of the parentheses is considered an expression, and this expression is combined with the statement .
So let's say it's a syntax idiosyncrasy. The cleanest way to use is without parentheses, but using parentheses is not exactly "wrong".
According to the PHP manual, parentheses are not required around your argument, but be careful when comparing the return value. As the example quoted:
// Não vai funcionar, verificando se existe // include(('vars.php') == 'OK'), Ex: include(''); if (include('vars.php') == 'OK') { echo 'OK'; } // Funciona! if ((include 'vars.php') == 'OK') { echo 'OK'; }
require and require_once produce an E_COMPILE_ERROR when the file is not found or can not be accessed. E_COMPILE_ERROR for script execution.
include and include_once produce an E_WARNING when the file is not found or can not be accessed. E_WARNING not for the script, depending on the settings, an error message is displayed next to the HTML.
Suppose you have a script that generates a PDF report and makes the download available. Reporting is crucial, but emailing to the manager is optional. So you need to generate the PDF but if you do not send it by email you do not have to prevent the user from viewing the PDF.
Let's suppose there are 4 files:
relatorio.php : The file accessed by the user and which performs the filtering and processing of the data and displays the report on the screen and sends it via email.
configuracoes.php : A file containing variable definitions such as PDF dimensions, SMTP server authentication, manager email, session directives and error_reporting, among other things. >
lib / pdf.php : Library that generates a PDF file.
lib / smtp.php : Library that sends emails using SMTP.
In this case, the file relatorio.php needs the file configuracoes.php and lib / pdf.php and lib / smtp.php is used, but for our business logic it is not crucial.
Then we would make the file relatorio.php as follows:
require_once "configuracoes.php";
require_once "lib/pdf.php";
include_once "lib/smtp.php";
/*
Código para gerar o relatório...
*/
$pdf = new PDF($relatorio,$configuracoesPDF); // $relatorio foi criada acima. $configuracoesPDF está no arquivo configuracoes.php. Classe PDF está no arquivo lib/pdf.php
if(function_exists("EnviarEmailComAnexo")) //Método EnviarEmailComAnexo está no arquivo lib/smtp.php
{
EnviarEmailComAnexo($emailDoGerente, "Relatório gerado", $mensagemPadrao, $pdf.GetStream());
}
echo $pdf.ToString(); // Método ToString no arquiov lib/pdf.php
Can you see the difference now?