以下代码
<?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);
?>
生成以下错误:
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"}
我想知道这种情况的发生,在这种情况下可以采取哪些措施来防止此错误。
答案 0 :(得分:2)
array(name => value)
不是有效的语法,php现在不是name
。您需要在数组键周围使用引号。
使用array("domain" => $email_domain, ...
等
答案 1 :(得分:1)
您应该在数组中的键周围使用引号,如下所示: -
$call = array('domain'=>$email_domain, 'email'=>$email_user, 'password'=>$email_pass, 'quota'=>$email_quota);
答案 2 :(得分:1)
如果您想使用文字作为数组键,则需要用双引号或单引号括起它们:
$call = array('domain'=>$email_domain,
'email'=>$email_user,
'password'=>$email_pass,
'quota'=>$email_quota);