我该如何解决这个问题:使用未定义的常量 slug - 假设 'slug'

How do I fix this: Use of undefined constant slug - assumed 'slug'

我把我的 HTML 网站变成了 PHP 网站。我目前处于测试阶段,但是,我一直在呈现的页面上显示 "Notices" 而不是内容。

我试过用 $ 将常量转换为变量,但通知仍然出现。但是,它从未定义的常量变为未定义的变量。我把它们改回常量。

我使用的是 WinXP 32 位,因此,我将 Xammp 1.8.2 与 PHP 5.4.31

一起使用

有问题的数组:

    $navItems = array(
                      array(
                              slug => "index.php",
                              title => "Home"
                            ),
                      array(
                              slug => "about.php",
                              title => "About Me"
                            ),
                      array(
                              slug => "portfolio.php",
                              title => "Portfolio"
                            ),
                      array(
                              slug => "contact.php",
                              title => "Contact"
                            )
        );

我期待看到实际的页面内容。但是,我得到:

注意:使用未定义的常量 slug - 假设 'slug' 在 C:\xampp\htdocs\zmglobal-it.com.php\includes\arrays.php 第 5

注意:使用未定义的常量标题 - 在 C:\xampp\htdocs\zmglobal-it.com.php\includes\arrays.php 第 6

行假设 'title'

键值必须用引号引起来,所以使用 "keyname" 或 'keyname'

''' { $navItems = array( array( "slug" => "index.php", "title" => "Home", ), array( "slug" => "about.php", "title" => "About Me", ), array( "slug" => "portfolio.php", "title" => "Portfolio", ), array( "slug" => "contact.php", "title" => "Contact", ) ); } '''

你的数组看起来不错,你可能只是想稍微修改一下,并在一些需要的地方添加 "',例如:

$navItems = array(
    array(
        "slug" => "index.php",
        "title" => "Home",
    ),
    array(
        "slug" => "about.php",
        "title" => "About Me",
    ),
    array(
        "slug" => "portfolio.php",
        "title" => "Portfolio",
    ),
    array(
        "slug" => "contact.php",
        "title" => "Contact",
    ),
);

或者也许:

$navItems = [
    [
        "slug" => "index.php",
        "title" => "Home",
    ],
    [
        "slug" => "about.php",
        "title" => "About Me",
    ],
    [
        "slug" => "portfolio.php",
        "title" => "Portfolio",
    ],
    [
        "slug" => "contact.php",
        "title" => "Contact",
    ],
];

var_dump($navItems);

输出

这是var_dump($navItems);的输出:

array(4) {
  [0]=>
  array(2) {
    ["slug"]=>
    string(9) "index.php"
    ["title"]=>
    string(4) "Home"
  }
  [1]=>
  array(2) {
    ["slug"]=>
    string(9) "about.php"
    ["title"]=>
    string(8) "About Me"
  }
  [2]=>
  array(2) {
    ["slug"]=>
    string(13) "portfolio.php"
    ["title"]=>
    string(9) "Portfolio"
  }
  [3]=>
  array(2) {
    ["slug"]=>
    string(11) "contact.php"
    ["title"]=>
    string(7) "Contact"
  }
}

Reference 1

What does the PHP error message “Notice: Use of undefined constant” mean?