PHP pass variable to include

前端 未结 13 2867
你的背包
你的背包 2020-11-27 17:19

I\'m trying to pass a variable into an include file. My host changed PHP version and now whatever solution I try doesn\'t work.

I think I\'ve tried every op

13条回答
  •  日久生厌
    2020-11-27 17:47

    According to php docs (see $_SERVER) $_SERVER['PHP_SELF'] is the "filename of the currently executing script".

    The INCLUDE statement "includes and evaluates the specified" file and "the code it contains inherits the variable scope of the line on which the include occurs" (see INCLUDE).

    I believe $_SERVER['PHP_SELF'] will return the filename of the 1st file, even when used by code in the 'second.php'.

    I tested this with the following code and it works as expected ($phpSelf is the name of the first file).

    // In the first.php file
    // get the value of $_SERVER['PHP_SELF'] for the 1st file
    $phpSelf = $_SERVER['PHP_SELF'];
    
    // include the second file
    // This slurps in the contents of second.php
    include_once('second.php');
    
    // execute $phpSelf = $_SERVER['PHP_SELF']; in the secod.php file
    // echo the value of $_SERVER['PHP_SELF'] of fist file
    
    echo $phpSelf;  // This echos the name of the First.php file.
    

提交回复
热议问题