dpglo66848 2014-10-20 18:31
浏览 244
已采纳

Const数组在php 5.6中运行?

In the php manual, it is stated in user contributed notes that const array is now allowed. I even checked other posts here in stackoverflow and they said the same. I tested the following code:

<?php

class Constants{
      const test = array("a"=>"apple","b"=>"ball","c"=>"car");
}
echo Constants::test["b"];
echo Constants::test["c"];
echo Constants::test["a"];
?>

Output:

ball
car
apple

The above code works. However This doesn't work if I use define('test',array("a"=>"apple","b"=>"ball","c"=>"car") outside of the class. Is this a new undocumented change or happening only in my setup?

My setup is php 5.6.1(32-bit thread safe) and apache 2.4.10 (32-bit) on windows 7 x64. I downloaded them from the their respective sites directly. I didn't use any WAMP stacks.

Also note that I am using netbeans 8.0.1 IDE for hints and it shows this error. Anyone know how to remove it?

Syntax error
unexpected: [
after:  identifier 'test'
expected:   instanceof, as, =>, }, ',', OR, XOR, &&, ?, ;, ||, &&, |, ^, &, ==, !=, ===, !==, <=,
>=, <, >, <<, >>, +, -, *, /, %, '.', ], (, ), :
----
  • 写回答

1条回答 默认 最新

  • duanjiu1894 2014-10-21 14:15
    关注

    Apparently if I use const outside of a class, the error in netbeans IDE won't show up.

    const test = array("a"=>"apple","b"=>"ball","c"=>"car");
    echo test["b"];
    echo test["c"];
    echo test["a"];
    

    This should work!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用