11

I found this snippet of code online and I was wondering what it does:

$k[$i] = ord($key{$i}) & 0x1F;

I know that ord() returns an ASCII value, but I'm unclear on what the curly brackets do $key{$i} and also what this does & 0x1F.

Billy ONeal
  • 97,781
  • 45
  • 291
  • 525
NightHawk
  • 3,380
  • 8
  • 34
  • 54

4 Answers4

8

That's not an array syntax. It's for accessing single characters from strings only. The index must be numeric.

 $str{1} == $str[1]

It's briefly mentioned here in the info box: http://www.php.net/manual/en/language.types.string.php#language.types.string.substr

Also, it's officially declared deprecated. It has been on and off supposed deprecation in the manual, but is still very valid, if uncommon, syntax.


The & 0x1F is a bit-wise AND. In this case it limits the decimal values to 0 till 31.

mario
  • 138,064
  • 18
  • 223
  • 277
  • 1
    This answer is inaccurate. In fact, as I explain in my answer, the curly bracket syntax is a fully valid (not deprecated) alternative to square bracket syntax. The reference that mario links to only underscores my point--the two forms are equivalent. – Tripartio Feb 01 '14 at 20:34
  • 1
    @Ochado Good catch. That's no longer relevant. Previous versions of the manual *did* [label it as deprecated for PHP 5.3](https://core.trac.wordpress.org/ticket/13900), and once before pertaining 6.0 as seen [in the DE-version](http://www.php.net/manual/de/language.types.string.php). So better duplicate: [PHP $string{0} vs. $string\[0\];](http://stackoverflow.com/q/335205) (Personally I've also secretly still been using the `{1}` syntax for decorative purposes, btw.) – mario Feb 01 '14 at 20:47
  • 1
    The index does **not** need to be only numeric. PHP will do a conversion automatically. – Pacerier Nov 07 '14 at 20:47
  • @Pacerier: I guess I wanted to imply [`is_numeric("1")`](http://php.net/is_numeric). But you're right; it's more of a custom int cast/cut for string indicies. – mario Nov 07 '14 at 20:53
3

In PHP, either [square brackets] or {curly braces} are fully legal, interchangeable means of accessing an array:

Note: Both square brackets and curly braces can be used interchangeably for accessing array elements (e.g. $array[42] and $array{42} will both do the same thing in the example above).

Source: http://www.php.net/manual/en/language.types.array.php

Both are fully valid; curly braces are not deprecated.

Concerning "& 0x1F", others like @Tadek have responded:

& is a binary operator. See more in the documentation [http://www.php.net/manual/en/language.operators.bitwise.php]. It sets the bits if they are set in both variables.

Tripartio
  • 1,617
  • 1
  • 20
  • 26
  • 1
    Consider of removing comments on other answers from your own answer. – bummi Jul 24 '13 at 06:33
  • I've edited this answer to remove your comment about the accepted answer (which might not always *be* the accepted answer). Consider downvoting and/or commenting on other answers if you think they're correct. If you don't have enough reputation to comment, just let your answer speak for itself. – Caleb Jul 24 '13 at 06:41
  • Thanks for the comments and for the edit. As I'm new to contributing to Stack Overflow, I appreciate the pointers. – Tripartio Jul 24 '13 at 15:44
3

First part of your question: curly braces

This is about referencing the value of the variable.

In your example, if $i is equal to 123, then

$k[$i] = ord($key{$i}) & 0x1F;

will be the same as

$k[123] = ord($key[123]) & 0x1F;

Second part of your question: '&' sign

& is a binary operator. See more in the documentation. It sets the bits if they are set in both variables.

Tadeck
  • 117,059
  • 25
  • 140
  • 191
2

The & 0x1F is a bit-wise operation, used to "normalize" values of characters, colors, etc, that are put into hex but aren't necessarily "read" the same. Read more on Wikipedia here.

& 0x1f

is equivalent to

& 00011111

This can effective set the first three bits of any byte-sized value to 0.

0xff & 0x1f

results in

0x1f

or

11111111 & 00011111

results in

00011111
Joe
  • 10,698
  • 7
  • 47
  • 63