如何在变量中执行和获取 .php 文件的内容? [英] How to execute and get content of a .php file in a variable?

查看:26
本文介绍了如何在变量中执行和获取 .php 文件的内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在其他页面的变量中获取 .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;

?>

这篇关于如何在变量中执行和获取 .php 文件的内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆