Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
105 views
in Technique[技术] by (71.8m points)

php - What is this bitwise OR doing in this weird array key construct?

Can someone explain to me what this means?? I have never seen this construct - taken from the Prestashop doc

foreach ( $languages as $language )
{
  echo '<div id="test_' . $language['id_lang'|'id_lang'] .... // <-- What the??
  // ... 
}

$language contains the following keys:

Array
(
    [id_lang] => 1
    [name] => English (English)
    // and others... 
)

The result is that it takes the value of $language["id_lang"] - 1. But I don't understand the syntax and can't find any documentation about it.

question from:https://stackoverflow.com/questions/13452247/what-is-this-bitwise-or-doing-in-this-weird-array-key-construct

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This php -a session shows that it's totally meaningless:

php > $value = 'something'|'something';
php > echo $value;
something
php > $arr = array('abc' => 1, 'def' => 2);
php > echo $arr['abc'|'abc'];
1
php > echo $arr['def'|'def'];
2

Basically, if you "bitwise or" anything by itself, you get the original value. This property is called idempotence in mathematics. For further info, read:

Honestly, the original author of that code had no idea what they were doing.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...