登录

Get error Notice: Array to string conversion数组到字符串的转换错误

1970-01-01 ·PHP编程

数组,字符串转换

在PHP类构造中使用数组获取数组到字符串的转换错误

获取错误通知:数组到字符串的转换
Get error Notice: Array to string conversion

what"s wrong in this line $this->database["dsn"] = "mysql:host=$this->database["host"];dbname=$this->database["db"]";

代码:
class databaseClass {
  //数据变量
  private $database;

  //构造
  public function __construct() {
    // database info
    $this->database["host"] = "localhost";
    $this->database["db"] = "dbname";
    $this->database["username"] = "root";
    $this->database["password"] = "123";
    $this->database["dsn"] = "mysql:host=$this->database["host"];dbname=$this->database["db"]";
  }
}

对于变量插值,需要使用复杂的语法(大括号)。
You need to use complex syntax (curly braces) for the variable interpolation.

"mysql:host={$this->database["host"]};dbname={$this->database["db"]}";

请参阅手册中关于变量分析的“复杂(大括号)语法”部分。



使用简单语法(不带大括号)PHP只是试图将$this->database插入到字符串中,当它将数组转换为字符串时,会出错error Notice: Array to string conversion。


Using simple syntax (without the braces) PHP is just trying to insert $this->database into the string, which gives you that notice when it converts the array to a string.




· END ·

TAG:数组,字符串转换 来源:网络

热门标签

首页 - 导航 - 免责 - 官方微信 - 用户反馈

© 2019 GOODTEXT.CN