There are many ways to use php curly braces, such as: 1. "function name(){}" or "for(){}" usage; 2. "$str{4}" usage; 3. "{$val}" usage.
The operating environment of this article: windows7 system, PHP7.1 version, DELL G3 computer
# What is it?
Detailed explanation of the use of PHP braces (curly braces {})1. No matter what program, function name(){}, for(){ }, ....This is too much, and I don’t even know what it is used for.
2. $str{4} is followed by
{}after the variable of the string. Braces and square brackets
[]both replace a certain character. String variables are treated as arrays.
3. {$val} is the problem I encountered. At this time, the role of the curly brackets is to tell PHP that the enclosed items should be treated as variables. The following example:
//The following is okay as it's inside a string. Constants are not //looked for within strings so no E_NOTICE error here print "Hello $arr[fruit]"; // Hello apple //With one exception, braces surrounding arrays within strings //allows constants to be looked for print "Hello {$arr[fruit]}"; // Hello carrot print "Hello {$arr['fruit']}"; // Hello apple
echo $str{0}; // 输出为 h ,也可以 $str[0] echo $str{1}; // 输出为 e ,也可以 $str[1]
The above is the detailed content of What is the usage of curly braces in php. For more information, please follow other related articles on the PHP Chinese website!