Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

php

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.

like image 404
dearlbry Avatar asked Nov 19 '12 10:11

dearlbry


1 Answers

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:

  • http://en.wikipedia.org/wiki/Idempotence
  • http://en.wikipedia.org/wiki/Bitwise_operation#OR

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

like image 159
Botond Balázs Avatar answered Sep 28 '22 02:09

Botond Balázs