参考 - 这个错误在 PHP 中意味着什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12769982/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Reference - What does this error mean in PHP?
提问by hakre
What is this?
这是什么?
This is a number of answers about warnings, errors, and notices you might encounter while programming PHP and have no clue how to fix them. This is also a Community Wiki, so everyone is invited to participate adding to and maintaining this list.
这是有关在编写 PHP 时可能遇到的警告、错误和通知的许多答案,但不知道如何修复它们。这也是一个社区 Wiki,因此邀请所有人参与添加和维护此列表。
Why is this?
为什么是这样?
Questions like "Headers already sent"or "Calling a member of a non-object"pop up frequently on Stack Overflow. The root cause of those questions is always the same. So the answers to those questions typically repeat them and then show the OP which line to change in their particular case. These answers do not add any value to the site because they only apply to the OP's particular code. Other users having the same error cannot easily read the solution out of it because they are too localized. That is sad because once you understood the root cause, fixing the error is trivial. Hence, this list tries to explain the solution in a general way to apply.
Stack Overflow 上经常会弹出诸如“标题已经发送”或“调用非对象的成员”之类的问题。这些问题的根本原因总是相同的。因此,这些问题的答案通常会重复它们,然后向 OP 显示在特定情况下要更改哪一行。这些答案不会为站点增加任何价值,因为它们仅适用于 OP 的特定代码。其他遇到相同错误的用户无法轻松从中读取解决方案,因为他们过于本地化。这很可悲,因为一旦您了解了根本原因,修复错误就变得微不足道了。因此,此列表试图以通用的应用方式解释解决方案。
What should I do here?
我应该在这里做什么?
If your question has been marked as a duplicate of this one, please find your error message below and apply the fix to your code. The answers usually contain further links to investigate in case it shouldn't be clear from the general answer alone.
如果您的问题已被标记为与此问题重复,请在下方找到您的错误消息并将修复应用到您的代码。答案通常包含进一步的调查链接,以防仅凭一般答案就不清楚。
If you want to contribute, please add your "favorite" error message, warning or notice, one per answer, a short description what it means (even if it is only highlighting terms to their manual page), a possible solution or debugging approach and a listing of existing Q&A that are of value. Also, feel free to improve any existing answers.
如果您想做出贡献,请添加您“最喜欢的”错误消息、警告或通知、每个答案一个、其含义的简短描述(即使只是突出显示其手册页中的术语)、可能的解决方案或调试方法以及现有有价值的问答列表。此外,请随时改进任何现有的答案。
The List
列表
- Nothing is seen. The page is empty and white.(also known as White Page/Screen Of Death)
- Code doesn't run/what looks like parts of my PHP code are output
- Warning: Cannot modify header information - headers already sent
- Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean givena.k.a.
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resourcea.k.a.
Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given(or similar variations) - Warning: [function]expects parameter 1 to be resource, boolean given
- Warning: [function]: failed to open stream: [reason]
- Warning: open_basedir restriction in effect
- Warning: Division by zero
- Warning: Illegal string offset 'XXX'
- Warning: count(): Parameter must be an array or an object that implements Countable
- Parse error: syntax error, unexpected '['
- Parse error: syntax error, unexpected T_XXX
- Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE
- Parse error: syntax error, unexpected T_PAAMAYIM_NEKUDOTAYIM
- Parse error: syntax error, unexpected 'require_once' (T_REQUIRE_ONCE), expecting function (T_FUNCTION)
- Parse error: syntax error, unexpected T_VARIABLE
- Fatal error: Allowed memory size of XXX bytes exhausted (tried to allocate XXX bytes)
- Fatal error: Call to a member function ... on a non-object or null
- Fatal Error: Call to Undefined function XXX
- Fatal Error: Cannot redeclare XXX
- Fatal error: Can't use function return value in write context
- Fatal error: Declaration of AAA::BBB() must be compatible with that of CCC::BBB()'
- Fatal error: Using $this when not in object context
- Fatal error: Object of class Closure could not be converted to string
- Fatal error: Undefined class constant
- Notice: Array to string conversion
- Notice: Trying to get property of non-object error
- Notice: Undefined variable or property
- Notice: Undefined Index
- Notice: Undefined offset XXX[Reference]
- Notice: Uninitialized string offset: XXX
- Notice: Use of undefined constant XXX - assumed 'XXX'
- MySQL: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ... at line ...
- Strict Standards: Non-static method [<class>::<method>]should not be called statically
- Warning: function expects parameter X to be boolean/string/integer
- HTTP Error 500 - Internal server error
- Deprecated: Array and string offset access syntax with curly braces is deprecated
- 什么都看不见。页面是空白的和白色的。(也称为白页/死机屏幕)
- 代码不运行/我的 PHP 代码的一部分看起来像是输出
- 警告:无法修改标头信息 - 标头已发送
- 警告:mysql_fetch_array()预计参数1是资源,布尔给出又名
警告:mysql_fetch_array():提供的参数不是一个有效的MySQL结果资源又名
警告:mysqli_num_rows()预计参数1是mysqli_result,布尔给定(或类似的变化) - 警告:[function]期望参数 1 是资源,给定布尔值
- 警告:[功能]:无法打开流:[原因]
- 警告:open_basedir 限制生效
- 警告:除以零
- 警告:非法字符串偏移“XXX”
- 警告:count():参数必须是一个数组或一个实现 Countable 的对象
- 解析错误:语法错误,意外的“[”
- 解析错误:语法错误,意外的 T_XXX
- 解析错误:语法错误,意外的 T_ENCAPSED_AND_WHITESPACE
- 解析错误:语法错误,意外 T_PAAMAYIM_NEKUDOTAYIM
- 解析错误:语法错误,意外的“require_once”(T_REQUIRE_ONCE),期望函数(T_FUNCTION)
- 解析错误:语法错误,意外的 T_VARIABLE
- 致命错误:已用尽 XXX 字节的允许内存大小(尝试分配 XXX 字节)
- 致命错误:在非对象或 null 上调用成员函数...
- 致命错误:调用未定义的函数 XXX
- 致命错误:无法重新声明 XXX
- 致命错误:无法在写入上下文中使用函数返回值
- 致命错误:AAA::BBB() 的声明必须与 CCC::BBB()的声明兼容'
- 致命错误:不在对象上下文中使用 $this
- 致命错误:类 Closure 的对象无法转换为字符串
- 致命错误:未定义的类常量
- 注意:数组到字符串的转换
- 注意:尝试获取非对象错误的属性
- 注意:未定义的变量或属性
- 注意:未定义索引
- 注意:未定义的偏移量 XXX[参考]
- 注意:未初始化的字符串偏移量:XXX
- 注意:使用未定义的常量 XXX - 假定为“XXX”
- MySQL:您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,了解要在附近使用的正确语法...
- 严格标准:非静态方法[<class>::<method>]不应静态调用
- 警告:函数期望参数 X 为布尔值/字符串/整数
- HTTP 错误 500 - 内部服务器错误
- 不推荐使用:不推荐使用带花括号的数组和字符串偏移访问语法
Also, see:
另请参阅:
回答by Gordon
Warning: Cannot modify header information - headers already sent
警告:无法修改标头信息 - 标头已发送
Happens when your script tries to send an HTTP header to the client but there already was output before, which resulted in headers to be already sent to the client.
当您的脚本尝试向客户端发送 HTTP 标头但之前已经有输出时发生,这导致标头已经发送到客户端。
This is an E_WARNINGand it will not stop the script.
这是一个E_WARNING并且它不会停止脚本。
A typical example would be a template file like this:
一个典型的例子是这样的模板文件:
<html>
<?php session_start(); ?>
<head><title>My Page</title>
</html>
...
The session_start()function will try to send headers with the session cookie to the client. But PHP already sent headers when it wrote the <html>element to the output stream. You'd have to move the session_start()to the top.
该session_start()函数将尝试将带有会话 cookie 的标头发送到客户端。但是 PHP 在将<html>元素写入输出流时已经发送了标头。您必须将 移到session_start()顶部。
You can solve this by going through the lines beforethe code triggering the Warning and check where it outputs. Move any header sending code before that code.
您可以通过检查触发警告的代码之前的行并检查它的输出位置来解决此问题。将任何标头发送代码移动到该代码之前。
An often overlooked output is new lines after PHP's closing ?>. It is considered a standard practice to omit ?>when it is the last thing in the file. Likewise, another common cause for this warning is when the opening <?phphas an empty space, line, or invisible character before it, causing the web server to send the headers and the whitespace/newline thus when PHP starts parsing won't be able to submit any header.
一个经常被忽视的输出是 PHP 关闭后的新行?>。?>当它是文件中的最后一件事时省略被认为是一种标准做法。同样,此警告的另一个常见原因是当开头<?php有一个空格、行或不可见字符时,导致 Web 服务器发送标题和空格/换行符,因此当 PHP 开始解析时将无法提交任何标题。
If your file has more than one <?php ... ?>code block in it, you should not have any spaces in between them. (Note: You might have multiple blocks if you had code that was automatically constructed)
如果您的文件中有多个<?php ... ?>代码块,则它们之间不应有任何空格。(注意:如果您有自动构造的代码,您可能有多个块)
Also make sure you don't have any Byte Order Marks in your code, for example when the encoding of the script is UTF-8 with BOM.
还要确保您的代码中没有任何字节顺序标记,例如当脚本的编码是带有 BOM 的 UTF-8 时。
Related Questions:
相关问题:
回答by hakre
Fatal error: Call to a member function ... on a non-object
致命错误:在非对象上调用成员函数...
Happens with code similar to xyz->method()where xyzis not an object and therefore that methodcan not be called.
发生类似于xyz->method()where xyzis not an object 的代码,因此method无法调用。
This is a fatal error which will stop the script (forward compatibility notice: It will become a catchable error starting with PHP 7).
这是一个将停止脚本的致命错误(前向兼容性注意事项:从 PHP 7 开始,它将成为一个可捕获的错误)。
Most often this is a sign that the code has missing checks for error conditions. Validate that an object is actually an object before calling its methods.
大多数情况下,这表明代码缺少对错误条件的检查。在调用它的方法之前验证一个对象实际上是一个对象。
A typicalexample would be
一个典型的例子是
// ... some code using PDO
$statement = $pdo->prepare('invalid query', ...);
$statement->execute(...);
In the example above, the query cannot be prepared and prepare()will assign falseto $statement. Trying to call the execute()method will then result in the Fatal Error because falseis a "non-object" because the value is a boolean.
在上面的示例中,无法准备查询prepare()并将分配false给$statement。尝试调用该execute()方法将导致致命错误,因为它false是一个“非对象”,因为该值是一个布尔值。
Figure out whyyour function returned a boolean instead of an object. For example, check the $pdoobject for the last error that occurred. Details on how to debug this will depend on how errors are handled for the particular function/object/class in question.
弄清楚为什么你的函数返回一个布尔值而不是一个对象。例如,检查$pdo对象是否存在上次发生的错误。有关如何调试的详细信息将取决于有关特定函数/对象/类的错误处理方式。
If even the ->prepareis failing then your $pdodatabase handle object didn't get passed into the current scope. Find where it got defined. Then pass it as a parameter, store it as property, or share it via the global scope.
如果即使->prepare失败,那么您的$pdo数据库句柄对象也不会传递到当前范围。找到它被定义的地方。然后将其作为参数传递,将其存储为属性,或通过全局范围共享。
Another problem may be conditionally creating an object and then trying to call a method outside that conditional block. For example
另一个问题可能是有条件地创建一个对象,然后尝试调用该条件块之外的方法。例如
if ($someCondition) {
$myObj = new MyObj();
}
// ...
$myObj->someMethod();
By attempting to execute the method outside the conditional block, your object may not be defined.
通过尝试在条件块之外执行该方法,您的对象可能未定义。
Related Questions:
相关问题:
回答by nalply
Nothing is seen. The page is empty and white.
什么都看不见。页面是空白的和白色的。
Also known as the White Page Of Deathor White Screen Of Death. This happens when error reporting is turned off and a fatal error (often syntax error) occurred.
也称为死亡白页或死亡白屏。当错误报告被关闭并且发生致命错误(通常是语法错误)时,就会发生这种情况。
If you have error logging enabled, you will find the concrete error message in your error log. This will usually be in a file called "php_errors.log", either in a central location (e.g. /var/log/apache2on many Linux environments) or in the directory of the script itself (sometimes used in a shared hosting environment).
如果您启用了错误日志,您将在错误日志中找到具体的错误消息。这通常位于名为“php_errors.log”的文件中,位于中央位置(例如/var/log/apache2在许多 Linux 环境中)或脚本本身的目录中(有时在共享托管环境中使用)。
Sometimes it might be more straightforward to temporarily enable the display of errors. The white page will then display the error message. Take care because these errors are visible to everybody visiting the website.
有时,暂时启用错误显示可能更直接。然后白页将显示错误消息。请注意,因为访问该网站的每个人都可以看到这些错误。
This can be easily done by adding at the top of the script the following PHP code:
这可以通过在脚本顶部添加以下 PHP 代码轻松完成:
ini_set('display_errors', 1); error_reporting(~0);
The code will turn on the display of errors and set reporting to the highest level.
该代码将打开错误显示并将报告设置为最高级别。
Since the ini_set()is executed at runtime it has no effects on parsing/syntax errors. Those errors will appear in the log. If you want to display them in the output as well (e.g. in a browser) you have to set the display_startup_errorsdirective to true. Do this either in the php.inior in a .htaccessor by any other method that affects the configuration beforeruntime.
由于ini_set()在运行时执行它对解析/语法错误没有影响。这些错误会出现在日志中。如果您还想在输出中显示它们(例如在浏览器中),您必须将display_startup_errors指令设置为true. 在php.ini或 中执行此操作,.htaccess或通过影响运行前配置的任何其他方法执行此操作。
You can use the same methods to set the log_errorsand error_logdirectives to choose your own log file location.
您可以使用相同的方法设置log_errors和error_log指令来选择您自己的日志文件位置。
Looking in the log or using the display, you will get a much better error message and the line of code where your script comes to halt.
查看日志或使用显示,您将获得更好的错误消息和脚本停止的代码行。
Related questions:
相关问题:
- PHP's white screen of death
- White screen of death!
- PHP Does Not Display Error Messages
- PHP emitting 500 on errors - where is this documented?
- How to get useful error messages in PHP?
- All PHP "White Page of Death" Questions on Stackoverflow
Related errors:
相关错误:
回答by Gordon
Notice: Undefined Index
注意:未定义索引
Happens when you try to access an array by a key that does not exist in the array.
当您尝试通过数组中不存在的键访问数组时发生。
A typical example of an Undefined Indexnotice would be (demo)
Undefined Index通知的典型示例是(演示)
$data = array('foo' => '42', 'bar');
echo $data['spinach'];
echo $data[1];
Both spinachand 1do not exist in the array, causing an E_NOTICEto be triggered.
双方spinach并1在阵列中不存在,造成E_NOTICE被触发。
The solution is to make sure the index or offset exists prior to accessing that index. This may mean that you need to fix a bug in your program to ensure that those indexes do exist when you expect them to. Or it may mean that you need to test whether the indexes exist using array_key_existsor isset:
解决方案是在访问该索引之前确保该索引或偏移量存在。这可能意味着您需要修复程序中的错误,以确保这些索引确实在您期望的时候存在。或者这可能意味着您需要使用array_key_exists或测试索引是否存在isset:
$data = array('foo' => '42', 'bar');
if (array_key_exists('spinach', $data)) {
echo $data['spinach'];
}
else {
echo 'No key spinach in the array';
}
If you have code like:
如果你有这样的代码:
<?php echo $_POST['message']; ?>
<form method="post" action="">
<input type="text" name="message">
...
then $_POST['message']will not be set when this page is first loaded and you will get the above error. Only when the form is submitted and this code is run a second time will the array index exist. You typically check for this with:
$_POST['message']首次加载此页面时不会设置then ,您将收到上述错误。只有在提交表单并且第二次运行此代码时,数组索引才会存在。您通常通过以下方式检查:
if ($_POST) .. // if the $_POST array is not empty
// or
if ($_SERVER['REQUEST_METHOD'] == 'POST') .. // page was requested with POST
Related Questions:
相关问题:
回答by xdazz
Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given
警告:mysql_fetch_array() 期望参数 1 是资源,给定的布尔值
First and foremost:
首先也是最重要的:
Please, don't use
mysql_*functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statementsinstead, and use PDOor MySQLi- this articlewill help you decide which. If you choose PDO, here is a good tutorial.
请不要
mysql_*在新代码中使用函数。它们不再被维护并被正式弃用。看到红框了吗?了解准备好的语句,并使用PDO或MySQLi-本文将帮助您决定哪个。如果您选择 PDO,这里有一个很好的教程。
This happens when you try to fetch data from the result of mysql_querybut the query failed.
当您尝试从结果中获取数据mysql_query但查询失败时会发生这种情况。
This is a warning and won't stop the script, but will make your program wrong.
这是一个警告,不会停止脚本,但会使您的程序出错。
You need to check the result returned by mysql_queryby
您需要检查返回的结果mysql_query通过
$res = mysql_query($sql);
if (!$res) {
die(mysql_error());
}
// after checking, do the fetch
Related Questions:
相关问题:
- mysql_fetch_array() expects parameter 1 to be resource, boolean given in select
- All "mysql_fetch_array() expects parameter 1 to be resource, boolean given" Questions on Stackoverflow
Related Errors:
相关错误:
Other mysql*functions that also expect a MySQL result resource as a parameter will produce the same error for the same reason.
其他mysql*将 MySQL 结果资源作为参数的函数也会出于同样的原因产生相同的错误。
回答by xdazz
Fatal error: Using $this when not in object context
致命错误:不在对象上下文中使用 $this
$thisis a special variable in PHPwhich can not be assigned. If it is accessed in a context where it does not exist, this fatal error is given.
$this是 PHP 中一个不能赋值的特殊变量。如果在它不存在的上下文中访问它,则会出现此致命错误。
This error can occur:
可能会发生此错误:
If a non-static method is called statically. Example:
class Foo { protected $var; public function __construct($var) { $this->var = $var; } public static function bar () { // ^^^^^^ echo $this->var; // ^^^^^ } } Foo::bar();How to fix:review your code again,
$thiscan only be used in an object context, and should never be used in a static method. Also, a static method should not access the non-static property. Useself::$static_propertyto access the static property.If code from a class method has been copied over into a normal function or just the global scope andkeeping the
$thisspecial variable.
How to fix:Review the code and replace$thiswith a different substitution variable.
如果静态调用非静态方法。例子:
class Foo { protected $var; public function __construct($var) { $this->var = $var; } public static function bar () { // ^^^^^^ echo $this->var; // ^^^^^ } } Foo::bar();如何修复:再次检查您的代码,
$this只能在对象上下文中使用,并且永远不应在静态方法中使用。此外,静态方法不应访问非静态属性。使用self::$static_property访问静态属性。如果来自类方法的代码已被复制到普通函数或只是全局范围并保留
$this特殊变量。
如何修复:查看代码并替换$this为不同的替换变量。
Related Questions:
相关问题:
- Call non-static method as static: PHP Fatal error: Using $this when not in object context
- Copy over code: Fatal error: Using $this when not in object context
- All "Using $this when not in object context" Questions on Stackoverflow
回答by Gordon
Fatal error: Call to undefined function XXX
致命错误:调用未定义的函数 XXX
Happens when you try to call a function that is not defined yet. Common causes include missing extensions and includes, conditional function declaration, function in a function declaration or simple typos.
当您尝试调用尚未定义的函数时发生。常见原因包括缺少扩展和包含、条件函数声明、函数声明中的函数或简单的拼写错误。
Example 1 - Conditional Function Declaration
示例 1 - 条件函数声明
$someCondition = false;
if ($someCondition === true) {
function fn() {
return 1;
}
}
echo fn(); // triggers error
In this case, fn()will never be declared because $someConditionis not true.
在这种情况下,fn()永远不会被声明,因为$someCondition不是真的。
Example 2 - Function in Function Declaration
示例 2 - 函数声明中的函数
function createFn()
{
function fn() {
return 1;
}
}
echo fn(); // triggers error
In this case, fnwill only be declared once createFn()gets called. Note that subsequent calls to createFn()will trigger an error about Redeclaration of an Existing function.
在这种情况下,fn只会在createFn()被调用时声明。请注意,后续调用createFn()将触发有关重新声明现有函数的错误。
You may also see this for a PHP built-in function. Try searching for the function in the official manual, and check what "extension" (PHP module) it belongs to, and what versions of PHP support it.
您也可以在 PHP 内置函数中看到这一点。尝试在官方手册中搜索该函数,并查看它属于什么“扩展”(PHP 模块),以及哪些版本的PHP 支持它。
In case of a missing extension, install that extension and enable it in php.ini. Refer to the Installation Instructions in the PHP Manual for the extension your function appears in. You may also be able to enable or install the extension using your package manager (e.g. aptin Debian or Ubuntu, yumin Red Hat or CentOS), or a control panel in a shared hosting environment.
如果缺少扩展,请安装该扩展并在 php.ini 中启用它。请参阅 PHP 手册中的安装说明,了解您的功能所在的扩展。您也可以使用包管理器(例如apt在 Debian 或 Ubuntu、yumRed Hat 或 CentOS 中)或控制面板启用或安装扩展在共享托管环境中。
If the function was introduced in a newer version of PHP from what you are using, you may find links to alternative implementations in the manual or its comment section. If it has been removed from PHP, look for information about why, as it may no longer be necessary.
如果该函数是在您使用的较新版本的 PHP 中引入的,您可以在手册或其注释部分找到替代实现的链接。如果它已从 PHP 中删除,请查找有关原因的信息,因为它可能不再需要。
In case of missing includes, make sure to include the file declaring the function before calling the function.
如果缺少包含,请确保在调用函数之前包含声明该函数的文件。
In case of typos, fix the typo.
如果有错别字,请修正错字。
Related Questions:
相关问题:
回答by LeleDumbo
Parse error: syntax error, unexpected T_XXX
解析错误:语法错误,意外的 T_XXX
Happens when you have T_XXXtokenin unexpected place, unbalanced (superfluous) parentheses, use of short tag without activating it in php.ini, and many more.
当您在意想不到的地方有T_XXX令牌、不平衡(多余)括号、使用短标签而不在 php.ini 中激活它时,就会发生这种情况,等等。
Related Questions:
相关问题:
- Reference: PHP Parse/Syntax Errors; and How to solve them?
- Parse Error: syntax error: unexpected '{'
- Parse error: Syntax error, unexpected end of file in my PHP code
- Parse error: syntax error, unexpected '<' in - Fix?
- Parse error: syntax error, unexpected '?'
- 参考:PHP 解析/语法错误;以及如何解决它们?
- 解析错误:语法错误:意外的“{”
- 解析错误:语法错误,我的 PHP 代码中出现意外的文件结尾
- 解析错误:语法错误,意外的“<” - 修复?
- 解析错误:语法错误,意外的“?”
For further help see:
如需进一步帮助,请参阅:
- http://phpcodechecker.com/- Which does provide some more helpful explanations on your syntax woes.
- http://phpcodechecker.com/- 这确实为您的语法问题提供了一些更有帮助的解释。
回答by xdazz
Fatal error: Can't use function return value in write context
致命错误:无法在写入上下文中使用函数返回值
This usually happens when using a function directly with empty.
这通常发生在直接使用函数时empty。
Example:
例子:
if (empty(is_null(null))) {
echo 'empty';
}
This is because emptyis a language construct and not a function, it cannot be called with an expression as its argument in PHP versions before 5.5. Prior to PHP 5.5, the argument to empty()must be a variable, but an arbitrary expression (such as a return value of a function) is permissible in PHP 5.5+.
这是因为empty是语言结构而不是函数,在 PHP 5.5 之前的版本中不能使用表达式作为参数调用它。在 PHP 5.5 之前, to 的参数empty()必须是变量,但在 PHP 5.5+ 中允许任意表达式(例如函数的返回值)。
empty, despite its name, does not actually check if a variable is "empty". Instead, it checks if a variable doesn't exist, or == false. Expressions (like is_null(null)in the example) will always be deemed to exist, so here emptyis only checking if it is equal to false. You could replace empty()here with !, e.g. if (!is_null(null)), or explicitly compare to false, e.g. if (is_null(null) == false).
empty,尽管它的名字,实际上并不检查变量是否为“空”。相反,它会检查变量是否不存在,或者== false. 表达式(如is_null(null)示例中所示)将始终被视为存在,因此这里empty仅检查它是否等于 false。您可以empty()在此处替换为!,例如if (!is_null(null)),或明确与 false 进行比较,例如if (is_null(null) == false)。
Related Questions:
相关问题:
回答by Jocelyn
MySQL: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ... at line ...
MySQL:您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,了解要在附近使用的正确语法...
This error is often caused because you forgot to properly escape the data passed to a MySQL query.
这个错误通常是因为你忘记正确地转义传递给 MySQL 查询的数据。
An example of what notto do (the "Bad Idea"):
不该做什么的例子(“坏主意”):
$query = "UPDATE `posts` SET my_text='{$_POST['text']}' WHERE id={$_GET['id']}";
mysqli_query($db, $query);
This code could be included in a page with a form to submit, with an URL such as http://example.com/edit.php?id=10(to edit the post n°10)
此代码可以包含在带有要提交的表单的页面中,其 URL 为http://example.com/edit.php?id=10(编辑帖子 n°10)
What will happen if the submitted text contains single quotes? $querywill end up with:
如果提交的文本包含单引号会怎样?$query最终会得到:
$query = "UPDATE `posts` SET my_text='I'm a PHP newbie' WHERE id=10';
And when this query is sent to MySQL, it will complain that the syntax is wrong, because there is an extra single quote in the middle.
并且当这个查询发送到MySQL时,它会抱怨语法错误,因为中间多出了一个单引号。
To avoid such errors, you MUSTalways escape the data before use in a query.
为避免此类错误,在查询中使用之前,您必须始终对数据进行转义。
Escaping data before use in a SQL query is also very important because if you don't, your script will be open to SQL injections. An SQL injection may cause alteration, loss or modification of a record, a table or an entire database. This is a very serious security issue!
在 SQL 查询中使用之前转义数据也非常重要,因为如果不这样做,您的脚本将受到 SQL 注入的影响。SQL 注入可能会导致记录、表或整个数据库的更改、丢失或修改。这是一个非常严重的安全问题!
Documentation:
文档:

