2

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?

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.