PHP奇怪的按位运算符对字符串的影响 更新。。搬到一家旅馆。

PHP奇怪的按位运算符对字符串的影响 更新。。搬到一家旅馆。,php,bitwise-operators,Php,Bitwise Operators,好的,在阅读了PHP文档之后,这些按位运算符现在已经很清楚了,但是,嗯,这是什么 #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 ^ // str

好的,在阅读了PHP文档之后,这些按位运算符现在已经很清楚了,但是,嗯,这是什么

#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) ""
为什么
#dump2
显示长度==21个字符,但为0个字符?

在Notepad++中复制时,字符串中没有字符的迹象,那么,为什么
strlen>0
?-这让我很困惑,因为Notepad++可以显示某种位级别的字符(至少我认为这些是位级别的,如果我错了请更正),请参见图片:

这实际上是由于:

$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 &gt; (<) 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#?"
在这两行中,它只返回两个字符串中的字母,我想它会做一些比较或其他事情:

echo 'i want you to compare me' | 'compare me with this';    
#crazy-line // returns 'koqoveyou wotko}xise me'
写这篇文章的时候,甚至发生了一些奇怪的事情。我复制了返回值,并将其粘贴到post textarea后,当指针位于
疯狂行的末尾时,它实际上是右边的一个“空格”,而不是它应该在的位置。当退格时,它清除最后一个字符,但指针仍然是向右的一个“空格”

这导致我将此值复制到记事本++:

而且,嗯,正如你们所看到的,这个字符串中有一个“长方体”字符,它不会出现在浏览器中(至少在我的Chrome浏览器上是最新的)。是的,当这个字符从该字符串中移除(通过退格)时,它会返回到正常状态-右侧不再有“空格”

那么,首先,PHP内部的
|
是什么,为什么会有如此奇怪的行为?

这个看起来像一个盒子却不显示在浏览器中的更奇怪的角色是什么?

我非常好奇为什么会发生这种情况,下面是一个关于包含HTML实体的较长字符串的测试:

$string = 'i want you to compare me with an even longer string, that contains even more data and some HTML characters, like &euro; ' | 'And I am going to add some HTML characters, like &euro; 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 € "

最后一个值包含7个“长方体”字符。

它是按位OR运算符

如果左手和右手都是 参数是字符串,按位排列 操作人员将对机器进行操作 字符的ASCII值


来源:

管道字符
|
用于按位包含或比较:

下面是关于如何处理字符串的前一个SO线程:


这是一个按位OR运算符。它在字符串上的行为解释如下:



也就是说,
'x'|'a'
相当于整个字符串的
chr(ord('x')| ord('a'))
,以此类推。可能有人嫉妒你是第一个。;)我必须扪心自问为什么php会有这样的功能。@丹尼尔,字符串也可以是二进制数据,比如C的
无符号字符*
数据类型。我想在某些情况下,您可能希望对它们执行位操作。由于信誉最低而被接受。
$string = 'i want you to compare me with an even longer string, that contains even more data and some HTML characters, like &euro; ' | 'And I am going to add some HTML characters, like &euro; 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 € "
<?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
?>