Trying to load my first php site in a browser; Firefox displays odd coding tags, IE asks if I want to "open" the file, instead of loading the page. I think I copied a tutorial 100%, so I suppose it really should work.. Is there anything else I need to do to make this work, or may I have some code error?
functions.php
<?php
function displayContent($content) {
echo implode($content);
}
function displaySidebar($sidebar) {
echo implode($sidebar);
}
?>
template.php
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>index</title>
<meta name="author" content="SS" />
<link href="site.css" rel="stylesheet"type="text/css" />
</head>
<body>
<div class="container">
<div class="header">
header
</div>
<div class="sidebar">
<?php displaySidebar($sidebar); ?>
</div>
<div class="content">
<?php displayContent($content); ?>
</div>
<div class="footer">
footer
</div>
</div>
</body>
index.php
<?php
//include our functions
require_once ('functions.php');
// declare arrays
$content=array();
$sidebar=array();
//handle page request
$t=$_GET['t'];
if($t == 'about') {
//display about page //can we load a full template?
$content[]="<h2>about</h2>";
$content[]="<p>Content</p>";
$sidebar[]="";
} else {
//display the home page
$content[]="<h2>home page</h2>";
$content[]="<p>Content</p>";
$sidebar[]="";
}
//Important this is on bottom
//I guess content must be ready on load.
include ('template.php');
?>
OUTPUT: (Firefox)
about"; $content[]="
Content
"; $sidebar[]=""; } else { //display the home page $content[]="
home page
"; $content[]="
Content
"; $sidebar[]=""; } //Important this is on bottom //I guess content must be ready on load. include ('template.php'); ?>
Uh, have you already installed Apache and PHP, and have you put the files in Apache's web root? Because PHP is not HTML: it's not parsed by the browser. It's executed by the server, which then returns the HTML response to the browser.
It could be that your server doesn't have PHP set up, or maybe your files aren't ending in .php.
To check, I'd remove what you've done so far, and make a new file called index.php
. In it, I'd just write:
<?php
echo "Hi!";
?>
Then go to the page. If that works, then you know your script has some issue, if not, then you know the server isn't set up correctly.
If that does work, you may want to change echo "Hi!";
to say phpinfo();
. That tells you all about the PHP install, which may come in handy to know at some point!