1 session 3: flow control & functions inet academy open source web programming

33
1 Session 3: Flow Control & Functions iNET Academy Open Source Web Programming

Upload: melinda-adams

Post on 01-Jan-2016

222 views

Category:

Documents


0 download

TRANSCRIPT

1

Session 3: Flow Control & Functions

iNET Academy

Open Source Web Programming

2

Objectives

Discuss about expressions & operators Discuss about conditional statements Discuss about loop statements Discuss about calling & defining functions

3

Expressions

A statement is code that performs a task Statements are made up of epressions and operators An expression is a piece of code that evaluates to a value An operator is a code element that acts on an expression in some way

4

Operators

PHP has many types of operators, including Arithmetic operators Array operators Assignment operators Bitwise operators Comparison operators Execution operators Increment/decrement operators Logical operators String operators

An operator has four properties Number of operands Type of operands Order of precedent Operator associativity

5

Operators (cont.)

Number of Operands Binary operators: combine two expressions into a single expression Unary operators: take only one operand

Negation operator (-) Preincrement and predecrement

Ternary operator: takes three operator

6

Operators (cont.)

Types of Operands Certain operators expect their operands to be of particular data

types PHP automatically convert operands to the expected data types.

But there are times, conversions are impossible. Eg: “Becker”*”Furniture” ; “80” * “70”

To set or convert a variable’s type using settype to change the data type actually casting temporarily converts the value

7

Operators (cont.)

Order of Precedence Determines which operator processes first in an expression Eg: * and / before + and – If operators have the same precedence, they are processed in the

order they appear in the expression

Use ( and ) to change the precendence

8

9

Operators (cont.)

Relational operators: used to compare two operands and return either TRUE or FALSE value.

Equality operator (==) only checks whether the values are the same. Data type

conversion may be required Eg: “1” == 1 return TRUE (===) checks whether the values and types are the same. Eg: ‘1’ === 1 returns FALSE

Inequality operator != checks for the opposite of equality Comparison operators: <, <=, >, >= Logical operators

10

Conditionals

There are three conditionals in PHP If ? : : (shorthand for an if statement) switch

11

The if statement

Common true conditions $var, if $var is not an empty set (0), an empty string, or NULL Isset($var) if $var is not NULL, an empty string or an empty set An expression return TRUE

12

The elseif statement

13

The ? operator

14

The switch statement

Compares an expression to numerous values

15

The switch statement (cont.)

Breaking out: using break keyword

16

The switch statement (cont.)

Defaulting

17

Looping

While For Do … while

18

While loops

19

Do .. While loops

20

For loops

21

Breaking Out of a Loop

Use break statement

22

Continue Statement

Stop processing the current block of code and jump to the next iteration of the loop

23

Functions

Create & execute a function Send values to a function Return values from a function Verify if a function exists

24

Calling Functions

To call a function Write the name of the function An opening parenthesis ( The parameters A closing parenthesis

Function’s name are not case-sensitive Most functions have return values

25

Defining Functions

Four elements of a function’s definition 1 – the function keyword 2 – the function’s name 3 – the arguments are optional 4 – the code of the function

Function can be defined and called anywhere

26

Parameters

Information pass to a function Parameter’s definition

Type of data: don’t have to define Parameter’s names must be specified

27

Parameters (cont.)

Parameters can have default values

28

Parameters (cont.)

Parameter Reference: pass the reference to value, not pass the value

29

Including and Requiring PHP files

Place functions in a separate file to make code more readable PHP provides four functions

include require include_once require_once

The include statement:

include and attach other

PHP scripts to your scripts Include files should use

.php extension instead of

other extensions like .inc

because of security reason

30

Including and Requiring PHP files (cont.) The include_once statement: prevent inlcuding many nested PHP

scripts

31

Including and Requiring PHP files (cont.) require and require_once functions

When using include and include_once, if the including PHP files is not exist, PHP post a warning and try to execute the program

Require and require_once is used to make sure that a file is included and to stop the program if it isn’t

32

Testing a function

To check for the existence of functions, use the function function_exist() It takes a parameter as the function’s name Return TRUE or FALSE depending on whether the function has been

defined

33

Practice In this practice, you will:

Ex1: Write a loop using the “for” loop to print all even numbers which are less than 50

Ex2: Same task as Ex1, but using the “while” loop Ex3: Write a program to find prime numbers which are less

than 100. Display them in each rows. Ex4: Print the prime numbers from Ex3 in a table, each rows

has 10 numbers. Ex5: Write a program that includes previous exercises. Use

a variable name $flag. If $flag==1 then include Ex1 If $flag==2 then include Ex2 If $flag==3 then include Ex3 If $flag==4 then include Ex4

Ex6: Pack Ex1-Ex4 into four functions. Use the same routine as Ex5.