中括弧を使用する場合と使用しない場合の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>";
}
}
ブロックステートメントのコロン形式は、一般にテンプレート用に予約されています。明示的な 'endif'は閉じ中括弧よりも見やすいものです。これをコントローラ/クラスコードに使用しないでください。いくつかの標準化は良いです。 – halfer
@meagarこのことについて話を進めるにあたって最良の方法でそれを保留にしたりアドバイスしたりして、これを言い直すのを手伝ってもらえますか? – ShapCyber
@ShapCyber質問がすべての開発者にとって優先度の高いコード臭に関連しているので、すべての回答は意見があります。 – fyrye