简体   繁体   English

errorInfo()[2],PHP 5.3中是否出现意外的“ [”?

[英]errorInfo()[2], unexpected '[' in PHP 5.3?

This simple line 这条简单的线

$prep = $this->pdo->prepare($sql) or common::error("PDO prepare error: ".$prep->errorInfo()[2]);

is working on PHP 5.5, but fails on PHP5.3 (even if the PHP documentation says that PDOStatement::errorInfo returns an array on php 5.1+) saying: 在PHP 5.5上运行,但在PHP5.3上失败(即使PHP文档说PDOStatement :: errorInfo在php 5.1+上返回数组)也说:

Parse error: syntax error, unexpected '[' in ... 解析错误:语法错误,...中出现意外的'['

I use it many times in my application, and unfortunately I can't update the PHP version on this machine. 我在应用程序中使用了很多次,但是不幸的是我无法在这台机器上更新PHP版本。 Any workaround? 任何解决方法?

Just break it up into two steps: 只需将其分为两个步骤:

$error = $prep->errorInfo();
$error_msg = $error[2];

$prep = $this->pdo->prepare($sql) or common::error("PDO prepare error: ".$error_msg);

FYI, this is called Array Dereferencing and became available in PHP 5.4. 仅供参考,这称为数组解引用,并且在PHP 5.4中可用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM