if


php128 apg

PROVIDES conditions for DECISION TAKING at runtime when you have only one expression to evaluate.

Is a very important language construction, because it allows decision making during the programming process.

In PHP its structure is similar to the C language.

In the following, we will show the first type of conditional structure:  if .



SINGLE STATEMENT


In the following, we will show the SINGLE STATEMENT of -  if  - conditional structure:



<?php

// SINGLE EXPRESSION
// SINGLE STATEMENT

if (expr 1)
    
statement 1

where
,

expr 1 THE EXPRESSION TO BE EVALUATED

and

statement 1 EXECUTED IF expr 1 evaluates to TRUE
                     IGNORED  
IF expr 1 evaluates to FALSE

?>

SINGLE STATEMENT (USUAL FORMAT)


In the following, we will show the SINGLE STATEMENT (USUAL FORMAT) of -  if  - conditional structure:



<?php

// SINGLE EXPRESSION
// SINGLE STATEMENT

if (expr 1)
    {
    
statement 1
    
}

where,

expr 1 THE EXPRESSION TO BE EVALUATED

and

statement 1 EXECUTED IF expr 1 evaluates to TRUE
                     IGNORED  
IF expr 1 evaluates to FALSE
?>

if


The result of this conditional evaluation is always a BOOLEAN type.

The  expr 1  will be evaluated:

01 .  statement 1  will be executed...

      If the  expr 1  is verified as TRUE

02 .  statement 1  ignored...

      If every time  expr 1  is verified as FALSE.



if ... else


PROVIDES conditions for DECISION TAKING at run time when you have two expressions to evaluate.

Is a very important language construction, because it allows decision making during the programming process.

In PHP its structure is similar to the C language.

In the following, we will show the second type of conditional structure:  if ... else .



<?php

// TWO EXPRESSIONS
// TWO STATEMENTS

if (expr 1)
    {
    
statement 1
    
}
else
    {
    
statement 2
    
}
    
    
where,

expr 1 Expression 1 to be EVALUATED

expr 2 
Expression 2 to be EVALUATED


and


statement 1 Executed IF expr 1 evaluates to TRUE

statement 2 
Executed IF expr 1 evaluates to FALSE

?>

if ... else


The result of this conditional evaluation is always a BOOLEAN type.

The  expr 1  will be evaluated:

01 .  statement 1  will be executed...

      If the  expr 1  is verified as TRUE

02 .  statement 2  executed...

      If every time  expr 1  is verified as FALSE.



if ... elseif ... else


PROVIDES conditions for DECISION TAKING at run time when there are more than two expressions to evaluate.

Is a very important language construction, because it allows decision making during the programming process.

In PHP its structure is similar to the C language.

In the following, we will show the third type of conditional structure:  if ... elseif ... else .



<?php

// MORE THAN TWO EXPRESSIONS
// MORE THAN TWO STATEMENTS 

if (expr 1)
    {
    
statement 1
    
}
elseif(
expr 2)
    {
    
statement 2
    
]
elseif(
expr 3)
    {
    
statement 3
    
]
. . . . . . . . . . . .

else
    {
    
statement N
    
}
    
    
where,

expr 1 Expression 1 to be EVALUATED

expr 2 
Expression 2 to be EVALUATED

. . . . . . . . . . . . . .

expr N Expression N to be EVALUATED


and


statement 1 Executed IF expr 1 is checked as TRUE

statement 2 
Executed IF expr 1 is checked as FALSE and 
                                        
expr 2 is checked as TRUE

. . . . . . . . . . . . .

statement N Executed by DEFAULT - that is
                       IF 
all PREVIOUSLY EVALUATED EXPRESSIONS 
                                          are checked 
as FALSE

?>

if ... elseif ... else


The result of this conditional evaluation is always a BOOLEAN type.

01 .  statement 1  will be executed ...

      If the  expr 1  is verified as TRUE.

02 .  statement 2  executed ...

      If the expr 2 is verified as TRUE and the expr 1 is verified as FALSE.

03 .  statement 3  executed ...

      If expr 1 and expr 2 are FALSE and expr 3 is TRUE.

. . . . . . . . . . . . . . .

0N .  statement N  executed ...

      if all of the above expressions are evaluated as FALSE.





Of course, there are other implementations, more complex and specific, for conditional evaluation.

Right now, what we teach you is enough.

We shall certainly return to the matter soon.