Reputation: 5848
In the code like this:
<?php
$a = 'абв';
for($i = 0; $i < strlen($a); $i++)
{
echo $a[$i]>>6, ' ';
}
The output is 0 0 0 0 0 0
, which is not what is expected. The expected output is values 3 and 2, depending on whether we are in a beginning or the end of the UTF-8 character.
What is a problem?
Upvotes: 2
Views: 116
Reputation: 5848
Although PHP provides many functions that are a simply wrapper around their C counterparts, the developers have decided to add an extra check to the bit shift operators. In PHP shift-left and shift-right always return 0 (not even false, which would make some sense).
In order to bit-shift a character, it is necessary to wrap it with ord
function:
<?php
$a = 'абв';
for($i = 0; $i < strlen($a); $i++)
{
echo ord($a[$i])>>6, ' ';
}
This produces: 3 2 3 2 3 2
as expected.
Upvotes: 2