-1

I came across $string{0} here: https://github.com/bluerhinos/phpMQTT/blob/6c15cef0b1b2cb4c438e526c78d805b69a733f9d/phpMQTT.php#L154

I'm struggling to understand the {0} part of it. Does it mean the last character, the first character, the last digit in binary representation, the first digit in binary representation?

Is it still compatible with PHP 7.1?

wawa
  • 4,816
  • 3
  • 29
  • 52

3 Answers3

3

$string{0} is similar to $string[0], which will print the character at 0th index in $string variable.

ash__939
  • 1,614
  • 2
  • 12
  • 21
1

It is string access and modification by character. Since PHP 5.5 it also works with string literals.

axiac
  • 68,258
  • 9
  • 99
  • 134
  • 5.5 just introduced character access to a string *literal*, e.g. `"foo"[0]`. I think string variables have worked since, well, forever. – iainn Dec 12 '17 at 17:10
  • @iainn You're right, I didn't read the documentation careful enough on the first time. I corrected the answer. – axiac Dec 12 '17 at 17:13
1

Characters within strings may be accessed and modified by specifying the zero-based offset as in $str{42}

However, this syntax is deprecated as of PHP 6. Use square brackets instead.

Looks like the German manual needs an update; thx to iainn for pointing it out!

source

musashii
  • 445
  • 6
  • 13