Include/Require 'file.php' or Include/Require('file.php')

Asked

Viewed 468 times

10

I know that PHP allows the inclusion of files in both modes, but I would like to know the correct way to use these functions, or if the two modes are correct.

  • 4

    Just to complement have include, require, include_once, require_once. All behave as @bfavaretto mentioned.

3 answers

11


include and require sane statements, not functions. Therefore, parentheses are not necessary. When you include them, the whole part of the parentheses is considered an expression, and that expression is what is combined with the statement.

Let’s say it’s a syntax idiosyncrasy. The cleanest way to use it is without parentheses, but using parentheses is not exactly "wrong".

5

According to the PHP manual, parentheses are not needed around your argument, but be careful when comparing the return value. As the cited example:


// 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';
}
  • Good observation.

3

require and require_once produce an E_COMPILE_ERROR when the file is not found or cannot be accessed. E_COMPILE_ERROR for script execution.

include and include_once produce an E_WARNING when the file is not found or cannot be accessed. E_WARNING not for the script, depending on the settings, is or is not displayed an error message next to HTML.

Let’s assume you have a script that generates a PDF report and makes the download available. It is crucial to generate the report, but sending by email to the manager is optional. So you need to generate the PDF but if you do not send by email you do not need to prevent the user from seeing the PDF.

Let’s assume there are 4 files:

  • php report.
  • php settings.
  • lib/pdf.php
  • lib/smtp.php

php report.: The file accessed by the user and that filters and processes the data and displays the report on the screen and sends it by email.

php settings.: File containing variable definitions such as PDF dimensions, SMTP server authentication, manager email, Session and error_reporting directives, 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.php report file needs the.php configurations file and lib/pdf.php and lib/smtp.php is used, but for our business logic it is not crucial.

Then we’d do the file php report. 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 ?

  • I think you misunderstood the question. It’s not about include/require versus their respective _Once, it’s about whether or not to use parentheses.

  • Yes, I seem to have misunderstood even, I understood that you wanted to know the difference between include and require and not whether to use with or without parentheses.

  • 1

    Yeah. But I’m not the one who asked, I’m the one who answered :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.