问题
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
回答1:
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"
}
}
Reference 1
What does the PHP error message “Notice: Use of undefined constant” mean?
回答2:
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", ) ); } '''
来源:https://stackoverflow.com/questions/56316101/how-do-i-fix-this-use-of-undefined-constant-slug-assumed-slug