Several common ways of using braces in php code

user:visitors Date:2022-03-07 16:25:451465

In different programming languages, curly brackets {} are commonly used symbols. This article briefly introduces the common usage of curly brackets (curly brackets) in php.

1. The beginning and end of a sentence

function xxxxx(){

...

}


for(){

...

}

For example, define a function xxxxx, or perform actions such as for loops.


2. Treat strings as arrays.

Example:

<?php

$zfc = 'abcde';

echo $zfc{2};

?>

The braces also have the same function as the square brackets [] to call the string as an array. You can also use each character in the string 'abcde' as an element in the array to directly start from 0 through the braces to call the specified digit number index. character.

We can also use this method to determine whether the specified string is greater or less than the character length value.

<?php

$zxzf="a1b2c3"

if (isset($zxzf{2})){}

if (strlen($zxzf)< 3){}

?>

Since isset is more efficient than strlen, it is better to use isset($zxzf{2}) to determine whether the character reaches the specified length.


3. Force the contents of curly brackets to be treated as variables.

Example:

<?php

$dkh['a']="xbl";

echo $dkh['a'];

?>

The above example can output the value of index a in the array $dkh is xbl, and then we add curly brackets to $dkh['a'] to see what effect it has.

<?php

$dkh['a']="xbl";

$xbl="123";

echo {$dkh['a']};

?>

After adding braces, we will find that the output value becomes 123. After adding braces, $dkh['a'] turns the original value xbl into the variable $xbl. When outputting, the variable $xbl is output. value of .

The above are some of the more common uses of curly brackets in php code.

Popular articles
latest article