Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
152 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 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...