Web Tech Unit II
Web Tech Unit II
Unit- II
Conditional statement in PHP
PHP lets programmers evaluate different conditions during of a program and take decisions based on
whether these conditions evaluate to true of false. These conditions, and the actions associated with
them, are expressed by means of a programming construct called a conditional statement.
• The if Statement : In if Statements Output will appear when only Condition must be true.
• The if-else Statement : if-else statements allows you to display output in both the condition(if
condition is true display some message otherwise display other message).
• The if-elseif-else Statement : The if-else-if-else statement lets you chain together multiple if-
else statements, thus allowing the programmer to define actions for more than just two possible
outcomes.
• The switch Statement :The switch statement is similar to a series of if statements on the same
expression.
1|Page
AGURCHAND MANMULL JAIN COLLEGE
(A Unit of Sri. S.S. Jain Educational Society)
Meenambakkam, Chennai-600061
Co-Education
A Jain Minority Institution affiliated to the University of Madras
NAAC Reaccredited
1. If: 1. <?php 12 is
less
2. 2. $num=12; than
3. if(condition){ 3. if($num<100){ 100
4. //code to be execu 4. echo "$num is less than 100
ted ";
5. } 5. }
6. ?>
1. If else: 1. <?php 12 is
even
2. 2. $num=12; numbe
3. if(condition){ 3. if($num%2==0){ r
4. //code to be execu 4. echo "$num is even number
ted if true ";
5. }else{ 5. }else{
6. //code to be execu 6. echo "$num is odd number"
ted if false ;
7. } 7. }
8. ?>
2|Page
AGURCHAND MANMULL JAIN COLLEGE
(A Unit of Sri. S.S. Jain Educational Society)
Meenambakkam, Chennai-600061
Co-Education
A Jain Minority Institution affiliated to the University of Madras
NAAC Reaccredited
1. If else if : B
Grade
2.
3. if (condition1){
4. //code to be execu
ted if condition1 is
true
5. } elseif (condition2
){
6. //code to be execu
ted if condition2 is
true
7. } elseif (condition3
){
8. //code to be execu
ted if condition3 is
true
9. ....
} else{
//code to be execu
ted if all given con
ditions are false
}
Switch statement
PHP switch statement is used to execute one statement from multiple conditions. It works like
PHP if-else-if statement.
3|Page
AGURCHAND MANMULL JAIN COLLEGE
(A Unit of Sri. S.S. Jain Educational Society)
Meenambakkam, Chennai-600061
Co-Education
A Jain Minority Institution affiliated to the University of Madras
NAAC Reaccredited
1. The default is an optional statement. Even it is not important, that default must
always be the last statement.
2. There can be only one default in a switch statement. More than one default may
lead to a Fatal error.
3. Each case can have a break statement, which is used to terminate the sequence of
statement.
4. The break statement is optional to use in switch. If break is not used, all the
statements will execute after finding matched case value.
4|Page
AGURCHAND MANMULL JAIN COLLEGE
(A Unit of Sri. S.S. Jain Educational Society)
Meenambakkam, Chennai-600061
Co-Education
A Jain Minority Institution affiliated to the University of Madras
NAAC Reaccredited
5. PHP allows you to use number, character, string, as well as functions in switch
expression.
6. Nesting of switch statements is allowed, but it makes the program more complex
and less readable.
7. You can use semicolon (;) instead of colon (:). It will not generate any error.
5|Page
AGURCHAND MANMULL JAIN COLLEGE
(A Unit of Sri. S.S. Jain Educational Society)
Meenambakkam, Chennai-600061
Co-Education
A Jain Minority Institution affiliated to the University of Madras
NAAC Reaccredited
6|Page
AGURCHAND MANMULL JAIN COLLEGE
(A Unit of Sri. S.S. Jain Educational Society)
Meenambakkam, Chennai-600061
Co-Education
A Jain Minority Institution affiliated to the University of Madras
NAAC Reaccredited
Switch Example
7|Page
AGURCHAND MANMULL JAIN COLLEGE
(A Unit of Sri. S.S. Jain Educational Society)
Meenambakkam, Chennai-600061
Co-Education
A Jain Minority Institution affiliated to the University of Madras
NAAC Reaccredited
The for loop is used when you know in advance how many times the script should run. It should
be used if the number of iterations is known otherwise use while loop. It allows users to put all
the loop related statements in one place
Syntax
initialization - Initialize the loop counter value. The initial value of the for loop is done
only once. This parameter is optional.
8|Page
AGURCHAND MANMULL JAIN COLLEGE
(A Unit of Sri. S.S. Jain Educational Society)
Meenambakkam, Chennai-600061
Co-Education
A Jain Minority Institution affiliated to the University of Madras
NAAC Reaccredited
condition - Evaluate each iteration value. The loop continuously executes until the
condition is false. If TRUE, the loop execution continues, otherwise the execution of the
loop ends.
9|Page
AGURCHAND MANMULL JAIN COLLEGE
(A Unit of Sri. S.S. Jain Educational Society)
Meenambakkam, Chennai-600061
Co-Education
A Jain Minority Institution affiliated to the University of Madras
NAAC Reaccredited
PHP while loop can be used to traverse set of code like for loop. The while loop executes
a block of code repeatedly until the condition is FALSE. Once the condition gets FALSE, it
exits from the body of loop.
The while loop is also called an Entry control loop because the condition is checked
before entering the loop body. This means that first the condition is checked. If the
condition is true, the block of code will be executed.
10 | P a g e
AGURCHAND MANMULL JAIN COLLEGE
(A Unit of Sri. S.S. Jain Educational Society)
Meenambakkam, Chennai-600061
Co-Education
A Jain Minority Institution affiliated to the University of Madras
NAAC Reaccredited
11 | P a g e
AGURCHAND MANMULL JAIN COLLEGE
(A Unit of Sri. S.S. Jain Educational Society)
Meenambakkam, Chennai-600061
Co-Education
A Jain Minority Institution affiliated to the University of Madras
NAAC Reaccredited
Example
PHP do-while loop can be used to traverse set of code like php while loop. The PHP
do-while loop is guaranteed to run at least once.
The PHP do-while loop is used to execute a set of code of the program several times.
If you have to execute the loop at least once and the number of iterations is not
even fixed, it is recommended to use the do-while loop.
It executes the code at least one time always because the condition is checked after
executing the code.
The do-while loop is very much similar to the while loop except the condition check.
The main difference between both loops is that while loop checks the condition at
the beginning, whereas do-while loop checks the condition at the end of the loop.
12 | P a g e
AGURCHAND MANMULL JAIN COLLEGE
(A Unit of Sri. S.S. Jain Educational Society)
Meenambakkam, Chennai-600061
Co-Education
A Jain Minority Institution affiliated to the University of Madras
NAAC Reaccredited
1. do{
2. //code to be exec
uted
3. }while(condition);
The foreach loop is used to traverse the array elements. It works only on array and object.
It will issue an error if you try to use it with the variables of different datatype.
The foreach loop works on elements basis rather than index. It provides an easiest way to
iterate the elements of an array.
13 | P a g e
AGURCHAND MANMULL JAIN COLLEGE
(A Unit of Sri. S.S. Jain Educational Society)
Meenambakkam, Chennai-600061
Co-Education
A Jain Minority Institution affiliated to the University of Madras
NAAC Reaccredited
syntax Flowchart
14 | P a g e
AGURCHAND MANMULL JAIN COLLEGE
(A Unit of Sri. S.S. Jain Educational Society)
Meenambakkam, Chennai-600061
Co-Education
A Jain Minority Institution affiliated to the University of Madras
NAAC Reaccredited
A nested loop is a loop within a loop, where an inner loop is executed multiple times
for each iteration of the outer loop. In PHP, the for keyword indicates a loop. The for
loop executes a block of statements repeatedly until the specified condition returns
false. Here is an example of a nested for loop in PHP that creates a chessboard:
15 | P a g e
AGURCHAND MANMULL JAIN COLLEGE
(A Unit of Sri. S.S. Jain Educational Society)
Meenambakkam, Chennai-600061
Co-Education
A Jain Minority Institution affiliated to the University of Madras
NAAC Reaccredited
Output
PHP break statement breaks the execution of the current for, while, do-while, switch, and
for-each loop. If you use break inside inner loop, it breaks the execution of inner loop
only.
The break keyword immediately ends the execution of the loop or switch structure. It
breaks the current flow of the program at the specified condition and program control
resumes at the next statements outside the loop.
16 | P a g e
AGURCHAND MANMULL JAIN COLLEGE
(A Unit of Sri. S.S. Jain Educational Society)
Meenambakkam, Chennai-600061
Co-Education
A Jain Minority Institution affiliated to the University of Madras
NAAC Reaccredited
Continue Statement:
The PHP continue statement is used to continue the loop. It continues the current flow of
the program and skips the remaining code at the specified condition.
The continue statement is used within looping and switch control structure when you
immediately jump to the next iteration.
The continue statement can be used with all types of loops such as - for, while, do-while,
and foreach loop. The continue statement allows the user to skip the execution of the
code for the specified condition.
17 | P a g e
AGURCHAND MANMULL JAIN COLLEGE
(A Unit of Sri. S.S. Jain Educational Society)
Meenambakkam, Chennai-600061
Co-Education
A Jain Minority Institution affiliated to the University of Madras
NAAC Reaccredited
A nested loop is a loop within a loop, where an inner loop is executed multiple times
for each iteration of the outer loop. In PHP, the for keyword indicates a loop. The for
loop executes a block of statements repeatedly until the specified condition returns
false. Here is an example of a nested for loop in PHP that creates a chessboard:
18 | P a g e
AGURCHAND MANMULL JAIN COLLEGE
(A Unit of Sri. S.S. Jain Educational Society)
Meenambakkam, Chennai-600061
Co-Education
A Jain Minority Institution affiliated to the University of Madras
NAAC Reaccredited
Output
19 | P a g e
AGURCHAND MANMULL JAIN COLLEGE
(A Unit of Sri. S.S. Jain Educational Society)
Meenambakkam, Chennai-600061
Co-Education
A Jain Minority Institution affiliated to the University of Madras
NAAC Reaccredited
PHP break statement breaks the execution of the current for, while, do-while, switch, and
for-each loop. If you use break inside inner loop, it breaks the execution of inner loop
only.
The break keyword immediately ends the execution of the loop or switch structure. It
breaks the current flow of the program at the specified condition and program control
resumes at the next statements outside the loop.
20 | P a g e
AGURCHAND MANMULL JAIN COLLEGE
(A Unit of Sri. S.S. Jain Educational Society)
Meenambakkam, Chennai-600061
Co-Education
A Jain Minority Institution affiliated to the University of Madras
NAAC Reaccredited
Continue Statement:
The PHP continue statement is used to continue the loop. It continues the current flow of
the program and skips the remaining code at the specified condition.
The continue statement is used within looping and switch control structure when you
immediately jump to the next iteration.
The continue statement can be used with all types of loops such as - for, while, do-while,
and foreach loop. The continue statement allows the user to skip the execution of the
code for the specified condition.
21 | P a g e