问题
The following code
<?php
$email_domain = "abc@gmail.com";
$email_user = "Roshan";
$email_pass = "admin";
$email_quota = "200";
$call = array(domain=>$email_domain, email=>$email_user, password=>$email_pass, quota=>$email_quota);
echo json_encode($call);
?>
generated following error:
Notice: Use of undefined constant domain - assumed 'domain' in C:\xampp\htdocs\test2.php on line 7
Notice: Use of undefined constant email - assumed 'email' in C:\xampp\htdocs\test2.php on line 7
Notice: Use of undefined constant password - assumed 'password' in C:\xampp\htdocs\test2.php on line 7
Notice: Use of undefined constant quota - assumed 'quota' in C:\xampp\htdocs\test2.php on line 7
{"domain":"abc@gmail.com","email":"Roshan","password":"admin","quota":"200"}
I want to know the occurrence of this and in this case what can be done to prevent this error.
回答1:
array(name => value)
is not a valid syntax, php doesn't now what name
is. You need quotation marks around your array keys.
Use array("domain" => $email_domain, ...
etc.
回答2:
You should use quotes around the keys in the array, like this :-
$call = array('domain'=>$email_domain, 'email'=>$email_user, 'password'=>$email_pass, 'quota'=>$email_quota);
回答3:
If you want to use literals as your array keys, you need to surround them in either double or single quotes:
$call = array('domain'=>$email_domain,
'email'=>$email_user,
'password'=>$email_pass,
'quota'=>$email_quota);
来源:https://stackoverflow.com/questions/27597900/use-of-undefined-constant-error-in-php