0

I have a simple Vars.php page:

<?php
//Vars.php
$WeekOfDateSelected = date('l, m/d/Y', strtotime($MonthYear));
$NextSundayOfDateSelected = date('l, m/d/Y', strtotime('this Sunday', strtotime($WeekOfDateSelected)));

?>

I have another PHP that includes Vars.php and builds a table:

<html>
<?php
//AnalyticsTest.php

include($_SERVER['DOCUMENT_ROOT']."/~/~/~/~/~/Vars.php");

function WeekTable() {

echo "<table id=\"a\">
<tr>
<th style=\"text-align: center;\"><a href=\"#\">< previous week</a></th>
<th colspan=\"4\" style=\"text-align: center;\"><h2>Week of ";
echo $WeekOfDateSelected;
echo " - "; 
echo $NextSundayOfDateSelected; 
echo "</th>
<th style=\"text-align: center;\"><a href=\"#\">next week ></a></th>
</tr>
</table>";

}

?>

</html>

Basically, when I call WeekTable() everything outputs correctly except for the PHP variables $WeekOfDateSelected and $NextSundayOfDateSelected, which output blank.

John Conde
  • 217,595
  • 99
  • 455
  • 496
bob
  • 235
  • 2
  • 3
  • 12

1 Answers1

6

You need to pass those variables as parameters to that function. Otherwise they are out of scope.

function WeekTable($WeekOfDateSelected,$NextSundayOfDateSelected) {
    // ...
}

Make sure you pass them when you call the function:

 WeekTable($WeekOfDateSelected,$NextSundayOfDateSelected);

You can also use the global keyword but that is a bad programming practice so I will not show that here.

John Conde
  • 217,595
  • 99
  • 455
  • 496
  • I'm getting a "Missing argument 1 for WeekTable()" and "Missing argument 2 for WeekTable()" error ... I'm in Wordpress as well (not sure if that impacts anything) – bob Aug 07 '15 at 18:53
  • You don't have the variables in your function call. It should look like this: `WeekTable($WeekOfDateSelected,$NextSundayOfDateSelected);` – John Conde Aug 07 '15 at 18:59