First, the reason for the errors is the variables are not in scope, meaning inside the function they are undefined, if you want them to have meaning the you would have to do something like display($fname);.
Ah, here’s something directly from php.net that will help you out.
[php]
<?php
function foo()
{
global $color;
include 'vars.php';
echo "A $color $fruit";
}
/* vars.php is in the scope of foo() so *
* $fruit is NOT available outside of this *
* scope. $color is because we declared it *
* as global. */
foo(); // A green apple
echo "A $color $fruit"; // A green
?>
[/php]
Though in my opinion I would avoid using GLOBAL VARIABLES.
Second, using the include the way you are doing is wrong for the most part, well not wrong but a little bit reverse logic in my opinion and causing a lot of logical errors (or just plain errors).
This his how I utilize the include. I have a bunch of chores I want to get done, such as setting up a connection to my database and variables that I want to use on more than one page, etc… I think it better to show you:
I have a file called utilities.inc.php
[php]<?php
if ($_SERVER[“SERVER_NAME”] != ‘localhost’) {
if ($_SERVER[“HTTPS”] != “on”) { // Redirect to a secure website ( https )
header(“Location: https://” . $_SERVER[“HTTP_HOST”] . $_SERVER[“REQUEST_URI”]);
exit();
}
}
utilities.inc.php
include ‘functions.secret.token.php’;
// Autoload classes from “classes” directory:
function class_loader($class) {
require(‘lib/classes/’ . $class . ‘.php’);
}
spl_autoload_register(‘class_loader’);
header(‘Content-Type: text/html; charset=utf-8’);
// Start the session:
$seconds = 60;
$minutes = 60;
$hours = 24;
$days = 14;
session_set_cookie_params($seconds * $minutes * $hours * $days, “”);
session_start();
$db = Database::getInstance();
$pdo = $db->getConnection();
// Check for a user in the session:
//$user = (isset($_SESSION[‘user’])) ? $_SESSION[‘user’] : NULL;
$current_id = (isset($_SESSION[‘current_id’])) ? $_SESSION[‘current_id’] : 1;
function html_escape($raw_input) {
// important! don’t forget to specify ENT_QUOTES and the correct encoding
return htmlspecialchars($raw_input, ENT_QUOTES | ENT_HTML401, ‘UTF-8’);
}
[/php]
that is used practically for every page to I write. If I didn’t use the include then I would have to type that exact code over and over again. That would get very time consuming and not to mention boring.
Hope that helps.
A great practical way of doing getting experience is doing some like the following.
[php]<?php
require(‘lib/includes/utilities.inc.php’);
$pageTitle = ‘J.R. Pepp | Website Design & Development’;
include ‘lib/includes/header.inc.php’; // Top Portion of HTML5
?>
<h4 class="container span12 websiteName">Website Design & Development</h4>
<div class="container">
<figure class="span7 homePage">
<figcaption class="intro">J.R. Pepp, based in Livonia, Michigan is a small Web Design & Development company. John R. Pepp, Web Designer & Developer designs and develops responsive Web sites coded in HTML5/CSS3, JQuery, and PHP at reasonable prices. All Web sites will have lively color themes, images, videos and many other great enhancements giving the website a professional, modern, clean feel to it. The mobile phone and tablet market is growing in popularity, so don't be left behind with an outdated Website! Pepster's Place will make sure your website site is capable of being seen by the vast majority of media devices that are out there.</figcaption>
<img class="homePicture" src="lib/images/img-john-pepp.png" alt="John Pepp - Owner of J.R. Pepp">
</figure>
<div class="span3 amazonAd">
<script type='text/javascript'>
amzn_assoc_ad_type = 'banner';
amzn_assoc_tracking_id = 'jrpewedede-20';
amzn_assoc_marketplace = 'amazon';
amzn_assoc_region = 'US';
amzn_assoc_placement = 'assoc_banner_placement_default';
amzn_assoc_linkid = '7NTFUWIVA6FDG7V6';
amzn_assoc_campaigns = 'electronicsrot';
amzn_assoc_p = '12';
amzn_assoc_banner_type = 'rotating';
amzn_assoc_width = '300';
amzn_assoc_height = '250';
</script>
<script src='//z-na.amazon-adsystem.com/widgets/q?ServiceVersion=20070822&Operation=GetScript&ID=OneJS&WS=1'></script>
</div>
</div>
</section>
<?php
include 'lib/includes/footer.inc.php'; // Bottom Portion of HTML5
[/php]
Notice how I use the include(s) to write the top portion and bottom portion of HTML5 this way I don’t have to repeat myself for every page and I even have my header and navigation in the top portion of my include.
Hope this also helps.
P.S. Notice on the bottom I don’t close the <?php statement off, this is intentional. You don’t want to close off if there is not HTML going on below it, this is for security reasons.