如何在变量中执行和获取.php文件的内容?
问题描述:
我想在其他页面的变量中获取.php文件的内容.
I want to get contents of a .php file in a variable on other page.
我有两个文件,myfile1.php
和myfile2.php
.
myfile2.php
<?PHP
$myvar="prashant"; //
echo $myvar;
?>
现在,我想在myfile1.php中的变量中获取myfile2.php的回显值,我尝试了以下方法,但其所有内容也包括php标记().
Now I want to get the value echoed by the myfile2.php in an variable in myfile1.php, I have tried the follwing way, but its taking all the contents including php tag () also.
<?PHP
$root_var .= file_get_contents($_SERVER['DOCUMENT_ROOT']."/myfile2.php", true);
?>
请告诉我如何将一个PHP文件返回的内容转换为另一个PHP文件中定义的变量.
Please tell me how I can get contents returned by one PHP file into a variable defined in another PHP file.
谢谢
答
您可以使用 include 指令来做到这一点.
You can use the include directive to do this.
文件2:
<?php
$myvar="prashant";
?>
文件1:
<?php
include('myfile2.php');
echo $myvar;
?>