Okay, after reading PHP documentation it's clear now with those bitwise operators, but, huh, what is this?
#dump1
var_dump('two identical strings' | 'two identical strings'); // mind the |
// string(21) "two identical strings"
#dump2
var_dump('two identical strings' ^ 'two identical strings'); // mind the ^
// string(21) ""
Why #dump2
shows that length == 21, but 0 characters?
When copied in Notepad++ there are no signs of characters inside the string, so, how come strlen > 0
? - this confuses me, because Notepad++ can show some kind of bit-level (at least I think that those are bit-level, correct me if I'm wrong) characters, see picture:
This is actually result from:
$string = 'i want you to compare me with an even longer string, that contains even more data and some HTML characters, like € ' ^ 'And I am going to add some HTML characters, like € again to this side and see what happens'; // mind the ^
var_dump(htmlentities($string)); // had to add htmlentities, otherwise > (<) characters are added in this case, therefore messing up the output - Chrome force closes `<body>` then
// string(101) "(NA'TAOOGCP MI<<m-NC C IRLIHYRSAGTNHEI RNAEAAOP81#?"
While experimenting, I found out this:
echo 'one' | 'two';
// returns 'o'
echo 'one' | 'twoe';
// returns 'oe'
So, seeing that in those two lines it returns only letters which are in both strings, I was thinking it does some comparison or something:
echo 'i want you to compare me' | 'compare me with this';
#crazy-line // returns 'koqoveyou wotko}xise me'
While writing this, even stranger stuff happened. I copied that returned value, and after pasting it into post textarea, when pointer is positioned at the end of crazy-line
, it is actually one "space" to the right not where it should be. When backspacing, it clears last character, but pointer is still one "space" to the right.
That lead me to copy this value inside Notepad++:
And, huh, as you can see there is a 'boxy' character within this string that doesn't show up inside browser (at least on mine, Chrome, latest). And yes, when this character is removed from that string (by backspacing), it returns back to normal - no more "space" to the right.
So, first, what is this |
inside PHP, and why there is such a strange behavior?
And what is this even stranger character, that looks like a box and doesn't show up in browser?
I'm pretty damn curious why this is happening, so here is one more test with longer strings containing HTML entities:
$string = 'i want you to compare me with an even longer string, that contains even more data and some HTML characters, like € ' | 'And I am going to add some HTML characters, like € again to this side and see what happens';
var_dump($string);
// returns string(120) "inwaota}owo}ogcopave omwi||mmncmwwoc|o~wmrl{wing}r{augcontuonwhmweimorendaweealawomepxuo characters, like € "
Last value contains 7 of those 'boxy' characters.
That's a bitwise OR operator. It's behavior on strings is explained here: http://php.net/manual/en/language.operators.bitwise.php#example-107
<?php
echo 12 ^ 9; // Outputs '5'
echo "12" ^ "9"; // Outputs the Backspace character (ascii 8)
// ('1' (ascii 49)) ^ ('9' (ascii 57)) = #8
echo "hallo" ^ "hello"; // Outputs the ascii values #0 #4 #0 #0 #0
// 'a' ^ 'e' = #4
echo 2 ^ "3"; // Outputs 1
// 2 ^ ((int)"3") == 1
echo "2" ^ 3; // Outputs 1
// ((int)"2") ^ 3 == 1
?>
its the bitwise OR operator.
If both the left-hand and right-hand parameters are strings, the bitwise operator will operate on the characters' ASCII values.
Source: http://php.net/manual/en/language.operators.bitwise.php
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With