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 ?
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 ?
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.
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
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.