[英]How to check does array key exist in defined constant array [PHP 7 define()]
PHP7 brought possibility define array constants with define(). PHP7带来了使用define()定义数组常量的可能性。 In PHP 5.6, they could only be defined with const.
在PHP 5.6中,它们只能用const定义。
So I can use define( string $name , mixed $value ))
to set array of constants, but it seems that it forgot to bring also upgrade of defined ( mixed $name )
along since it still only accepts string
value or am I missing something? 所以我可以使用
define( string $name , mixed $value ))
来设置常量数组,但似乎它忘记带来defined ( mixed $name )
升级,因为它仍然只接受string
值或者我错过了什么?
PHP v: < 7
I had to define every animal separately define('ANIMAL_DOG', 'black');
PHP v: < 7
我必须定义每个动物分别define('ANIMAL_DOG', 'black');
, define('ANIMAL_CAT', 'white');
,
define('ANIMAL_CAT', 'white');
etc. or serialize my zoo . 等或序列化我的动物园 。
PHP v: >= 7
I can define entire zoo which is freaking awesome, but I can't find my animal in the zoo as simply I can find single ANIMAL. PHP v: >= 7
我可以定义整个动物园 ,这是非常棒的,但我在动物园里找不到我的动物,因为我可以找到单一的动物。 It is reasonable in the real world, but here's supplementary question if I haven't miss something. 这在现实世界中是合理的,但如果我没有遗漏某些东西,这里是补充问题。
Is that intentional that defined(); 这是故意定义的(); does not accept array?.
不接受数组? If I define my zoo...
如果我定义我的动物园......
define('ANIMALS', array(
'dog' => 'black',
'cat' => 'white',
'bird' => 'brown'
));
... why can't I find my dog simply defined('ANIMALS' => 'dog');
...为什么我不能简单地
defined('ANIMALS' => 'dog');
我的狗defined('ANIMALS' => 'dog');
? ?
1. Prints always: The dog was not found
1.始终打印:
The dog was not found
print (defined('ANIMALS[dog]')) ? "1. Go for a walk with the dog\n" : "1. The dog was not found\n";
2. Prints always: The dog was not found
and when dog really does not exist shows Notice + Warning 2.始终打印:
The dog was not found
狗,当狗确实不存在时显示通知+警告
/** if ANIMALS is not defined
* Notice: Use of undefined constant ANIMALS - assumed ANIMALS...
* Warning: Illegal string offset 'dog'
* if ANIMALS['dog'] is defined we do not get no warings notices
* but we still receive The dog was not found */
print (defined(ANIMALS['dog'])) ? "2. Go for a walk with the dog\n" : "2. The dog was not found\n";
3. regardless of whether the ANIMALS
, ANIMALS['dog']
is defined or not, I get Warning: 3.无论是否定义了
ANIMALS
, ANIMALS['dog']
,我都会收到警告:
/* Warning: defined() expects parameter 1 to be string, array given...*/
print defined(array('ANIMALS' => 'dog')) ? "3. Go for a walk with the dog\n" : "3. The dog was not found\n";
4. I get Notice if ANIMALS['dog']
is not defined 4.我得到通知,如果
ANIMALS['dog']
没有定义
/* Notice: Use of undefined constant ANIMALS - assumed 'ANIMALS' */
print (isset(ANIMALS['dog'])) ? "4. Go for a walk with the dog\n" : "4. The dog was not found\n";
5. So am I correct that there is only one option left then? 5.我也是纠正只有一个选择离开呢?
print (defined('ANIMALS') && isset(ANIMALS['dog'])) ? "Go for a walk with the dog\n" : "The dog was not found\n";
PHP 7 allows you to define
a constant array, but what is being defined as a constant in that case is the array itself , not its individual elements. PHP 7允许您
define
一个常量数组,但在这种情况下被定义为常量的是数组本身 ,而不是它的各个元素。 In every other regard the constant functions as a typical array, so you'll need to use conventional methods to test for the existence of a specific key within it. 在其他方面,常量函数作为典型数组,因此您需要使用传统方法来测试其中是否存在特定键。
Try this: 尝试这个:
define('ANIMALS', array(
'dog' => 'black',
'cat' => 'white',
'bird' => 'brown'
));
print (defined('ANIMALS') && array_key_exists('dog', ANIMALS)) ?
"Go for a walk with the dog\n" : "The dog was not found\n";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.