PHP: What do the curly braces in $variable{0} do?

时光怂恿深爱的人放手 提交于 2019-12-17 20:36:11

问题


I was going through a codebase and came across a line I had a question about. It's something I haven't seen before and I was wondering if someone could explain it for me. Here's the code:

$variableName = $array[1];
$variableName{0} = strtolower($variableName{0});
$this->property = $variableName;

What are the curly braces being used for? I've used curly braces to define variables as variable names before, but is this the same thing? I can't seem to find any resources online that explain it, but I'm not sure if I'm searching for the right thing.


回答1:


access the single byte with that index {0} => first char (in non-utf8 string)

you could simply test it with:

$var='hello';
echo $var{0};



回答2:


It's setting the first character of the string to lower case. It's a string shortcut operator, functioning the same as this:

<?php
$variableName = strtolower(substr($variableName, 0, 1)) . substr($variableName, 1)



回答3:


Curly braces {} work the same as square brackets [], for array or string indexing. I'm guessing it is borrowed from perl, in which the square brackets are used for arrays and braces are used for hashes. But in PHP arrays and hashes are the same thing.



来源:https://stackoverflow.com/questions/9844185/php-what-do-the-curly-braces-in-variable0-do

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!