Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What Does Using A Single Pipe '|' In A Function Argument Do?

Take for instance the following code:

phpinfo(INFO_MODULES | INFO_ENVIRONMENT | INFO_VARIABLES); 

A single argument is being used, but I am providing a list of options separated by a single pipe symbol.

  • What exactly is happening with the argument value in the function?
  • Can I use the same thing in my own functions?
  • Is so how, and are there benefits to this over say passing an array instead?
like image 899
cryptic ツ Avatar asked Dec 11 '12 00:12

cryptic ツ


People also ask

What does'|'mean in typescript?

it is also called union type in typescript. A value that can be any of numerous kinds is described by a union type. Each type is separated by a pipe (|), thus the number| string represents a value whose type can be a string or a number. or we can say either of the specified types is permissible.

What does single pipe mean in JavaScript?

A single pipe is a bit-wise OR. Performs the OR operation on each pair of bits. a OR b yields 1 if either a or b is 1. JavaScript truncates any non-integer numbers in bitwise operations, so its computed as 0|0 , which is 0.

What does pipe mean in PHP?

php pipe equals for number. 0. Receive Constant or Enumeration as Argument in PHP.

What are pipes in js?

The JavaScript Pipeline Operator ( |> ) is used to pipe the value of an expression into a function. This operator makes chained functions more readable. This function is called using ( |> ) operator and whatever value is used on the pipeline operator is passed as an argument to the function.


1 Answers

Bitwise operators

Bitwise operators modify the bits of the values involved. A bitwise OR basically ORs together each bit of both the left and right argument. For example:

5 | 2 

Would translate to bits/binary as:

101 | 10 

Which would result in:

111 

Because:

1 || 0 = 1 0 || 1 = 1 1 || 0 = 1 

And as an Integer that is the representation of 7 which is exactly what you get if you:

echo 5 | 2; 

In the words of Eddie Izzard... Flag!

As Ignacio states, this is most often used in PHP (and other langauges) as a way to combine multiple flags. Each flag is usually defined as a constant whose value is normally set to an integer that represents just one bit at a different offset:

define('FLAG_A', 1); /// 0001 define('FLAG_B', 2); /// 0010 define('FLAG_C', 4); /// 0100 define('FLAG_D', 8); /// 1000 

Then when you OR these together they operate each on their own bit offset and will never collide:

FLAG_A | FLAG_C 

Translates to:

1 | 100 

So you end up turning on:

101 

Which represents the integer 5.

Then all the code has to do—the code that will be reacting to the different flags being set—is the following (using a bitwise AND):

$combined_flags = FLAG_A | FLAG_C;  if ( $combined_flags & FLAG_A ) {   /// do something when FLAG_A is set }  if ( $combined_flags & FLAG_B ) {   /// this wont be reached with the current value of $combined_flags }  if ( $combined_flags & FLAG_C ) {   /// do something when FLAG_C is set } 

At the end of the day it just makes things easier to read by having named constants, and generally more optimal by relying on integer values rather than strings or arrays. Another benefit of using constants is that if they are ever mistyped when used, the compiler is in a better situation to tell and to throw a warning... if a string value is used it has no way of knowing that anything is wrong.

define('MY_FLAG_WITH_EASY_TYPO', 1);  my_function_that_expects_a_flag( MY_FLAG_WITH_EASY_TPYO );  /// if you have strict errors on the above will trigger an error  my_function_that_expects_a_flag( 'my_string_with_easy_tpyo' );  /// the above is just a string, the compiler knows nowt with  /// regard to it's correctness, so instead you'd have to /// code your own checks. 
like image 57
Pebbl Avatar answered Sep 20 '22 13:09

Pebbl