Home > Backend Development > PHP Tutorial > Some functions of braces in php

Some functions of braces in php

山海
Release: 2023-04-07 12:34:01
forward
2611 people have browsed it

Braces are the most common, most easily overlooked and most indispensable part of PHP. This article summarizes the three major functions of "{}" and hopes to be helpful to readers.

Some functions of braces in php

The three major functions of "{}" are:

Combine multiple independent statements into a compound statement, such as if ... else ... is often used like this in

to delimit variable indirect references to avoid ambiguity. For example, the difference between ${$my_var[8]} and ${$my_var}[8]

is used to indicate a single character in a string variable (the subscript starts from 0).

For example: "my_string" = "joker";

It seems that it is OK without adding it. What does adding {} mean?

Also Why should field names be included with ``?

At least it is easier to read~~~'' is required by the insert into statement, because strings must appear in pairs

Add {} Sometimes it is to prevent the variable name from being connected with the following string.

For example,

{$cid}dd

If cid=aa

then {$cid}dd=aadd

If you don’t add it, take a look at $ciddd yourself. Wouldn’t it become a ciddd variable~~

 //   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

Add a brace after the PHP variable { }, filled with numbers, refers to the characters of the corresponding serial number of the PHP variable.

For example:

$str = 'hello';

echo $str{0}; // The output is h

echo $str{1 }; // The output is e

If you want to check whether a string meets the specified length, you can consider using braces (braces) plus isset to replace the strlen function, because isset is a language structure, strlen is a function, so 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).

The following explanations are more illustrative:

means that {} is a variable, and it will be processed as a variable during execution

Reference variables in strings The special inclusion method is used so that the . operator can not be used, thereby reducing the amount of code input. In fact, the output block is equivalent to print "hello ".$arr['fruit'];

The function of curly brackets in php is as follows:

1. No matter what program, function name(){}, for(){},... there are too many, and I don’t even know what they are used for.

2. $str{4} is followed by {} after the string variable. Just like the curly brackets and square brackets, a certain string variable is treated as an array.

3. {$val}, the role of the curly brackets at this time is to tell PHP that the enclosed items should be treated as variables.

$arr=array(0=>123,&#39;name&#39;=>&#39;小猪&#39;);
 foreach($array as $k=>$v){
 echo "select * from blog_blogs where blog_tags like &#39;%{$arr[$k]}%&#39;order by blog_id"; //加一个大括号只是将作   为变量的标志符
 }
 echo &#39;<br/ ><br/><br/><br/><br/><br/><br/ >&#39;;
 foreach($array as $k=>$v){
 echo "select * from blog_blogs where blog_tags like&#39;%{{$arr[$k]}}%&#39; order by blog_id"; //加两个大括号,外面的 将作为普通的字符
 }
 //用大括号来区分变量
 //echo "$arr[&#39;name&#39;]";//用此句会报语法错误
 echo "{$arr[&#39;name&#39;]}";//此句正常,大括号内的字符将作为变量来处理
 //$str{4}在字符串的变量的后面跟上{}大括号和中括号一样都是把某个字符串变量当成数组处理
 $str = &#39;abcdefg&#39;;
 echo $str{4};
Copy after login

The above is the detailed content of Some functions of braces in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:csdn.net
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template