Php Lesson 5 – Conditions

In programming conditions, the situations we mentioned real (true) to be or not to be (false) It is used to determine the codes that will work in case of

If/Else/ElseIf

If, the condition inside the parentheses true if is specified, the specified operation and if is specified again false It is the fiction that performs the specified action in the event of a problem. Its usage is as follows;

if( Condition ) {
process
} elseif ( Else Condition) {
process
} else {
process
}

<?php
define("TAM_ISMI_KULLAN", FALSE);

$isim = "Erhan";
$soyad = "Kılıç";

if(TAM_ISMI_KULLAN == TRUE){ // (Burada oluşturduğumuz sabitin trueWe have stated the condition that it must be equal to . If an equality condition even if we don't put it condition we put in parentheses variable, still or something else trueChecks whether it is equal to PHP. $fullname= $name. " " . $surname; // If here, use the full name our constant If true, name and surname variables } else{ // Else usage is not mandatory. $fullname = $name; // If true if not, it's not, it's just a name variable I am full with variable We are creating. } echo "Hello Everyone!!"; echo $name; ?>

Sometimes we may want to specify the cases where our condition is negative. If we expand the example above, if FULL_NAME_USE constant create this constant false We might want to write code that will work when there is a problem. In that case, we would do something like the following:

<?php
define("TAM_ISMI_KULLANMA", FALSE);

$isim = "Erhan";
$soyad = "Kılıç";

if(!TAM_ISMI_KULLANMA){ // (Burada ise başına ! işareti koyarak false olduğu durumu koşul olarak belirtiyoruz. Temelde şu şekilde de yapabilirdik: if(TAM_ISMI_KULLANMA == FALSE). Aslında false eşitliği arasak bile koşul false because it is PHP this true and performs the necessary action. False We set the condition to be and false our condition when true $fullname = $name; } else{ $fullname= $name. " " . $surname; } echo "Hello everyone!!"; echo $name; ?>

Switch/Case

Switch/Case whereas ifUnlike , if one of the conditions we specify works, it will run the valid code. For example, if there are more than 2-3 conditions that a variable can have, if If we try to write with many Elseif We have to write. We can create many conditions more easily with Switch/Case.

Usage structure;

switch (n) { // n This is where the condition is queried. It could be anything.
case label1:
      codes
break; // The reason we use break is if condition even if it is correct and the codes work PHP other conditionIt is to ensure that it exits as it continues to test whether the values are correct.
case label2:
      codes
break;
case label3:
      codes
break;

default: // default if there is no condition It is used to specify the codes to be run when this does not happen. It is not mandatory.
      codes
}

Example;

<?php

$islem = "toplama";
 $a = 1;
 $b = 10;

switch ($islem) {
    case 'toplama':
        echo $a + $b;
        break;
    case 'bölme':
        echo $a / $b;
        break;
    case 'çarpma':
        echo $a * $b;
        break;
    case 'çıkarma':
        echo $a - $b;
        break;
}

?>

 

0 0 votes
Article Rating
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments