Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pipe sign in PHP Code

Tags:

php

I wanted to concatenate 2 variables, and by error I typed another code and I got a strange result.

This is what looks like the code :

echo 'Hello World' | 'test';

Result : |e|o World

What the pipe sign do if not concatenated ?

like image 831
Hamza Avatar asked Mar 17 '12 12:03

Hamza


People also ask

What does pipe mean in PHP?

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

What does <? PHP mean in PHP?

What Does PHP Mean? The abbreviation PHP initially stood for Personal Homepage. But now it is a recursive acronym for Hypertext Preprocessor.

What does pipe mean in Haskell?

Pipe (" | ") symbol introduces a guard. Guards are evaluated top to bottom. the first True guard wins. otherwise in the Haskell system Prelude evaluates to true.


4 Answers

According to the PHP manual

"|" is a "bitwise OR". Bitwise operators allow evaluation and manipulation of specific bits within an integer.

 Example          Name                            Result
 $a | $b      Or (inclusive or)     Bits that are set in either $a or $b are set.

Example:

$a =    9;
$b =    10;
echo $a | $b;

This would output the number 11 as follows:

                                      1 Byte ( 8 bits )
Place Value            128    64    32    16    8    4    2    1
$a                       0     0     0     0    1    0    0    1    =    9
$b                       0     0     0     0    1    0    1    0    =   10
$a | $b                  0     0     0     0    1    0    1    1    =   11

If you notice we have 3 bits set, in the 8, 2, and 1 column.. add those up 8+2+1 and you get 11.

For mere string concatenation use the dot . operator.

Hope that clarifies it.

like image 61
Songo Avatar answered Oct 12 '22 11:10

Songo


It's the OR bitwise operator

If you want to concat string you should use dot

echo "ABC" . "DEF";

Example of OR bitwise usage

// base 16 - result in 0x03
$result = 0x01 | 0x02;

// base 2
0000 0001
0000 0010
---------
0000 0011
like image 42
Rezigned Avatar answered Oct 12 '22 11:10

Rezigned


That | means 'bitwise OR', which will convert the strings into binary, then overlay them on each other to calculate the result using logical OR for each position i.e. if either string has a 1 at that position, then the result will have a 1, otherwise, you'll get a 0.

In this case, it's doing this with the numerical ascii character codes of each character, which sometimes leads to new character codes and sometimes to garbage, which won't render. This is why the beginning of 'Hello world' is messed up, where it is overlaid with 'test', but the end is fine because it's not having any 1s added to it by another string at that point. See here for a more detailed example from the manual (uses XOR, but same idea).

Use . for concatenation.

like image 41
Matt Gibson Avatar answered Oct 12 '22 13:10

Matt Gibson


I would guess that it's a bitwise OR

like image 20
Rich Avatar answered Oct 12 '22 13:10

Rich