Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Array-like statement with curly brackets - what is the purpose?

Tags:

php

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.

like image 916
NightHawk Avatar asked May 25 '11 19:05

NightHawk


People also ask

What is the purpose of curly brackets?

In writing, curly brackets or braces are used to indicate that certain words and/or sentences should be looked at as a group. Here is an example: Hello, please pick your pizza toppings {chicken, tomatoes, bacon, sausage, onion, pepper, olives} and then follow me.

How are curly braces used for an array?

The elements in cell arrays are cells. These cells can contain different types of values. With cell arrays, you can refer to the cells, or to the contents of the cells. Using curly braces for the subscripts will reference the contents of a cell; this is called content indexing.

What is {} used for in code?

Brackets, or braces, are a syntactic construct in many programming languages. They take the forms of "[]", "()", "{}" or "<>." They are typically used to denote programming language constructs such as blocks, function calls or array subscripts. Brackets are also known as braces.

What is the purpose of {} squiggly braces in Java?

Different programming languages have various ways to delineate the start and end points of a programming structure, such as a loop, method or conditional statement. For example, Java and C++ are often referred to as curly brace languages because curly braces are used to define the start and end of a code block.


3 Answers

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.

like image 156
mario Avatar answered Nov 15 '22 16:11

mario


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.

like image 20
Tripartio Avatar answered Nov 15 '22 15:11

Tripartio


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.

like image 20
Tadeck Avatar answered Nov 15 '22 16:11

Tadeck