PHP使用未定义的常量

时间:2015-03-28 13:51:54

标签: php

所以我得到了一堆未定义的常量错误,我不知道为什么。我在Windows WAMP服务器上运行,如果这有所不同。我在Dreamweaver CS6中编写代码并且没有显示任何错误。这是代码:

<!DOCTYPE html>
<?php
    $services = Array(
                "website" => array (
                    title => "Web Site Design",
                    price => "Vaires Contact us for a Free Quote",
                    blurb => "We make good websites"
                ),
                "nas" => array (
                    title => "NAS Storage",
                    price => "Vaires Contact us for a Free Quote",
                    blurb =>" We make make good servers"
                ),
                "localserver" => array (
                    title => "Local Sever Setup",
                    price => "Vaires Contact us for a Free Quote",
                    blurb => "We make make good servers"
                ),

);
?>
<html>
<head>
<meta charset="utf-8">
<?php include 'includes/header.php'?>
<title>Anise Technologys | Services</title>
</head>
<body>
<div class="wrapper">
<?php include 'includes/nav.php'?>
<div class="content">
  <h1 id="title-center">Services</h1>
  As a business technology solution we offer a wide range of solutions to fit your business's needs
  <div class="list">
    <?php foreach ($services as $key => $item) {?>
    <div class="list-left"><?php echo $item[title]; ?></div>
    <div class="list-mid"><?php echo $item[blurb]; ?></div>
    <div class="list-right"><a href="http://localhost/latech/service?item=<?php echo $key; ?>">More</a></div>
    <hr>
    <?php } ?>
  </div>
</div>
</div>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

数组的键值是字符串,应该这样引用

$services = Array(
            "website" => array (
                'title' => "Web Site Design",
                'price' => "Vaires Contact us for a Free Quote",
                'blurb' => "We make good websites"
            ),
            "nas" => array (
                'title' => "NAS Storage",
                'price' => "Vaires Contact us for a Free Quote",
                'blurb' =>" We make make good servers"
            ),
            "localserver" => array (
                'title' => "Local Sever Setup",
                'price' => "Vaires Contact us for a Free Quote",
                'blurb' => "We make make good servers"
            ),
);

PHP不会将未加引号的字符串值视为常量,它将检查是否存在具有该名称的常量,并替换其值(如果存在)。

如果不存在该名称的常量,那么它将(慷慨地)假设您打算使用带引号的字符串,并将其视为这样;但它会发出通知让你知道你应该修理它。

请注意,在检查常量列表和发出该通知时都存在性能开销,因此修复它是有益的

另请注意,当您在代码中引用该数组时,同样适用

<div class="list-left"><?php echo $item[title]; ?></div>
<div class="list-mid"><?php echo $item[blurb]; ?></div>

应该是

<div class="list-left"><?php echo $item['title']; ?></div>
<div class="list-mid"><?php echo $item['blurb']; ?></div>