我该如何解决:使用未定义的常量段-假定为“段”

时间:2019-05-26 18:01:47

标签: php undefined

我带了HTML网站,并将其变成了PHP网站。我目前处于测试阶段,但是在呈现的页面而不是内容上我一直收到“通知”。

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

我使用的是32位WinXP,因此,我将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-在第5行的C:\ xampp \ htdocs \ zmglobal-it.com.php \ includes \ arrays.php中假定为“ slug”

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

2 个答案:

答案 0 :(得分:1)

键值必须加引号,因此请使用“键名”或“键名”

''' { $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", ) ); } '''

答案 1 :(得分:1)

您的数组看起来不错,您可能只需要稍加修改,然后在所需的某些位置添加"',例如:

$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"
  }
}

enter image description here

Reference 1

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