first.php:文件内容的变量值
<?php
$name ="somthing";
?>
second.php
$data = file_get_contents(first.php);
现在我想要得到$名字从$数据变量变量的值。怎么做?
first.php:文件内容的变量值
<?php
$name ="somthing";
?>
second.php
$data = file_get_contents(first.php);
现在我想要得到$名字从$数据变量变量的值。怎么做?
变化second.php到:
include 'first.php';
echo $name;
输出将是 “财产以后”
您应该使用包括,include_once,需要或require_once功能,的file_get_contents获取文件内容作为字符串而不解析它。
语法:
include 'first.php';
或
require 'first.php';
在PHP中,你可以插入一个PHP文件的内容到另一个PHP文件在服务器执行之前。
include和require语句用于在执行流程中插入写入其他文件的有用代码。
包括和要求是相同的,只是在失败:
要求会产生一个致命错误(E_COMPILE_ERROR)和停止脚本 包括只会产生一个警告(E_WARNING)和脚本将继续
如何让它工作?
first.php
<?php $name ="somthing"; ?>
second.php
<?php include 'first.php'; echo $name; ?>
将打印变量的内容$命名
你忘了问一个问题。 – 2013-05-14 09:35:32
这不是'file_get_contents()'的工作方式。使用'include()'而不是 – kennypu 2013-05-14 09:36:24
是的,你可以使用include或require函数,你可以从文件中获取变量。 – 2013-05-14 09:47:18