PHP包含()GET属性(包括file.php?q = 1) [英] PHP include() with GET attributes (include file.php?q=1)
问题描述
我想要 include()
一个位于我的服务器上的php文件,以及额外的GET属性。
但它不起作用:
I want to include()
a php file located on my server, with additional GET attributes.
But it won't work:
include('search.php?q=1');
它给出的错误:
The error it gives:
PHP Warning: include(): Failed opening './search.php?q=1' for inclusion
好像它试图打开一个字面上名为'search.php?q = 1'的文件,而不是打开'search.php'文件并发送GET属性。
Seems like it tries to open a file literally named 'search.php?q=1' instead of opening the 'search.php' file and sending it the GET attributes.
*请注意,如果我没有放置任何GET属性,它就会工作:
*Note that it does work if I don't put any GET attributes:
include('search.php');
推荐答案
你不想这样做: d必须做一个http请求才能传递GET参数。您以这种方式调用的PHP脚本将运行在单独的PHP进程中。
You don't want to do this: You'd have to do a http request to be able to pass GET parameters. A PHP script you call in this way will run in a separate PHP process.
最佳方式是在本地包含文件:
The optimal way is to include the file locally:
include('search.php');
并手动传递任何参数,例如
and to pass any parameters to it manually, like e.g.
$q = "1";
include('search.php'); // expects `$q` parameter
或者更干净地将所有内容放在 search.php
放入一个可以用参数调用的函数或类中:
or, more cleanly, putting whatever you have in search.php
into a function or class that you can call with a parameter:
include('search.php'); // defines function my_search($q)
my_search(1);
这篇关于PHP包含()GET属性(包括file.php?q = 1)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!