I am just wondering if there are reasons or an advantage of writing PHP snippet with or without using curly braces.
I have these three approach of getting ordinal suffix of numbers. What should be the best approach in this case
Approach 1: Without "braces" the "curly brackets"
function ordinalSuffix($n) {
//any number such as zero(0) should end with 'th' other than 1,2,3 which must be 1st 2nd 3nd
$appends = array('th','st','nd','rd','th','th','th','th','th','th');
// using php modulus check if number is 11, 12 or 13
if ((($n % 100) >= 11) && (($n % 100) <= 13))
return $n. 'th'."<br/>";
else
return $n. $appends[$n % 10]."<br/>";
}
//Example Usage
$studentMarks=range(1,26);
foreach($studentMarks as $key => $studentResult)
//check for odd and even
if(($key % 2) == 1)
echo "<div class='dark'>Student - (". ($key + 1) .") Result is :".ordinalSuffix($studentResult)."</div>";
else
echo "<div class='ligth'>Student - (". ($key + 1) .") Result is :".ordinalSuffix($studentResult)."</div>";
Approach 2: Without "braces" the "curly brackets"
function ordinalSuffix($n) {
//any number such as zero(0) should end with 'th' other than 1,2,3 which must be 1st 2nd 3nd
$appends = array('th','st','nd','rd','th','th','th','th','th','th');
// using php modulus check if number is 11, 12 or 13
if ((($n % 100) >= 11) && (($n % 100) <= 13)):
return $n. 'th'."<br/>";
else:
return $n. $appends[$n % 10]."<br/>";
endif;
}
//Example Usage
$studentMarks=range(1,26);
foreach($studentMarks as $key => $studentResult):
//check for odd and even
if(($key % 2) == 1):
echo "<div class='dark'>Student - (". ($key + 1) .") Result is :".ordinalSuffix($studentResult)."</div>";
else:
echo "<div class='ligth'>Student - (". ($key + 1) .") Result is :".ordinalSuffix($studentResult)."</div>";
endif;
endforeach;
Approach 3: Makes use of "braces" the "curly brackets"
function ordinalSuffix($n) {
//any number such as zero(0) should end with 'th' other than 1,2,3 which must be 1st 2nd 3nd
$appends = array('th','st','nd','rd','th','th','th','th','th','th');
// using php modulus check if number is 11, 12 or 13
if ((($n % 100) >= 11) && (($n % 100) <= 13)){
return $n. 'th'."<br/>";
}
else{
return $n. $appends[$n % 10]."<br/>";
}
}
//Example Usage
$studentMarks=range(1,26);
foreach($studentMarks as $key => $studentResult){
//check for odd and even
if(($key % 2) == 1){
echo "<div class='dark'>Student - (". ($key + 1) .") Result is :".ordinalSuffix($studentResult)."</div>";
}
else{
echo "<div class='ligth'>Student - (". ($key + 1) .") Result is :".ordinalSuffix($studentResult)."</div>";
}
}