简体   繁体   English

这个PHP有什么问题? 什么都没有出现

[英]What's wrong with this PHP? Nothing is showing up

I'm trying to learn PHP, so I could very well be making a very beginner-level mistake. 我正在尝试学习PHP,所以我很容易犯一个非常初学者级的错误。

Basically, I have a page.php file that is a class file for the page I want to create, and I want to include that in my home.php page (I do that using the require function). 基本上,我有一个page.php文件,它是我要创建的页面的类文件,我想将其包含在我的home.php页面中(我使用require函数来做到这一点)。 However, nothing shows up. 但是,没有任何显示。 I'm doing this based off a book I'm reading, and it seems to work fine there. 我正在根据正在阅读的书进行此操作,并且在此看来效果很好。

page.php: page.php:

<?php

class Page {
    // attributes
    public $content;
    public $title = "TLA Consulting Pty Ltd";
    public $keywords = "TLA Consulting, Three Letter Abbreviation, some of my best friends are search engines";
    public $buttons = array("Home" => "home.php", "Contact" => "contact.php", "Services" => "services.php", "Site Map" => "map.php");

    public function __set($name, $value) {
        $this->$name = $value;
    }

    public function display() {
        echo "<html>\n<head>\n";
        $this->displayTitle();
        $this->displayKeywords();
        $this->displayStyle();
        echo "</head>\n<body>\n";
        $this->displayHeader();
        $this->displayMenu($this->buttons); // try this line without giving it the parameter
        echo $this->content;
        $this->displayFooter();
        echo "</body>\n</html>";
    }

    public function displayTitle() {
        echo "<title>$this->title</title>";
    }

    public function displayKeywords() {
        echo "<meta name=\"keywords\" content=\"$this->keywords\" />";
    }

    public function displayStyle() {
?>
        <style>
            h1 {
                color: white;
                font-size: 24px;
                text-align: center;
                font-family: helvetica, arial, sans-serif;
            }

            .menu {
                color: white;
                font-size: 12px;
                text-align: center;
                font-family: helvetica, arial, sans-serif;
                font-weight: bold;
            }

            td {
                background-color: black;
            }

            p {
                color: black;
                font-size: 12px;
                text-align: justify;
                font-family: helvetica, arial, sans-serif;
            }
        </style>
<?php
    }

    public function displayHeader() {
?>
        <table width="100%" cellpadding="12" cellspacing="0" border="0">
            <tr bgcolor="black">
                <td aling="left"><img src="http://i.imgur.com/xBrUZ.png" alt="Iron Man's finger" /></td>
                <td><h1>TLA Consulting Pty Ltd</h1></td>
            </tr>
        </table>
<?php
    }

    public function displayMenu($buttons) {
        echo "<table width=\"100%\" bgcolor=\"white\">\n";
        echo "<tr>\n";

        // calculate button size
        $width = 100 / count($buttons);

        while (list($name, $url) = each($buttons)) {
            $this -> displayButton($width, $name, $url, !this->IsURLCurrentPage($url));
        }
        echo "</tr>\n</table>\n";
    }

    public function IsURLCurrentPage($url) {
        if (!strpos($_SERVER['PHP_SELF'], $url)) {
            return false;
        }
        else {
            return true;
        }
    }

    public function displayButton($width, $name, $url, $active = true) {
        if ($active) {
            echo "<td width=\"".$width."%\">
            <a href=\"$url\"><img src=\"http://i.imgur.com/xBrUZ.png\" alt=\"$name\" /></a>
            <a href=\"$url\"><span class=\"menu\">$name</span></a>
            </td>";
        }
        else {
            echo "<td width=\"".$width."%\">
            <img src=\"http://i.imgur.com/xBrUZ.png\" />
            <span class=\"menu\">$name</span>
            </td>";
        }
    }

    public function displayFooter() {
        echo "<p>I am a footer ololz</p>";
    }
}

?>

home.php: home.php:

<?php

    require("page.php");

    $homepage = new Page();

    $homepage->content = "<p>Hi there, I like blueberries greatlly.</p>
                          <p>I hope you'll join me in loving blueberries!</p>"

    $homepage->display();

?>

Any insight would be appreciated. 任何见识将不胜感激。

Two problems: 两个问题:

  • this should be $this on line 86 of page.php . this应该是page.php第86行上的$this
  • You need a semicolon at the end of ...blueberries!</p>" 您需要在...blueberries!</p>"末尾使用分号...blueberries!</p>"

You might not be seeing the errors if error reporting isn't turned on. 如果未打开错误报告,则可能看不到错误。

you are using the beginning and ending of php inconsistently. 您不一致地使用了php的开头和结尾。

For example here: 例如这里:

    public function displayStyle() {
?>
    <style>
        h1 {
            color: white;
            font-size: 24px;
            text-align: center;
            font-family: helvetica, arial, sans-serif;
        }

        .menu {
            color: white;
            font-size: 12px;
            text-align: center;
            font-family: helvetica, arial, sans-serif;
            font-weight: bold;
        }

        td {
            background-color: black;
        }

        p {
            color: black;
            font-size: 12px;
            text-align: justify;
            font-family: helvetica, arial, sans-serif;
        }
    </style>
  <?php  
  }

You are starting a function, then you break it with closing the php block "?>" and paste a stylesheet part which will be echoed when the page loads. 您正在启动一个函数,然后通过关闭php块“?>”来破坏它,并粘贴样式表部分,该样式表部分将在页面加载时回显。 If you want to display the style with this function, then you have to make it like: 如果要使用此功能显示样式,则必须使其如下所示:

<?php
 public function displayStyle() {

 echo   "<style>".
        "h1 {".
            "color: white;";   
  }
 ?>

In the home.php file you're missing a semicolon in the ine 10 在home.php文件中,ine 10中缺少分号

In the page.php file you're missing character "$" on line 86 在page.php文件中,第86行缺少字符“ $”

$this -> displayButton($width, $name, $url, !$this->IsURLCurrentPage($url));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM