2017-04-30 4 views
0

中括弧を使用する場合と使用しない場合のPHPスニペットの使用の理由または利点があるかどうかは疑問です。PHPで中カッコを使用するかどうかの重要性や含意は何ですか?

私は数字の接尾辞を得るこれらの3つのアプローチを持っています。この場合には最適なアプローチすべきか

アプローチ1:「かっこ」「中括弧」

なし:「かっこ」「中括弧」

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>"; 

アプローチ2をなし

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; 

アプローチ3: "ブレース" "中括弧"

を利用します
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>"; 
} 
} 
+0

ブロックステートメントのコロン形式は、一般にテンプレート用に予約されています。明示的な 'endif'は閉じ中括弧よりも見やすいものです。これをコントローラ/クラスコードに使用しないでください。いくつかの標準化は良いです。 – halfer

+0

@meagarこのことについて話を進めるにあたって最良の方法でそれを保留にしたりアドバイスしたりして、これを言い直すのを手伝ってもらえますか? – ShapCyber

+0

@ShapCyber​​質問がすべての開発者にとって優先度の高いコード臭に関連しているので、すべての回答は意見があります。 – fyrye

答えて

1

その後コードはあなたの条件の一部として実行されるものを心配する必要はありません。あなたの将来の自己、または誰でもこのコードを維持して、愚かで予防できないエラーを起こさせないでください。ブレースと予期せぬものがなく、IFステートメントに余分な行を1つ追加するだけで、バグが発生しにくくなります。また読むのも簡単です。保守性は常に重要であり、無視すべきではありません。

+0

括弧なしオプションは、PHP、IMOから削除する必要があります。 – ceejayoz

関連する問題