Home > Backend Development > PHP Problem > What is the usage of curly braces in php

What is the usage of curly braces in php

藏色散人
Release: 2023-03-12 12:22:01
Original
2417 people have browsed it

There are many ways to use php curly braces, such as: 1. "function name(){}" or "for(){}" usage; 2. "$str{4}" usage; 3. "{$val}" usage.

What is the usage of curly braces in php

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
Copy after login
######Another: The role of braces (braces {}) in PHP string variables#########Add after the PHP variable A curly bracket {}, filled with numbers, refers to the characters of the corresponding serial number of the PHP variable. ######For example: ######$str = 'hello';###
echo $str{0}; // 输出为 h ,也可以 $str[0]
echo $str{1}; // 输出为 e ,也可以 $str[1]
Copy after login
###If you want to check whether a string meets a certain length, you can consider using this kind of braces (curly braces ) to replace the strlen function by adding isset. Because isset is a language structure and strlen is a function, using isset is more efficient than using strlen. ######For example, to determine whether the length of a string is less than 5: ######if (!isset ($str{5})) is better than if (strlen ($str) < 5). ###### Recommended learning: "###PHP Video Tutorial###"###

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template