用include标签传递变量php
I am attempting to pass a variable to a different php file, my code to get the variable is this,
<?php
global $changeDate;
$changeDate = $_POST['editdate'];
$results = "SELECT title, DATE(start) FROM rota WHERE DATE(start) = '$changeDate' ";
$workingUser = mysql_query($results);
include 'phpfunctions/holddate.php';
?>
holddate.php is this,
<?php
$holdingdate = 0;
$holdingdate = $changeDate;
?>
on execution I get an error saying $changeDate
is an undefined variable in holddate.php, where is this going wrong? This includes the global
setting recomended but still recieve same error.
You shouldnt have to use global attribute or sesions.
Try turning errors on and see if you get any errors returned.
error_reporting(E_ALL);
ini_set('display_errors', '1');
From the DOCS
When a file is included, the code it contains inherits the variable scope of the line on which the include occurs. Any variables available at that line in the calling file will be available within the called file, from that point forward. However, all functions and classes defined in the included file have the global scope.
I would try to stay away from creating global variables unless its absolutely necessary. Also since you already tried tht and it does not work there is another problem besides the variable scope.
Use this :
<?php
global $changeDate;
$changeDate = $_POST['editdate'];
$results = "SELECT title, DATE(start) FROM rota WHERE DATE(start) = '$changeDate' ";
$workingUser = mysql_query($results);
include 'phpfunctions/holddate.php';
?>