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

Week 4

The document discusses different types of expressions and control structures in PHP including conditionals like if/else statements, elseif statements, switch statements and the ternary operator. It also covers different types of loops in PHP - while loops, do/while loops and for loops. Examples are provided for each along with activities for students to write small code snippets using the different expressions and control structures.

Uploaded by

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

Week 4

The document discusses different types of expressions and control structures in PHP including conditionals like if/else statements, elseif statements, switch statements and the ternary operator. It also covers different types of loops in PHP - while loops, do/while loops and for loops. Examples are provided for each along with activities for students to write small code snippets using the different expressions and control structures.

Uploaded by

santhirans770
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 38

HNDIT3022

Web Programming
Week4:- Expressions & Control flow in PHP
Expressions
• An expression is a combination of values, variables, operators, and functions
that results in a value.

• The simplest form of an expression is a literal, which simply means


something that evaluates to itself.
• Example:- 73 , "Hello".

• An expression could also simply be a variable, which evaluates to the value


that has been assigned to it.

• They are both types of expressions, because they return a value.


Example
Conditionals
Control Structures
Conditional
• Conditionals alter program flow. They enable you to ask questions
about certain things and respond to the answers you get in different
ways.
• There are three types of non-looping conditionals:
• The if statement
• The switch statement
• The ? operator.
The if statement
• In the case of an if statement, you could imagine coming across a
detour sign that you have to follow.
• If a certain condition is TRUE you drive off and follow the detour until
you return to where it started and then continue on your way in your
original direction.
• Or,
• If the condition isn’t FALSE, you ignore the detour and carry on driving
The if statement[2]
• The if statement executes some code if one condition is true.

• Syntax
if(condition){
code to be executed if condition is true;
}
Example

date() is a predefined function which formats a local date and time, and returns the
formatted date string.
else statement
• Sometimes when a conditional is not TRUE, you may not want to
continue on to the main program code immediately but might wish to
do something else instead.
• This is where the else statement comes in
else statement[2]
• With an if ... else statement, the first conditional statement is
executed if the condition is TRUE.
• But if it’s FALSE, the second one is executed

• Syntax
if(condition){
}
else{
}
Example
Activity 4.1
• Write down a PHP code segment to display whether a given number is
odd or even.
elseif Statement
• There are also times when you want a number of different
possibilities to occur, based upon a sequence of conditions.

• You can achieve this using the elseif statement.


elseif Statement[2]
• You may have as many elseif statements as you like.

• But as the number of elseif statements increases, you would probably be better
advised to consider a switch statement if it fits your needs.

• Syntax
if(condition){

}elseif(condition){

}else{

}
Example
The switch Statement
• The switch statement is useful in cases in which one variable or the result of an expression can
have multiple values, which should each trigger a different function.

• Syntax switch(value to check):


switch(value to check){ case value1:
case value1: //statement1;
//statement1; break;
break; OR ………..
……….. ………..
……….. …………
………… default:
default: //statement default;
//statement default; break;
break; endswitch
}
Example
Activity 4.2
• Write down a PHP code segment to print the weekday name when
day number is given. (use date(“N”) to get the day number
The ? Operator(ternary operator)
• Compact version of if…else
• The ? operator is passed an expression that it must evaluate, along
with two statements to execute:
• one for when the expression evaluates to TRUE,
• the other for when it is FALSE.
Example
Activity 4.3
• Write down a PHP code to print a given number is Odd or Even using
ternary operator.
Looping
Looping
• One of the great things about computers is that they can repeat
calculating tasks quickly and tirelessly.

• Often you may want a program to repeat the same sequence of code
again and again until something happens, such as a user inputting a
value or reaching a natural end.

• PHP’s various loop structures provide the


perfect way to do this
Types of loops
• While loop
• Do….while loop
• For loop
While Loop
• Syntax

while(condition){

//statement
increment/decrement

}
Activity 4.4
• Write down a PHP code segment to display the numbers from 10-1.
Example
do……while loop
• Syntax

do{
//statement
increment/decrement
}while(condition);
Example
Activity 4.6
• Write down a PHP code to display the numbers from 1-10
For loop
• The for loop, is also the most powerful, as it combines the abilities to
set up variables as you enter the loop, test for conditions while
iterating loops, and modify variables after each iteration.

• Syntax
for(initialization; condition; increment/decrement){
//statement
}
Example
Activity 4.6
• Write a small PHP code to print multiplication table of 5.

5 times 1= 5
5 times 2=10
……………
………….
………
For loop[2]
• A more complex form of the for statement even lets you perform
multiple operations in each of the three parameters

• Example
Break
• Just as you saw how to break out of a switch statement, you can also
break out of a for loop using the same break command.
Continue
• The continue statement is a little like a break statement, except that it
instructs PHP to stop processing the current loop and to move right to
its next iteration.
Questions ?
Thank You !

You might also like