Decision Structures



The most important aspect of a programming language is the decision making. Decisions are used to guide the flow of code in a program. A kind of programa is the element used to add intelligence. It specifies how a code should behave in what case. Especially used with comparison operators. It directs code flow depending on the state of a condition. It is these decision-making mechanisms that have already programmed a program. Just as in many different programming languages, the PHP programming language uses if-elseif-else and switch-case constructs as decision making mechanisms .

 

 

# Section Headings


# If - Elseif - Else

 Short If - Hand Use

# Switch - Case

 

 

# If - Elseif - Else


The syntax of the use of this structure is shown below.

if( $kosul )
{
    # Koşulun true dönmesi durumunda bu bölümdeki kodlar çalışır.
}
elseif( $kosul2 )
{
    # 1. koşul false ise bu koşula bakılır. Bu koşulun true dönmesi durumunda
    # bu bölümdeki kodlar çalışır.
    # Bu bloğun kullanımı zorunlu değildir.
}
else
{
    # Yukarıda belirtilen koşuların hiç birinin true dönmemesi durumunda
    # bu bölümdeki kodlar çalışır.
    # Bu bloğun kullanımı zorunlu değildir.
}

Let's give some examples for better understanding of the subject.

$kullanici = 'ZN';
$sifre     = '1234';

if( $kullanici === 'ZN' &&  $sifre === '1234' )
{
    echo 'Kullanıcı adı ve şifre doğru.';
}
else
{
    echo 'Kullanıcı adı veya şifre yanlış!';
}
The username and password are correct.

When the above code is examined, it is indicated how the program should behave according to the condition of the condition stated. Now we can say that we are slowly entering the programming.

Short If - Hand Use

With a 3-way comparison operator, you can create a decision-making mechanism in a shorter time.

$kullanici = 'ZN';
$sifre     = '1234';

echo ( $kullanici === 'ZN' &&  $sifre === '1234' )
     ? 'Kullanıcı adı ve şifre doğru.'
     : 'Kullanıcı adı veya şifre yanlış!';
The username and password are correct.

 

 

# Switch - Case


this decision structure, unlike if-else, allows a decision to be made based on a key that matches a value rather than a comparison. An if-else mechanism can do any work the switch-case can do, but not the other way around. The syntax is as follows.

switch( $anahtar )
{
    case 'değer1': 
        # Anahtar ile değerin eşleşmesi durumunda bu bölüm çalışır.
    break;

    case 'deger2':
        # Anahtar ile değerin eşleşmesi durumunda bu bölüm çalışır.
    break;

    default:
        # Hiç bir eşleşme sağlanamazsa bu bölüm çalışır. 
        # Bu bloğun kullanımı zorunlu değildir.
}

Let's give some examples for better understanding of the subject.

$sehir = 'istanbul';

switch( $sehir )
{
    case 'istanbul': echo 'Burası istanbul.'; break;
    case 'izmir'   : echo 'Burası izmir.';    break;

    default: echo 'Burası ankara.';
}
this is istanbul.

It can also be used as follows to run the same block according to more than one situation.

$sehir = 'çorum';

switch( $sehir )
{
    case 'istanbul':
    case 'ankara'  :
    case 'çorum'   :
    case 'izmir'   : echo 'Burası Türkiye.'; break;

    default: echo 'Burası Türkiye değil!';
}
this is Turkey.

It is important to understand how the switch statement works to avoid errors. The switch statement line is handled line by line (actually an idiom). Initially, no code is executed. Only when the case statement matches the value supplied with the switch statement, PHP starts to execute the related statements. The PHP switch continues to run until the block expires or until the first break statement is encountered. If you do not break at the end of a case block, PHP will continue to run the code from the next case statement.  

$sehir = 'istanbul';

switch( $sehir )
{
    case 'istanbul': echo 'Burası istanbul.';
    case 'izmir'   : echo 'Burası izmir.';  

    default: echo 'Burası ankara.';
}
this is istanbul.
This is izmir.
This is ankara.

In a switch statement, the condition is queried only once, and the result is compared to each case statement. In an elseif statement, the condition is re-queried. If the condition you want is more complicated than a simple comparison and / or in a loop, using a switch may be faster.