简体   繁体   English

在PHP中:我如何使用include()函数哪个参数是可变的?

[英]In PHP :how can I use include() Function which parameter is variable?

I need to use include Function with variable. 我需要使用include函数和变量。

but,when I try to do it I faced some errors . 但是,当我尝试这样做时,我遇到了一些错误。

Code : 代码:

$year=$_POST['year'];

$month=$_POST['month'];

$day=$_POST['day'];

include "Event.php?year=".$year."&month=".$month."&day=".$day;

so,can U help me ? 那么,你可以帮帮我吗? : ) :)

When you include files, you can't pass them any arguments. 包含文件时,不能传递任何参数。 However they DO inherit any variables in the current scope (global or method). 但是,它们继承当前范围(全局或方法)中的任何变量。

I'm guessing at what your code does, but I assume you can just do this: 我猜你的代码做了什么,但我想你可以这样做:

include "Event.php";

You dont need to use that kind of sintax. 你不需要使用那种sintax。 The $year, $month and $day variables will be perfectly visible and accessible on the Event.php file. $ year,$ month和$ day变量将在Event.php文件中完全可见并可访问。

To get along with what you want, check PHP manual on http://php.net/manual/en/function.include.php to see some examples. 为了与您的想法相处,请查看http://php.net/manual/en/function.include.php上的PHP手册以查看一些示例。

I'm not sure what you're trying to achieve, but I have a couple of guesses handy. 我不确定你想要达到的目标,但我有几个猜测方便。

1 - You are trying to redirect the user, with those parameters appended to the url, in which case you will probably need to utilise header : 1 - 您正在尝试重定向用户,并将这些参数附加到网址,在这种情况下,您可能需要使用header

header("Location: http://example.com/Event.php?year=$year&month=$month&day=$day");

2 - You are trying to capture the output of that page given the presence of certain GET parameters, in which case you can utilise file_get_contents : 2 - 如果存在某些GET参数,您试图捕获该页面的输出,在这种情况下,您可以使用file_get_contents

$output = file_get_contents("http://localhost/Event.php?year=$year&month=$month&day=$day");

Include the Event.php directly and access the $_POST or $_GET variables from there. 直接包含Event.php并从那里访问$ _POST或$ _GET变量。


include('Event.php');

// Event.php:
echo $_POST['year']; // works

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM