开发者

Is there a way to only pass a certain variable and it's value with the include function?

I want to include a page that contains a variable with a value that I need to insert in a database, but whe开发者_高级运维n I include the page, functions out of the scope of the second page try to run, which leads to an undefined error.

Basically I want this:

mainpage.php

    <?php
    $variable = 'value';
    function();
    ?>

secondpage.php

    <?php
    include 'mainpage.php';

    echo $variable;
    ?>

But I cant do this without also calling the function.


PHP executes statements line-by-line, so you cannot control what it does unless your mainpage.php has some conditional statements that you can influence from within secondpage.php.


Inside function type,

global $variable;

So your page will look like this:

$variable = 'value';
function();
echo $variable;

So if you want to function() to access $variable, you need to put global $variable inside function, echo $variable should echo "value".

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜