0

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>";
}
}
John Conde
  • 217,595
  • 99
  • 455
  • 496
ShapCyber
  • 3,382
  • 2
  • 21
  • 27
  • The colon form of block statements is generally reserved for templates, where an explicit `endif` is easier to spot than a closing brace. Don't use this for controller/class code - some standardisation is good. – halfer Apr 30 '17 at 19:32
  • @meagar can you help me to reword this as you have put it on hold or advice on best way to go about this. – ShapCyber Apr 30 '17 at 19:48
  • @ShapCyber all answers would be opinionated as the question is related to code-smell, which is preferentially unique to each developer. – Will B. Apr 30 '17 at 19:52
  • 1
    For use of the `colon` syntax, from the [PHP documents](http://php.net/manual/en/control-structures.alternative-syntax.php), `Mixing syntaxes in the same control block is not supported.` So it is best to standardize on using one style to prevent conflicting syntax issues. For `exclusion of braces` it is a readability issue and can result in needing to add them later as program requirements change. Ultimately it is generally best-practice to implement a coding standard, such as [PSR-2](http://www.php-fig.org/psr/psr-2/) – Will B. Apr 30 '17 at 19:56
  • This where to start from http://www.php-fig.org/psr/psr-2/ – ShapCyber Apr 30 '17 at 20:11

1 Answers1

1

Then you never have to worry about what code is executed as part of your conditional. Don't let your future self, or whomever maintains this code, make a stupid, preventable error. All it takes is adding one extra line to your IF statement without the brace and unexpected, hard to catch, bugs occur. It's also easier to read. Maintainability is always important and should not be ignored.

Pedro Lobito
  • 94,083
  • 31
  • 258
  • 268
John Conde
  • 217,595
  • 99
  • 455
  • 496