duanhunlou7051 2018-02-19 00:52
浏览 129
已采纳

如何在包含文件中使用父文件中声明的php变量

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>
  • 写回答

2条回答 默认 最新

  • dongzhan5246 2018-02-19 01:38
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵