Functions in PHP ... How to use?

-1

For a website a php file has been created that contains the functions for calculating a person's Body Mass Index (IMC) in the file funcoes.php.

You want to use this function on the main page (index.php). What code do I need to insert the file funcoes.php into the index.php file?

(Include, import, use, add or echo?)

    
asked by anonymous 18.11.2017 / 18:50

2 answers

0

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. 
    
    ?> 
    
        
    18.11.2017 / 19:09
    0

    Behind the code of index.php you must include the file that contains the function using include or require.

    include 'nome_do_arquivo.php';
    

    or

    require 'nome_do_arquivo.php';
    

    Once this is done, you only have to call the function.

        
    18.11.2017 / 18:54