How use a php variable declared in the parent file in an included file

随声附和 提交于 2019-12-11 07:35:17

问题


I'm trying to pass the php variable $shareURL = "someURL"; from the parent page of test.php into the included file of commentTest.php. Is this possible? If yes, please help.

Parent File = Test.php

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
</head>
<body>
    <?php 
    $shareURL = "someURL";
    echo "$shareURL";
    include "http://domainName.net/assets/includes/commentTest.php"; 
    ?>
</body>
</html>

PHP Included File = commentTest.php

<?PHP 
echo "<div class='fb-comments' data-href='$shareURL' data-num-posts='5' data-width='100%'></div>";
?>

HTML Output Source

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
</head>
<body>
    someURL<div class='fb-comments' data-href='' data-num-posts='5' data-width='100%'></div></body>
</html>

回答1:


Change your Test.php to this:

include "/assets/includes/commentTest.php";




回答2:


Thanks everyone!

Your comments and answers helped me find the solution I needed.

$root = dirname(__FILE__);
include "$root/assets/includes/commentTest.php";

Apparently my root is here /var/www/html instead of right after the TLD in the URL.



来源:https://stackoverflow.com/questions/48858279/how-use-a-php-variable-declared-in-the-parent-file-in-an-included-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!