Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
574 views
in Technique[技术] by (71.8m points)

linux - PHP syntax for arrays: different behaviour between PHP versions

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]
];

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.

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The shortened array syntax was only added in PHP 5.4: Arrays

As of PHP 5.4 you can also use the short array syntax, which replaces array() with [].

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

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

Live result


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...