我的第一个php不工作


My first php not working

尝试在浏览器中加载我的第一个php站点;Firefox显示奇怪的编码标签,IE询问我是否要"打开"文件,而不是加载页面。我认为我100%复制了教程,所以我想它真的应该有效。我还需要做些什么来完成这项工作,或者我可能有一些代码错误?

显然也

<?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');
?>

输出:(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'); ?>

嗯,你已经安装了Apache和PHP,你把文件放在Apache的web根目录下了吗?因为PHP不是HTML:它不会被浏览器解析。它由服务器执行,然后向浏览器返回HTML响应。

如果您在本地工作,请尝试安装wamp或xampp(如果您在主机上工作)。我建议找一个支持php和mysql的

可能是您的服务器没有设置PHP,或者您的文件没有以。PHP结尾。

为了检查,我将删除到目前为止所做的,并创建一个名为index.php的新文件。在里面,我只写:

<?php
    echo "Hi!";
?>

然后进入页面。如果正常工作,那么你知道你的脚本有一些问题,如果没有,那么你知道服务器没有正确设置。

如果这确实有效,您可能想要将echo "Hi!";更改为phpinfo();。这将告诉您有关PHP安装的所有信息,这些信息在某些时候可能会派上用场!