1

I took my HTML website and turned it into a PHP site. I'm in the testing phase currently however, I keep getting "Notices" on the rendered page instead of content.

I've tried turning the constants to variables with $, but the notice still appears. However, it changed from undefined constant to undefined variable. I changed them back to constants.

I'm on a WinXP 32-bit, therefore, I use Xammp 1.8.2 with PHP 5.4.31

the array in question:

    $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"
                            )
        );

I am expecting to see the actual page content. However, I get:

Notice: Use of undefined constant slug - assumed 'slug' in C:\xampp\htdocs\zmglobal-it.com.php\includes\arrays.php on line 5

Notice: Use of undefined constant title - assumed 'title' in C:\xampp\htdocs\zmglobal-it.com.php\includes\arrays.php on line 6

Dharman
  • 30,962
  • 25
  • 85
  • 135
  • 2
    Use strings for your keys, like `'slug' => "index.php"` and `'title' => "Home"` – Chris White May 26 '19 at 18:03
  • Possible duplicate of [What does the PHP error message "Notice: Use of undefined constant" mean?](https://stackoverflow.com/questions/2941169/what-does-the-php-error-message-notice-use-of-undefined-constant-mean) – Dharman May 26 '19 at 18:47
  • @Dharman That didn't look similar to my question to me, and it answered nothing for me. I was more confused after looking at that page. – Ziana Mitchell May 26 '19 at 20:01

2 Answers2

3

Your array looks great, you might just want to slightly modify it, and add " or ' in some places that are required, for example like:

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

or maybe:

$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);

Output

This is the output of 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?

Community
  • 1
  • 1
Emma
  • 27,428
  • 11
  • 44
  • 69
1

Key values must be quoted so use "keyname" or '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", ) ); } '''
Jimmix
  • 5,644
  • 6
  • 44
  • 71