Usage of Brackets
In PHP, round brackets, square brackets and curly brackets are in use.
Round Brackets ()
Round brackets are 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 arrays elements. Array key is written within square brackets.
$fruits = array('Mango', 'Apple', 'Banana'); echo $fruits[0];
Curly Brackets {}
Curly brackets are used to mark class, function (methods in OOP terminology), loop and control structure bodies.
They can also be used within strings to separate variables from surrounding text.
$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";
If $verb is an array, an element of it can be used like below.
echo "Past tense of this verb is {$verb['past_tense']}";
If $verb is an object and has a method called getPastTense() that returns past tense of the verb, it can be used like below.
echo "Past tense of this verb is {$verb->getPastTense()}";