doujian1954 2012-06-27 08:55
浏览 101
已采纳

映射常量值组

I have lists where strings correspond with values. For example, in a company list I could have Microsoft correspond with the letters MS.

Obviously, this could be transposed as a table in MySQL to make the list extensible but, from curiosity, how would you express this in constant form in PHP ?

P.S: There is the class-with-constants approach (see accepted answer here: PHP and Enumerations) to act as an enumeration but would that really be of any use seeing that enumerations map to integer values ?

  • 写回答

3条回答

  • dongliming2416 2012-06-27 09:00
    关注

    How about using define

    define("MS","Microsoft");
    echo MS;
    

    This would echo Microsoft.

    http://php.net/manual/en/function.define.php

    Also the link you gave to a possible solution could just as easily be used with strings instead. The only reason it acts as an enum from other languages is because you define the values from 0 to n, instead of doing that just use string instead.

    class Companies {
        const MS  = 'Microsoft';
        const IBM = 'International Business Machines';
    }
    
    echo Companies::MS;
    

    I think this would work.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥50 易语言把MYSQL数据库中的数据添加至组合框
  • ¥20 求数据集和代码#有偿答复
  • ¥15 关于下拉菜单选项关联的问题
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况