映射常量值组

我列出了字符串与值对应的列表。 例如,在公司列表中,我可以将Microsoft与字母MS对应。</ p>

显然,这可以在MySQL中转换为表格以使列表可扩展但是,从好奇心,如何 你会用PHP中的常量形式表达吗?</ p>

PS:有一个带常量的类方法(参见这里接受的答案: PHP和枚举)作为一个枚举,但是看到枚举映射到整数值真的有用吗?</ p>
</ div >

展开原文

原文

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 ?

dousha1394
dousha1394 我是一个字母标签杀手:)
8 年多之前 回复
du512053619
du512053619 嘿,你破坏了本报告中的重点哈哈!:)
8 年多之前 回复

3个回答



如何使用define </ p>

  define(“MS”,“Microsoft”)  ; 
echo MS;
</ code> </ pre>

这将回应微软。</ p>

http://php.net/manual/en/function.define.php </ p>

您为可能的解决方案提供的链接可以很容易地与字符串一起使用。 它作为来自其他语言的枚举的唯一原因是因为你定义了从0到n的值,而不是仅仅使用字符串。</ p>

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

echo Companies :: MS;
</ code> </ pre>

我认为这是 会工作。</ p>
</ div>

展开原文

原文

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.

drmeu26880
drmeu26880 看到你的编辑。 以下内容可用于重现类似枚举的行为。 stackoverflow.com/questions/956401/...
8 年多之前 回复
dos8244
dos8244 没有想到定义。 谢谢你的提及。 以下是相关的:stackoverflow.com/questions/5892226 / ...
8 年多之前 回复



我可能会先看一下多维数组,如果一个公司可以有很多相应的字母,但是退回是他们可能难以管理,你的另一个选择是定义常量 </ p>
</ div>

展开原文

原文

i would probably start by looking at multi dimentional arrays if a single company can have many corresponding letters, however the draw back being that they can get difficult to manage, your other option is to define constants



如果你有键值对,你可以把它们放到一个对象中,所以它更实用。</ p>

检查此链接: arrayToObject()函数 </ p>
</ div>

展开原文

原文

If you have key-value pairs you can put those into an object, so its more practical to use.

check on this link: the arrayToObject() function

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问