PHP 오류 메시지 "알림 : 정의되지 않은 상수 사용"은 무엇을 의미합니까?
PHP는 "알림 : 정의되지 않은 상수 사용"이라는 로그에이 오류를 기록하고 있습니다.
로그 오류 :
PHP Notice: Use of undefined constant department - assumed 'department' (line 5)
PHP Notice: Use of undefined constant name - assumed 'name' (line 6)
PHP Notice: Use of undefined constant email - assumed 'email' (line 7)
PHP Notice: Use of undefined constant message - assumed 'message' (line 8)
관련 코드 줄 :
$department = mysql_real_escape_string($_POST[department]);
$name = mysql_real_escape_string($_POST[name]);
$email = mysql_real_escape_string($_POST[email]);
$message = mysql_real_escape_string($_POST[message]);
그것은 무엇을 의미하며 왜 보입니까?
배열 키를 인용해야합니다.
$department = mysql_real_escape_string($_POST['department']);
$name = mysql_real_escape_string($_POST['name']);
$email = mysql_real_escape_string($_POST['email']);
$message = mysql_real_escape_string($_POST['message']);
이기 때문에, 그것은라는 상수를 찾고 있었다 department
, name
, email
, message
, 등이 같은 일정을 찾을 수없는 경우, 문자열 ( '부서'등)와 같은 PHP (변태) 해석을. 분명히, 상수를 나중에 정의하면 쉽게 깨질 수 있습니다 (소문자 상수를 갖는 것은 좋지 않습니다).
이 오류 메시지는 PHP가 알 수없는 토큰을 동일한 이름의 상수 문자열로 암시 적으로 선언한다는 불행한 사실로 인해 발생합니다.
즉, 이것을 해석하려고합니다 (빠진 따옴표를 주목하십시오).
$_POST[department]
PHP에서 유효한 구문이 될 수있는 유일한 유효한 방법은 이전에 상수가 department
정의 된 경우입니다. 슬프게도,이 시점에서 치명적인 오류로 죽기보다는이 통지를 발행하고 상수가 동일한 이름과 값으로 정의 된 것처럼 작동합니다.
// Implicit declaration of constant called department with value 'department'
define('department', 'department');
이 오류 메시지를 얻을 수있는 방법은 여러 가지가 있지만 모두 동일한 근본 원인 ( 상수 일 수 있는 토큰) 이 있습니다.
따옴표없는 문자열 : $my_array[bad_key]
이것은 귀하의 경우에 문제가되는 것이며, 인용되지 않은 문자열 배열 키가 있기 때문입니다. 문자열 키를 수정하면 버그가 수정됩니다.
변화:
$department = mysql_real_escape_string($_POST[department]);
...(etc)...
에:
$department = mysql_real_escape_string($_POST['department']);
...(etc)...
변수없는 달러 기호 : var_without_dollar
이 오류 메시지가 표시 될 수있는 또 다른 이유 $
는 변수 또는 $this->
멤버에서 벗어난 경우 입니다. 예를 들어, 다음 중 하나가 비슷한 오류 메시지를 발생시킵니다.
my_local; // should be $my_local
my_member; // should be $this->my_member
변수 이름에 잘못된 문자가 있습니다 : $bad-variable-name
변수 이름에 허용되지 않는 문자를 사용하려고하면 유사하지만 더 미묘한 문제가 발생할 수 있습니다 -
. 밑줄 대신 하이픈 ( ) _
이 일반적인 경우입니다.
예를 들어 변수 이름에 밑줄이 허용되므로이 방법은 정상입니다 .
if (123 === $my_var) {
do_something();
}
그러나 이것은 아닙니다 :
if (123 === $my-var) {
do_something();
}
다음과 동일하게 해석됩니다.
if (123 === $my - var) { // variable $my minus constant 'var'
do_something();
}
클래스 범위를 지정하지 않고 클래스 상수 참조
클래스 상수를 참조하려면 클래스 범위를 지정해야합니다. ::
이것을 놓치면 PHP는 전역에 대해 이야기하고 있다고 생각합니다 define()
.
예 :
class MyClass {
const MY_CONST = 123;
public function my_method() {
return self::MY_CONST; // This is fine
}
public function my_method() {
return MyClass::MY_CONST; // This is fine
}
public function my_bad_method() {
return MY_CONST; // BUG - need to specify class scope
}
}
Using a constant that's not defined in this version of PHP, or is defined in an extension that's not installed
There are some system-defined constants that only exist in newer versions of PHP, for example the mode option constants for round()
such as PHP_ROUND_HALF_DOWN
only exist in PHP 5.3 or later.
So if you tried to use this feature in PHP 5.2, say:
$rounded = round($my_var, 0, PHP_ROUND_HALF_DOWN);
You'd get this error message:
Use of undefined constant PHP_ROUND_HALF_DOWN - assumed 'PHP_ROUND_HALF_DOWN' Warning (2): Wrong parameter count for round()
you probably forgot to use ""
.
For exemple:
$_array[text] = $_var;
change to:
$_array["text"] = $_var;
You missed putting single quotes around your array keys:
$_POST[email]
should be:
$_POST['email']
The correct way of using post variables is
<?php
$department = $_POST['department'];
?>
Use single quotation(')
<?php
${test}="test information";
echo $test;
?>
Notice: Use of undefined constant test - assumed 'test' in D:\xampp\htdocs\sp\test\envoirnmentVariables.php on line 3 test information
Insert single quotes.
Example
$department = mysql_real_escape_string($_POST['department']);
$name = mysql_real_escape_string($_POST['name']);
$email = mysql_real_escape_string($_POST['email']);
$message = mysql_real_escape_string($_POST['message']);
Am not sure if there is any difference am using code igniter and i use "" for the names and it works great.
$department = mysql_real_escape_string($_POST["department"]);
$name = mysql_real_escape_string($_POST["name"]);
$email = mysql_real_escape_string($_POST["email"]);
$message = mysql_real_escape_string($_POST["message"]);
regards,
Jorge.
Looks like the predefined fetch constants went away with the MySQL extension, so we need to add them before the first function...
//predifined fetch constants
define('MYSQL_BOTH',MYSQLI_BOTH);
define('MYSQL_NUM',MYSQLI_NUM);
define('MYSQL_ASSOC',MYSQLI_ASSOC);
I tested and succeeded.
'IT' 카테고리의 다른 글
Swift에서 수학적 PI 상수를 얻는 방법 (0) | 2020.06.13 |
---|---|
Intellij IDEA의 System.out.println () 바로 가기 (0) | 2020.06.13 |
Laravel 5 Clear Views 캐시 (0) | 2020.06.13 |
Android Lollipop에서 _really_ 프로그래밍 방식으로 기본 및 악센트 색상을 변경하는 방법은 무엇입니까? (0) | 2020.06.13 |
루비 배열 : % w 대 % W (0) | 2020.06.13 |