Variable passing with require() and include()

Something new… variables are passed to require() and include() from the main page.  So based on the variable you set in the main page, you can return different pieces of code, menu items, etc.

In the main page you set the variable, call the file, and display the output.

<?php
$x=2;
require(“menu.php”);
echo $message;
?>

In the called file, evaluate the variable, set the output variable and return it to the main page.

<?php
if ($x==1)
$message=”Have a nice weekend!”;
else
$message=”Have a \”nice\” day!”;
return $message;
?>

Note that nested quotes need a \ preceeding them.  The code above would put… Have a “nice” day! … in the page.

The switch() function would probably make more sense if more than two or three values are expected.

Leave a Reply