Usage of Brackets
In PHP round brackets, square brackets and curly brackets are in use. Round brackets are common in expressions and functions. Squire brackets are used in arrays. Curly brackets are used to mark function, loop and control structure bodies. Curly brackets can also be used within strings to separate variables from surrounding text.
Round Brackets ()
This type is so common in PHP. They are used in expressions, functions, control structures and loops.
/* In expressions */
$d = ($a+$b)*$c; // Brackets are in use to make sure that addition happens first
/* In functions */
function getFullName() {
}
/* In control structure */
if ($a < $b) {
}
/* In Loops */
foreach ($names as $name) {
}
Square Brackets []
Square brackets are used in array elements. Array key is mentioned within square brackets.
$fruits = array('Mango', 'Apple', 'Banana');
echo $fruits[0];
Curly Brackets {}
In above examples you can see how curly brackets are used to mark bodies of functions, control structures and loops. It’s commonly seen in PHP. What you would find bit uncommon is the usage of curly brackets within strings.
$verb = 'add'; echo "Present tense of this verb is $verb";
Think that you want to display the past tense of the verb without redefining it (just by adding ‘ed’).
echo "Past tense of this verb is $verbed";
If you tried above way then PHP would search for variable $verbed and throw an error (since it’s not defined). To separate the verb from suffix ‘ed’, you can use curly brackets as below.
echo "Past tense of this verb is {$verb}ed";
