[英]How can use a loop variable in function as a parameter of same function in php
[英]In PHP :how can I use include() Function which parameter is variable?
我需要使用include函數和變量。
但是,當我嘗試這樣做時,我遇到了一些錯誤。
代碼:
$year=$_POST['year'];
$month=$_POST['month'];
$day=$_POST['day'];
include "Event.php?year=".$year."&month=".$month."&day=".$day;
那么,你可以幫幫我嗎? :)
包含文件時,不能傳遞任何參數。 但是,它們繼承當前范圍(全局或方法)中的任何變量。
我猜你的代碼做了什么,但我想你可以這樣做:
include "Event.php";
你不需要使用那種sintax。 $ year,$ month和$ day變量將在Event.php文件中完全可見並可訪問。
為了與您的想法相處,請查看http://php.net/manual/en/function.include.php上的PHP手冊以查看一些示例。
我不確定你想要達到的目標,但我有幾個猜測方便。
1 - 您正在嘗試重定向用戶,並將這些參數附加到網址,在這種情況下,您可能需要使用header
:
header("Location: http://example.com/Event.php?year=$year&month=$month&day=$day");
2 - 如果存在某些GET參數,您試圖捕獲該頁面的輸出,在這種情況下,您可以使用file_get_contents
:
$output = file_get_contents("http://localhost/Event.php?year=$year&month=$month&day=$day");
直接包含Event.php並從那里訪問$ _POST或$ _GET變量。
include('Event.php');
// Event.php:
echo $_POST['year']; // works
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.