2

I need to use include Function with variable.

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 ? : )

2
  • 2
    What are you trying to do with include? Commented May 20, 2010 at 21:09
  • I don't get it either. What exactly are you trying to accomplish here? Commented May 20, 2010 at 21:14

4 Answers 4

4

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";

Sign up to request clarification or add additional context in comments.

2 Comments

thanks dear Coronatus . but ,your Suggestion it doesn't work : ( the main code I wrote it , to make a calender . therefore ,I need to do this with include to refresh every add new event in specific day .
yes I also think that it should do the work. If it's not working, then that means something is wrong with your code. If you don't have any copyright problems or such, please post your relevant code, so that others can view & understand what is wrong with it.
3

You dont need to use that kind of sintax. The $year, $month and $day variables will be perfectly visible and accessible on the Event.php file.

To get along with what you want, check PHP manual on http://php.net/manual/en/function.include.php to see some examples.

Comments

1

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:

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:

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

3 Comments

yes , your First guesses is exactly what i want to do , but ,it does not work with me can U tell me how ?
@Nina - what exactly happens. You cannot redirect after sending any output to the browser, so no echo statements, nothing.
this is not exactly right. We can at least output any data to browser first & then use the header function, but with one condition - you must use the function "ob_start()" at the very second line of your page where you are outputting the data to browser. The first line will be the PHP start tag "<?php" & no use of any whitespace character other than the use of "enter" in the first line.
0

Include the Event.php directly and access the $_POST or $_GET variables from there.


include('Event.php');

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.