PHP include not working with isset

Hi All,
I am trying to learn PHP and am going through David Powers’ PHP Solutions. I am getting the hang of things and finally beginning to understand after going through Lynda.com and a bunch of online resources. Now I am tackling this book and I am confused about why something doesn’t work. It is from pg 82-83 of his book and involves the use of an include to set the page title. All the other includes from this chapter work fine and so does this one - but only if a suggested isset statement is used. What I am having trouble with is why this is needed to make it work. The include code says to change the title to the filename minus the extension and has a condition that says if the title pages is index set $title = home. Unless you use the isset statement (<?php if (isset($title)) {echo “—{$title}”;} ?> it will not work. Why is that? If there was a problem with the include it wouldn’t work at all, yet when the isset statement isn’t there, it doesn’t work. Any ideas?

Thanks ahead.

Can you show us the code where you are defining the value of $title? You made mention of $title = home, but that isn’t valid PHP, so I’d like to see exactly how you are setting the value of $title.

Here is the content of title.inc.php
<?php
$title = basename($SERVER[‘SCRIPT_FILENAME’], ‘.php’);
$title = str_replace('
', ’ ', $title);
if ($title == ‘index’) {
$title = ‘home’;
}
$title = ucwords($title);

Below is the webpage I am trying to display - it works correctly with the isset statement. Remove it and it doesn’t. Sorry it is kinda long for a forum.

<?php include(‘./includes/title.inc.php’); ?>
<!DOCTYPE HTML>
<html>
<head>
<meta charset=utf-8">
<title>Japan Journey
<?php if (isset($title)) {echo “—{$title}”;} ?>
</title>
<link href=“styles/journey.css” rel=“stylesheet” type=“text/css” media=“screen”>
</head>

<body>
<div id=“header”>
<h1>Japan Journey</h1>
</div>
<div id=“wrapper”>
<?php include(‘./includes/menu.inc.php’); ?>
<div id=“maincontent”>
<h2>A Journey through Japan with PHP</h2>
<p>One of the benefits of using PHP is that you can extract common page elements such as the menu and footer, and turn them into server-side includes. Using PHP functions and conditional logic, you can automatically change the page title and indicate the current page in the menu. Generating a random image on the front page also adds interest to the site.</p>
<div id=“pictureWrapper”><img src=“images/basin.jpg” alt=“Water basin at Ryoanji temple” width=“350” height=“237” class=“picBorder”></div>
<p>Ut enim ad minim veniam, quis nostrud exercitation consectetur adipisicing elit. Velit esse cillum dolore ullamco laboris nisi in reprehenderit in voluptate. Mollit anim id est laborum. Sunt in culpa duis aute irure dolor excepteur sint occaecat.</p>
<p>Eu fugiat nulla pariatur. Ut labore et dolore magna aliqua. Cupidatat non proident, quis nostrud exercitation ut enim ad minim veniam.</p>
<p>Quis nostrud exercitation eu fugiat nulla pariatur. Ut labore et dolore magna aliqua. Sed do eiusmod tempor incididunt velit esse cillum dolore ullamco laboris nisi.</p>
<p>Sed do eiusmod tempor incididunt ullamco laboris nisi consectetur adipisicing elit. Ut aliquip ex ea commodo consequat. Qui officia deserunt ut labore et dolore magna aliqua. Velit esse cillum dolore eu fugiat nulla pariatur. Sed do eiusmod tempor incididunt cupidatat non proident, sunt in culpa. Sunt in culpa duis aute irure dolor excepteur sint occaecat.</p>
<p>Quis nostrud exercitation eu fugiat nulla pariatur. Ut labore et dolore magna aliqua. Sunt in culpa duis aute irure dolor excepteur sint occaecat.</p>
</div>
<?php include(‘./includes/footer.inc.php’); ?>
</div>
</body>
</html>

Thanks ahead again for any help.

Jim

hmmm… I don’t see why the isset would be required with the code that you have. I don’t get any errors or warnings when I ran it locally

<?php
$title = basename($_SERVER['SCRIPT_FILENAME'], '.php');
$title = str_replace('_', ' ', $title);
if ($title == 'index') {
	$title = 'home';
}
$title = ucwords($title);
?>

Now if your code was

<?php
$page = basename($_SERVER['SCRIPT_FILENAME'], '.php');
$page = str_replace('_', ' ', $page);
if ($page == 'index') {
	$title = 'home';
}
//$title = ucwords($title);
?>

Then you would need isset because $title isn’t defined unless you are on the index page and so the variable would not be set in all situations.

Thanks for the cool reply. When I comment out the isset on my server using the code that I showed you it doesn’t work.
I am going to try this on a different server and see if it makes a difference (I am not experienced enough to know how much difference this will make, but I might learn something.
I like the second set of code and understand why isset would be needed. Thanks for that, it helps.

When you commented it out, did you leave it as

<?php echo "- $title"; ?>

Yes. The dash works fine…

The code you supplied is missing a double-quote:



<!--  <meta charset=utf-8"> -->
<meta charset="utf-8">

Please explain what you mean when you say “…something doesn’t work.”

Also please show the code that is working and also exactly how you commented out the isset statement.

Also please include your code in block php tags (without the space which follows the leading square bracket):

[ php]
your code
[ /php]