PHP将变量发送到file_get_contents() [英] PHP sending variables to file_get_contents()

查看:104
本文介绍了PHP将变量发送到file_get_contents()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够通过file_get_contents()将一些变量发送到文件.

I want to be able to send a few variables to a file through file_get_contents().

这是firstfile.php:

This is firstfile.php:

<?php
$myvar = 'This is a variable';
// need to send $myvar into secondfile.php
$mystr = file_get_contents('secondfile.php');
?>

这是secondfile.php:

This is secondfile.php:

The value of myvar is: <?php echo $myvar; ?>

我希望变量$mystr等于'The value of myvar is: This is a variable'

还有其他功能可以让您在PHP中执行此操作吗?

Is there any other function that will let you do this in PHP?

推荐答案

获取文件 的内容与运行脚本 :

There is a big difference between getting the contents of a file and running a script:

  • include —此PHP指令将指定的文件作为脚本运行,并且作用域与进行include调用的作用域相同.因此,要将变量传递"给它,您只需要在调用include之前定义它们. include仅可在本地使用(只能包含与PHP服务器在同一文件系统上的文件).

  • include — this PHP directive runs the specified file as a script, and the scope is the same as the scope where the include call is made. Therefore, to "pass" variables to it, you simply need to define them before calling include. include can only be used locally (can only include files on the same file system as the PHP server).

file_get_contents 本地获取文件时,这只是检索文件中包含的文本.没有完成PHP处理,因此无法传递"变量.如果您在上面的示例中检查$myvar,您将看到它包含确切的字符串"<?php echo $myvar; ?>"—.它尚未执行.

file_get_contents — When getting a file locally, this simply retrieves the text that is contained in the file. No PHP processing is done, so there is no way to "pass" variables. If you inspect $myvar in your example above, you will see that it contains the exact string "<?php echo $myvar; ?>" — it has not been executed.

但是,PHP通过允许file_get_contents提取远程"文件的内容,使某些事情有些混乱.互联网地址.在这种情况下,概念是相同的— PHP只会提取该地址中包含的所有内容的原始结果—.但是PHP,Java,Ruby或远程服务器上正在运行的任何其他程序可能已执行以产生该结果.

However, PHP has confused some things a little by allowing file_get_contents to pull in the contents of a "remote" file — an internet address. In this case, the concept is the same — PHP just pulls in the raw result of whatever is contained at that address — but PHP, Java, Ruby, or whatever else is running on that remote server may have executed something to produce that result.

在这种情况下,您可以根据URL的规范(如果是API或类似的东西)在URL中传递"变量(称为GET请求参数).无法传递您选择的未指定要在该远程服务器处理的脚本中处理的变量.

In that case, you can "pass" variables in the URL (referred to as GET request parameters) according to the specifications of the URL (if it is an API, or something similar). There is no way to pass variables of your choosing that have not been specified to be handled in the script that will be processed by that remote server.

注意:尽管要小心,但称​​ MAY 的远程服务器"是您自己的服务器,因为如果您真的不知道它的全部工作原理,这可能会使您更加困惑(它将成为第二个完全独立的请求).尽管他们可以完成 like 结果,但通常没有充分的理由而不是使用include.

Note: The "remote server" referred to MAY be your own server, though be careful, because that can confuse things even more if you don't really know how it all works (it becomes a second, fully separate request). There is usually not a good reason to do this instead of using include, even though they can accomplish similar results.

这篇关于PHP将变量发送到file_get_contents()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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