Should require_once “some file.php” ; appear anywhere but the top of the file?

前端 未结 5 876
时光取名叫无心
时光取名叫无心 2021-01-05 23:32

Is the following example appropriate for PHP\'s require_once construct?

function foo( $param )
{
    require_once \"my_file.php\" ;
    //
    // do somethin         


        
5条回答
  •  Happy的楠姐
    2021-01-06 00:07

    When using require_once keep in mind that this is not some pre-processor directive. The require_once statements are executed when PHP runs the code and it only executes if the specific script has not already been included during the execution.

    For example:

    conf.php:

    
    

    myscript.php

     $maxAge) 
        return "1"; 
      else 
        return "0";
    }
    
     echo foo(30); // Echos 1
     echo foo(30); // Echos 0
    ?>
    

    The require_once is not executed on the second call to foo(..) since conf.php has already been included once.

提交回复
热议问题