PHP pass variable to include

前端 未结 13 2844
你的背包
你的背包 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:42

    You can use the extract() function
    Drupal use it, in its theme() function.

    Here it is a render function with a $variables argument.

    function includeWithVariables($filePath, $variables = array(), $print = true)
    {
        $output = NULL;
        if(file_exists($filePath)){
            // Extract the variables to a local namespace
            extract($variables);
    
            // Start output buffering
            ob_start();
    
            // Include the template file
            include $filePath;
    
            // End buffering and return its contents
            $output = ob_get_clean();
        }
        if ($print) {
            print $output;
        }
        return $output;
    
    }
    


    ./index.php :

    includeWithVariables('header.php', array('title' => 'Header Title'));
    

    ./header.php :

    <h1><?php echo $title; ?></h1>
    
    0 讨论(0)
  • 2020-11-27 17:44

    OPTION 1 worked for me, in PHP 7, and for sure it does in PHP 5 too. And the global scope declaration is not necessary for the included file for variables access, the included - or "required" - files are part of the script, only be sure you make the "include" AFTER the variable declaration. Maybe you have some misconfiguration with variables global scope in your PHP.ini?

    Try in first file:

      <?php 
      $myvariable="from first file";
      include ("./mysecondfile.php"); // in same folder as first file LOLL
      ?>
    

    mysecondfile.php

      <?php 
      echo "this is my variable ". $myvariable;
      ?>
    

    It should work... if it doesn't just try to reinstall PHP.

    0 讨论(0)
  • 2020-11-27 17:47

    I have the same problem here, you may use the $GLOBALS array.

    $GLOBALS["variable"] = "123";
    include ("my.php");
    

    It should also run doing this:

    $myvar = "123";
    include ("my.php");
    
    ....
    
    echo $GLOBALS["myvar"];
    

    Have a nice day.

    0 讨论(0)
  • 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.
    
    0 讨论(0)
  • 2020-11-27 17:49

    I've run into this issue where I had a file that sets variables based on the GET parameters. And that file could not updated because it worked correctly on another part of a large content management system. Yet I wanted to run that code via an include file without the parameters actually being in the URL string. The simple solution is you can set the GET variables in first file as you would any other variable.

    Instead of:

    include "myfile.php?var=apple";
    

    It would be:

    $_GET['var'] = 'apple';
    include "myfile.php";
    
    0 讨论(0)
  • 2020-11-27 17:49

    I know this is an old question, but stumbled upon it now and saw nobody mentioned this. so writing it.

    The Option one if tweaked like this, it should also work.

    The Original

    Option One

    In the first file:

    global $variable; 
    $variable = "apple"; 
    include('second.php'); 
    

    In the second file:

    echo $variable;

    TWEAK

    In the first file:

    $variable = "apple"; 
    include('second.php');
    

    In the second file:

    global $variable;
    echo $variable;
    
    0 讨论(0)
提交回复
热议问题