PHP array vs [ ] in method and variable declaration

后端 未结 4 758
面向向阳花
面向向阳花 2020-12-14 14:39

I have a question about PHP syntax.

When defining functions and variables, which convention should I use?

I know they do the same thing in practice but I wou

4条回答
  •  半阙折子戏
    2020-12-14 15:04

    from PHP docs:

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

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

    if you'd try using '[]' notation in earlier version than 5.4, it will fail, and PHP will throw a syntax error

    for backward-compatibility you should use array() syntax.

提交回复
热议问题