PHP pass variable to include

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

    Option 3 is impossible - you'd get the rendered output of the .php file, exactly as you would if you hit that url in your browser. If you got raw PHP code instead (as you'd like), then ALL of your site's source code would be exposed, which is generally not a good thing.

    Option 2 doesn't make much sense - you'd be hiding the variable in a function, and be subject to PHP's variable scope. You'ld also have to have $var = passvariable() somewhere to get that 'inside' variable to the 'outside', and you're back to square one.

    option 1 is the most practical. include() will basically slurp in the specified file and execute it right there, as if the code in the file was literally part of the parent page. It does look like a global variable, which most people here frown on, but by PHP's parsing semantics, these two are identical:

    $x = 'foo';
    include('bar.php');
    

    and

    $x = 'foo';
    // contents of bar.php pasted here
    
    0 讨论(0)
  • 2020-11-27 17:53

    Considering that an include statment in php at the most basic level takes the code from a file and pastes it into where you called it and the fact that the manual on include states the following:

    When a file is included, the code it contains inherits the variable scope of the line on which the include occurs. Any variables available at that line in the calling file will be available within the called file, from that point forward.

    These things make me think that there is a diffrent problem alltogether. Also Option number 3 will never work because you're not redirecting to second.php you're just including it and option number 2 is just a weird work around. The most basic example of the include statment in php is:

    vars.php
    <?php
    
    $color = 'green';
    $fruit = 'apple';
    
    ?>
    
    test.php
    <?php
    
    echo "A $color $fruit"; // A
    
    include 'vars.php';
    
    echo "A $color $fruit"; // A green apple
    
    ?>
    

    Considering that option number one is the closest to this example (even though more complicated then it should be) and it's not working, its making me think that you made a mistake in the include statement (the wrong path relative to the root or a similar issue).

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

    Do this:

    $checksum = "my value"; 
    header("Location: recordupdated.php?checksum=$checksum");
    
    0 讨论(0)
  • 2020-11-27 18:02

    In regards to the OP's question, specifically "The variable needs to be set and evaluated from the calling first file (it's actually '$_SERVER['PHP_SELF']', and needs to return the path of that file, not the included second.php)."

    This will tell you what file included the file. Place this in the included file.

    $includer = debug_backtrace();
    echo $includer[0]['file'];
    
    0 讨论(0)
  • 2020-11-27 18:05

    This worked for me: To wrap the contents of the second file into a function, as follows:

    firstFile.php

    <?php
        include("secondFile.php");
    
        echoFunction("message");
    

    secondFile.php

    <?php
        function echoFunction($variable)
        {
            echo $variable;
        }
    
    0 讨论(0)
  • 2020-11-27 18:07

    You can execute all in "second.php" adding variable with jQuery

    <div id="first"></div>
    
    <script>
    $("#first").load("second.php?a=<?php echo $var?>")
    </scrpt>
    
    0 讨论(0)
提交回复
热议问题