jol*_*olt 4 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 ^
// string(21) ""
Run Code Online (Sandbox Code Playgroud)
为什么#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 > (<) 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#?"
Run Code Online (Sandbox Code Playgroud)
在试验时,我发现了这个:
echo 'one' | 'two';
// returns 'o'
echo 'one' | 'twoe';
// returns 'oe'
Run Code Online (Sandbox Code Playgroud)
所以,看到在这两行中它只返回两个字符串中的字母,我认为它做了一些比较或其他:
echo 'i want you to compare me' | 'compare me with this';
#crazy-line // returns 'koqoveyou wotko}xise me'
Run Code Online (Sandbox Code Playgroud)
写这篇文章时,甚至发生了一些奇怪的事情.我复制了返回的值,并在将其粘贴到post textarea之后,当指针位于末尾时crazy-line,它实际上是右边的一个"空格"而不是它应该的位置.退格时,它会清除最后一个字符,但指针仍然是右边的一个"空格".
这导致我在Notepad ++中复制这个值:

而且,嗯,正如你所看到的,这个字符串中有一个"四四方方"的字符,不会出现在浏览器中(至少在我的,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 € ' | '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 € "
Run Code Online (Sandbox Code Playgroud)
最后一个值包含7个'boxy'字符.
它是按位OR运算符.
如果左侧和右侧参数都是字符串,则按位运算符将对字符的ASCII值进行操作.
资料来源:http://php.net/manual/en/language.operators.bitwise.php
这是一个按位OR运算符.它在字符串上的行为在这里解释: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
?>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2930 次 |
| 最近记录: |