简体   繁体   English

PHP-使用“ []”将参数作为数组传递

[英]PHP - Use “[ ]” to pass parameters as array

I have a function that receives parameters as an array: 我有一个函数以数组形式接收参数:

public myFunction($options = array("option1"=>true, "option2"=>true, "option4"=>"astring"), $anotherparameter = 0) {
    if($options["option1"] === true) {
         //And here all the magic is done
    }
}

When I use it somewhere else I use something like this: 当我在其他地方使用它时,我会使用如下代码:

myFunction(["option1"=>false], 1) { //Magic }

I have installed PHP 5.5 on my localhost and this WORKS, when I upload it to my remote server it throws an error, and I have to put the array parameters like: 我已经在本地主机和此工作区上安装了PHP 5.5,将其上传到远程服务器时会引发错误,并且必须将数组参数放置为:

myFunction(array("option1"=>false), 1) { //Magic }

Is it due to PHP version? 是由于PHP版本引起的吗? The remote server has PHP 5.3, I just updated it to 5.4, but I want to know if I need an specific version of PHP to work with this kind of array syntax, or should I use always array(...) format? 远程服务器具有PHP 5.3,我刚刚将其更新为5.4,但是我想知道是否需要特定版本的PHP才能使用这种数组语法,还是应该始终使用array(...)格式?

The array short syntax [] was added in PHP 5.4 :) 数组短语法[]是在PHP 5.4中添加的:)

See: http://php.net/manual/en/migration54.new-features.php 请参阅: http//php.net/manual/en/migration54.new-features.php

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

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