PHP奇怪的按位运算符对字符串的影响 [英] PHP strange bitwise operator impact on strings

查看:101
本文介绍了PHP奇怪的按位运算符对字符串的影响的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,在阅读PHP文档之后,现在可以使用这些按位运算符了,但是,这是什么?

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) ""

为什么#dump2显示的长度== 21,但是0个字符?

Why #dump2 shows that length == 21, but 0 characters?

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

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 &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#?"

我希望看到与#dump2相关的问题得到解答,谢谢!!


在实验的过程中,我发现了这一点:

i'd love to see this #dump2 related question answered, thanks in advance!


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'

在撰写本文时,甚至发生了一些陌生的事情.我复制了该返回值,然后将其粘贴到文本区域中,当指针位于crazy-line的末尾时,它实际上是右边的一个空格",而不是它应该在的位置.退格时,它会清除最后一个字符,但指针仍然是右侧的一个空格".

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.

这导致我在记事本++中复制此值:

而且,呵呵,正如您所看到的,此字符串中有一个"boxy"字符,该字符不会显示在浏览器中(至少在我的Chrome,最新的浏览器中).是的,当从字符串中删除该字符(通过退格键)时,它将恢复为正常状态-右边不再有空格".

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.

那么,首先,PHP中的|是什么,为什么会有这种奇怪的行为?

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?

我很好奇为什么会这样,所以这里是对包含HTML实体的较长字符串的另一项测试:

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 &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 € "

最后一个值包含这些"boxy"字符中的7个.

Last value contains 7 of those 'boxy' characters.

推荐答案

这是按位或运算符.它在字符串上的行为在这里进行了解释: http://php.net/manual/zh-CN/language.operators.bitwise.php#example-107

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

这篇关于PHP奇怪的按位运算符对字符串的影响的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆