数组的PHP语法:PHP版本之间的行为不同 [英] PHP syntax for arrays: different behaviour between PHP versions

查看:60
本文介绍了数组的PHP语法:PHP版本之间的行为不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下语法制作了一个带有数组的PHP页面:

I made a PHP page with an array using the following syntax:

$Legenda = [
    "Cores"      => ["#FF0000", "#FFA500", "#FFFF00", "#64FF00", "#00AA00", "#005500", "#0000FF"],
    "ValMinimos" => [50,         62.85714,  75.71429,  88.57143, 101.42857, 114.28571, 127.12286],
    "ValMaximos" => [62.85714,   75.71429,  88.57143, 101.42857, 114.28571, 127.12286, 140]
];

在Windows上工作正常(PHP 5.4.6,使用EasyPHP安装),但是当我将同一页面上载到Ubuntu服务器(PHP 5.3.10,使用apt-get从存储库安装)时,出现错误: 解析错误:语法错误,在第13行的/var/www/ShapeTest_server_get_dados.php中出现意外的'['".第13行是$ Legenda数组定义.

which works fine on windows (PHP 5.4.6, installed using EasyPHP), but when I uploaded the same page to a Ubuntu Server (PHP 5.3.10, installed using apt-get from repositories), I get an error: "Parse error: syntax error, unexpected '[' in /var/www/ShapeTest_server_get_dados.php on line 13". Line 13 is the $Legenda array definition.

自从我用PHP编程以来已经有一段时间了,但是我记得语法是有效的.在Linux上默认是否关闭了此选项,还是为什么不起作用?

It's been a while since I programmed with PHP, but as I remember that syntax was valid. Is this an option turned off by default on linux, or why doesn't this work?

推荐答案

缩短数组 <仅在PHP 5.4中添加了href ="http://php.net/manual/en/migration54.new-features.php" rel ="nofollow">语法:

从PHP 5.4开始,您还可以使用短数组语法,该语法将array()替换为[].

<?php
$array = array(
    "foo" => "bar",
    "bar" => "foo",
);

// as of PHP 5.4
$array = [
    "foo" => "bar",
    "bar" => "foo",
];
?>

实时结果

这篇关于数组的PHP语法:PHP版本之间的行为不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆