payment by upi: sinhamit@icici or payment by bank account name: amit kumar sinha, account number: 2646728782 IFSC code: KKBK0005660 SWIFT: KKBKINBB

Please support if you like my work by payment through upi: sinhamit@icici or payment by bank

account name: Amit Kumar Sinha,
account number: 2646728782
IFSC code: KKBK0005660
SWIFT: KKBKINBB


Conditional Blocks   in Category: PHP   by amit

🕙 Posted on 2023-05-31 at 10:08:16     Read in Hindi ...


Control Structures

    Till now, you have seen single-line or multi-line literals of different data-types, which are either assigned to a variable name, or some operation has been done by pre-defined functions, language constructs, etc. However, any PHP script is built out of a series of statements. These series of statements is called a BLOCK.

    Control Struc­tures are different kinds of CODE BLOCKS, which on passing some condition or some value, in form of literals or variable names, output desired results. For example, you can not directly display boolean values with echo function or print() function, in the web-browser, but with the help of these control structures, you can output desired results. Remember, var_dump() function not only outputs the boolean value, but also outputs the data-type of a literal or variable/constant name, along with the desired result.

    Let't examine following control structures one-by-one:

1) if() {}                                             ⇒ if block,

2) if() {} else {}                            ⇒ if-else block,

3) if() {} elseif() {} else {}   ⇒ if-elseif-else block,

4) if() {} else if() {} else {} ⇒ if-else if-else block − (else if are two words, having a whitespace between them),

5) while() {}                                      ⇒ while block,

6) do {} while()                               ⇒ do-while block,

7) for() {}                                          ⇒ for block,

8) foreach() {}                                 ⇒ foreach block,

9) switch() {}                                    ⇒ switch block,

10) match() {}                                     ⇒ match block,

11) declare() {}                                 ⇒ declare block,

12) break and/or continue − can be multi-level by post-fixing integer value inside parentheses.

13) return − it returns program control to the calling module. Execution resumes at the expression following the called module's invocation.

14) require and/or include,

15) require_once and/or include_once, and

16) goto − The goto operator can be used to jump to another section in the program. This is not a full unrestricted goto.

if   if-else   if-elseif-else

    if(){},     if(){} else{},   and   if(){} elseif(){} else{} are conditional blocks, which are frequently used in PHP and other program­ming languages. When a conditional expression placed inside () paren­theses just after if or elseif is true, then the statements inside {} curly braces are executed. Otherwise, the PHP inter­preter passes the control to next line or next block of code, and so on... Let't elaborate this with following example:

<?php if( true ) { echo "TRUE"; } // Outputs TRUE ?><br />

<?php if( true ) echo "TRUE"; // Outputs TRUE ?><br />

    When there is only one statement (usually terminated by ; semi-colon) after the condit­ional expression (inside the paren­theses), the curly braces are not required as shown above. Also, when the statement is just before the ?> (closing PHP tag), the terminator ; (semi-colon) is not necessary. You should not place single-line comments before the closing curly brace, otherwise ERROR will be displayed.

<?php if( true ) { echo "TRUE"; // Outputs ERROR as shown below: } ?>


Parse error: Unclosed '{' on line 1 in C:\xampp\htdocs\php_project\phptest.php on line 1

<?php
    if( true ) {
        echo 'TRUE'; // No ERROR will be shown
    }
?>

    Anything which is evaluated as true inside the () paren­theses will execute the codes inside the {} curly braces or a single statement after it. When the condit­ional expression is false, the codes inside the curly braces of if(){} block, are not executed and the PHP interpreter will go to next line of (at the bottom) of scripts. The else {} block is provided to execute the codes inside curly braces when the conditional expression is false.

<?php
    if( 0 ) {
        echo 'TRUE';
    } else {
        echo 'FALSE';
    }
?>

    In the above example, the output is FALSE, because the condi­tional expression inside the paren­theses is 0 (zero), which is evaluated as false. You should note herein that outputs TRUE and FALSE from all above examples, are not boolean values, but the string data-types. You can these as 'false repre­senta­tion' of boolean values!

    You can place comparison operators, logical operators, bitwise operators, etc. inside the conditional expression, to make it more dynamic. Also, you can evaluate variables and constants of any data-type inside paren­theses, as true or false. elseif or else if (two words with a blank space between them) are similar and have same effect. But the latter has slight differ­ence with former, in some examples, which will be described later.

<?php
    if( 0 ) {
        echo 'ZERO';
    } elseif( 1 ) {
        echo 'ONE';
    } else {
        echo 'TWO';
    }
?>

    In the above example, the output is ONE. The first block of code inside curly braces after if(0) will not be exeucted, because 0 (zero) is evaluated as false. The second block of code inside curly braces after elseif(1) will be executed, because 1 is evaluated as true. You can write the above script with else if keywords and the result is same as explained above.

<?php
    if( 0 ) {
        echo 'ZERO';
    } else if( 1 ) {
        echo 'ONE';
    } else {
        echo 'TWO';
    }
?>

    The third block of code and the else keyword is not necessary, and can be avoided, when you want to check two conditions. The else { } block is known as default block that is, when all conditional expressions are evaluated as false, it will be executed.

<?php
    if( 0 ) {
        echo 'ZERO';
    } elseif( 1 ) {
        echo 'ONE';
    }
?>

    You can write the above examples as shown below, but following code has three if(){} blocks. In all above examples of if(){} elseif(){} else{} only one block of codes will be executed, either any one condit­ional expression inside paren­theses is true or all condi­tional expressions are false. But in the following example, there are two or more if(){} or if(){} else{} blocks, which are not connected with each other. In this case (of below example,) when all condi­tional expressions inside paren­theses are true, all blocks of codes will be executed.

<?php
    if( 1 ) {   echo 'one';   }     if( 2 ) {   echo 'two';   }     if( 3 ) {   echo 'three';   }
?>

    The literals 1, 2 and 3 are placed inside paren­theses in above example to explain you that all conditional expressions are evaluated as true. If data received inside paren­theses is 0, '' (empty string), [] (empty array), null, etc. or anything which will be evaluated as false, the respective block of code inside curly braces will not be executed. Indentation is not important in PHP, except in some case, for example, heredoc and nowdoc strings. Therefore, the output of above example will be:

onetwothree

NESTED if-elseif-else

    You should carefully insert blank spaces and <br /> HTML element inside a pair of single/double quotes as you can seen in above example, the output has no blank space or line-break! Similarly, you can place as many elseif(){} blocks of code in between first if(){} block and last else{} block. A complex example of all possible NESTED if-elseif-else control structure is shown below:

<?php
  if( 4 < 3 ) {
    echo 'Condition 1 is true.<br />';
    if( [] ) { echo 'Nested condition 1 is true.<br />'; }
    elseif( "" ) { echo 'Nested condition 2 is true.<br />'; }
    else { echo 'Nested conditions are not true.<br />'; }
  } elseif( 3 === 3.0 ) {
    echo 'Condition 2 is true.<br />';
    if( 4 > 6 ) { echo 'Nested condition 3 is true.<br />'; }
    elseif( ! true ) { echo 'Nested condition 4 is true.<br />'; }
    else { echo 'Nested conditions in elseif(!true) block are not true.<br />'; }
  }
  else echo 'All conditions are false. Only one block of code will be executed.';
?>

    The above example is only an illustration, and can be much more complex and bigger. In the nested condition 4, ! (not operator) is used, which flips the evaluated conditional expression to opposite of it. Thus, true will become false, and much more behaviours can be expected when using ! operator. You will know about other operators in next pages.


Leave a Comment: