php - 将变量声明为字符串,然后将其用作数组?

标签 php php-7.4

PHP 中是否发生了一些变化,将变量声明为字符串,然后将其用作数组是不行的。我们升级到了 bitnamies wamp stack,它破坏了我们的应用程序。变量之一

$change="";

后来用了

$change[$k] = "this";

它仍然是单个字符串,而不是变成数组。这是我可以更改的 php.ini 配置吗?

最佳答案

从 7.1 开始,您描述的行为发生了变化。请参阅 Assignment via string index access on an empty string 的更改日志

String modification by character on an empty string now works like for non-empty strings, i.e. writing to an out of range offset pads the string with spaces, where non-integer types are converted to integer, and only the first character of the assigned string is used. Formerly, empty strings where silently treated like an empty array.

<?php
$change='';
$change[2] = "this";
var_dump($change); 

/*
Prior to 7.1: 
  array(1) {
    [2]=>
    string(4) "this"
  }

7.1 and up:
  string(3) "  t"

PHP 7 已经有了许多改进,使“更严格”的输入成为可能,并允许更好的 type hinting 。尽管如此,PHP 仍然被认为是一种松散类型的语言。一些示例包括,从 7.0 开始,Scalar type declarations , Return type declarationsstrict_types指令。

关于php - 将变量声明为字符串,然后将其用作数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64146031/

相关文章:

php - Nginx 找不到文件但从 mysql 中获取信息

javascript - 长轮询挂起所有其他服务器请求

php - 如何解决 php 7.4 中的 l5-swagger 问题 -"Trying to access array offset on value of type bool"?

php - 如何在 PHP 7.4 中启用 opcache 预加载?

floating-point - PHP 8 浮点小数点不同于 PHP 7

php - 验证php邮件

php - Codeigniter 和 PHP - 强制 404?

php - 在 PHP 中更新 JSON 数组

php - 不推荐使用带大括号的数组和字符串偏移访问语法

php7.4 mysqli 超时并显示 "gone away"