"Eğer" yazma farklı yolları nelerdir

7 Cevap

Koşullu ifadeler PHP kullanarak eğer yazma farklı yolları nelerdir?

Aşağıdaki örneğin bilmek

if($test == 1){
}else{
}

ve

if($test == 1)
   echo 'asdsa';
else
   echo 'sdaaa';

7 Cevap

Var alternative control structure syntax:

if ($text == 1):
    echo 'asdsa';
else:
   echo 'asdsa';
endif;

Diğer yol, bir ternary operator kullanmaktır. PHP belgelerinde örnek:

<?php
// Example usage for: Ternary Operator
$action = (empty($_POST['action'])) ? 'default' : $_POST['action'];

// The above is identical to this if/else statement
if (empty($_POST['action'])) {
    $action = 'default';
} else {
    $action = $_POST['action'];
}

?>

Diğer control structures are documented in the PHP manual de. Sadece koşullu ifadeler üçlü koşullu operatörü, if (ve else), ve elseif veya else if. Ancak, an alternative syntax var.

Zaten söyleniyor ne yanında, gibi şeyler var

$sql_link = mysql_connect('localhost', 'root') or die('no mysql');

Ya da benzeri Alternative syntax for control structures

(Atama "VEYA" hile gerçekten bir hile :) mysql_connect () true değerlendiririz değilse, PHP bu gerçekten bir kesmek ikinci ifadeyi değerlendirmeye çalışacağız:

if (mysql_connect('localhost', 'root')) {
    $sql_link = true;
}
else {
    die('no mysql');
}

)

"Koşullu Karmaşıklık" bir kod koku olduğunu unutmayın. Polymorphism senin arkadaşın.

Conditional logic is innocent in its infancy, when it’s simple to understand and contained within a few lines of code. Unfortunately, it rarely ages well. You implement several new features and suddenly your conditional logic becomes complicated and expansive. [Joshua Kerevsky: Refactoring to Patterns]

Bloklar kullanmayı öğrenmek için ise iç içe önlemek için yapabileceğiniz en basit şeylerden biri Guard Clauses. (Not: Bu PHP sözdizimi değil sözde kod gibi Regard burada teknikleri önemli ne vardır...)

double getPayAmount() {
if (_isDead) return deadAmount();
if (_isSeparated) return separatedAmount();
if (_isRetired) return retiredAmount();
return normalPayAmount();
};

Ben buldum başka bir şey oldukça iyi şeyleri kolaylaştırır, ve kod kendini documenting kılan, bir Consolidating conditionals.

double disabilityAmount() {
	if (isNotEligableForDisability()) return 0;
	// compute the disability amount

Koşullu ifadeleri ile ilgili diğer önemli refactoring teknikleri Decompose Conditional, Replace Conditional with Visitor içerir ve Reverse Conditional.

Şimdi bazı yeni çekiç var, her şey çivi gibi görünmeye izin vermeyin!

if($test == 1){
}else{
}

# can only be used if performing 1 line of code after statement
if($test == 1)
   echo 'asdsa';
else
   echo 'sdaaa';

#you can have as many elseif as you like (but you may wish to check out switch see below:
if($test == 1){
}elseif{
}else{
}

Ayrıca (anahtarı bakabilirsiniz) http://php.net/manual/en/control-structures.switch.php

switch($test)
{
    case "1" :
        break;
    case "2" :
        break;
    default :
        break;
}

Düz gelen PHP manual:

<?php
if ($a == 5):
    echo "a equals 5";
    echo "...";
elseif ($a == 6):
    echo "a equals 6";
    echo "!!!";
else:
    echo "a is neither 5 nor 6";
endif;
?>

If deyimi standart:

if(expression) {
    // code
} elseif(expression) {
    // code
} else {
    // code
}

Her cümleden sonra kod tek hatları için parantez olmadan:

if(expression)
    // single line of code
elseif(expression)
    // single line of code
else
    // single line of code

Alternatif kontrolü sözdizimi:

if(expression):
    // code
elseif(expression):
    // code
else:
    // code
endif;

Ve nihayet, üçlü operatörü:

(expression ? expression_if_true : expression_if_false);

: Olarak da yazılabilir hangi

(expression) ? expression_if_true : expression_if_false;

Yoksa parantez olmadan isterseniz tamamen eğer.