Use of undefined constant error in php

喜欢而已 提交于 2019-12-20 07:55:50

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!