简体   繁体   中英

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:

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

Is it due to PHP version? 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?

The array short syntax [] was added in PHP 5.4 :)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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