如何在变量中执行和获取 .php 文件的内容?

2022-01-19 00:00:00 return file php file-get-contents

我想在其他页面的变量中获取 .php 文件的内容.

I want to get contents of a .php file in a variable on other page.

我有两个文件,myfile1.phpmyfile2.php.

I have two files, myfile1.php and myfile2.php.

myfile2.php

<?PHP
    $myvar="prashant"; // 
    echo $myvar;
?>

现在我想在 myfile1.php 的一个变量中获取 myfile2.php 回显的值,我尝试了以下方式,但它也获取了包括 php tag() 在内的所有内容.

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;

?>

相关文章