0% found this document useful (0 votes)
13 views

Web Tech Unit II

Uploaded by

Parithi Tkm
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Web Tech Unit II

Uploaded by

Parithi Tkm
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

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

Department of Computer Science

WEB TECHNOLOGY (SE24A)


IV SEMESTER

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.

PHP supports different types of conditional statements

• 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.

Syntax Flowchart Example Output

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

Department of Computer Science

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

Department of Computer Science

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

Department of Computer Science

Important points to be noticed about switch case:

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

Department of Computer Science

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

Department of Computer Science

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

Department of Computer Science

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

Department of Computer Science

Looping Statement in PHP


When you want the same block of code to run over and over again in a row. Instead of adding
several almost equal lines in a script we can use loops to perform a task.
When you want to execute same statements more than one times then use loop.

Four types of Loop used in PHP :

• for : In for loop specified condition is predefined.


• while : while loop executes the statement while a specified condition is true. In
while loop first check the condition then execute the statement.
• do-while : do - while loop executes the statement once and next statement
depends upon a specified condition is true .In do - while loop first execute the
statement then check the condition, it means if condition is false in that case one
time statement execute.
• foreach : Foreach loop executes the statement() of an associative array.

for loop in PHP

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

for(initialization; condition; increment/decrement){


//code to be executed
}
Here,

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

Department of Computer Science

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.

Increment/decrement - It increments or decrements the value of the variable.

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

Department of Computer Science

PHP While Loop

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.

It should be used if the number of iterations is not known.

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

Department of Computer Science

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

Department of Computer Science

Example

PHP do-while loop

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.

syntax Flowchart Example Outp


ut

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

Department of Computer Science

1. do{
2. //code to be exec
uted
3. }while(condition);

PHP foreach loop

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.

In foreach loop, we don't need to increment the value.

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

Department of Computer Science

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

Department of Computer Science

Combining loops in PHP

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

Department of Computer Science

Output

Interrupting(continue) and skipping(break) loop in PHP

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

Department of Computer Science

Syntax Flowchart Example Outp


ut

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

Department of Computer Science

Syntax Flowchart Example Output

Combining loops in PHP

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

Department of Computer Science

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

Department of Computer Science

Interrupting(continue) and skipping(break) loop in PHP

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.

Syntax Flowchart Example Outp


ut

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

Department of Computer Science

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.

Syntax Flowchart Example Output

21 | P a g e

You might also like