简体   繁体   中英

PHP access variable from included file

have a file like so:

<?php
require_once "properties.php";
$GLOBALS["to_home"] = "../sfair";
echo tohome () . "index.php";
?>

And properties.php:

<?php
function tohome()
{
if(isset($GLOBALS['to_home']))
{
return $GLOBALS['to_home'] . "/";
}
else
{
return "';
}
}

Which should give me ../stair/index.php . But instead gives me index.php . How can I make the included file be able to access the variables of the including files? note: I found another answer ("unable to access global variable from included file" but it did not work.

Can't you send the $GLOBALS["to_home"] with the Function call? That's what functions are for...

echo tohome($GLOBALS["to_home"]) . "index.php";

Of course you will have to change the function contents, but I guess you will get the idea.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM