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

Slides:



Advertisements
Similar presentations
Session 1 & 2BBK P1 Module5-May-2007 : [‹#›] PHP: Moving On..
Advertisements

Objectives Using functions to organize PHP code
1 PHP Statement Constructs Server Scripting. 5-2 Basic Statement All Statements end in a semicolon. Statements are delimited from the HTML code by enclosing.
JavaScript Part for Repetition Statement for statement Cpecifies each of the items needed for counter-controlled repetition with a control variable.
PHP Functions and Control Structures. 2 Defining Functions Functions are groups of statements that you can execute as a single unit Function definitions.
ITC 240: Web Application Programming SUBHASH PRAJAPATI 04/14/15.
Basic Elements of Programming A VB program is built from statements, statements from expressions, expressions from operators and operands, and operands.
Chapter 4 Functions and Control Structures PHP Programming with MySQL.
JavaScript, Third Edition
C++ for Engineers and Scientists Third Edition
 2008 Pearson Education, Inc. All rights reserved JavaScript: Control Statements II.
The switch Statement, DecimalFormat, and Introduction to Looping
Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting Control Structures, Operators, and Functions.
The switch StatementtMyn1 The switch Statement Sometimes there can be a multiple-choice situation, in which you need to execute a particular set of statements.
2010/11 : [1]Building Web Applications using MySQL and PHP (W1)PHP Recap.
Week 9 PHP Cookies and Session Introduction to JavaScript.
Chap 3 – PHP Quick Start COMP RL Professor Mattos.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
Chapter 4: Decision Making with Control Structures and Statements JavaScript - Introductory.
1 Chapter 4: Selection Structures. In this chapter, you will learn about: – Selection criteria – The if-else statement – Nested if statements – The switch.
Control Structures – Selection Chapter 4 2 Chapter Topics  Control Structures  Relational Operators  Logical (Boolean) Operators  Logical Expressions.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
CSCI 1100/1202 January 28, The switch Statement The switch statement provides another means to decide which statement to execute next The switch.
20-753: Fundamentals of Web Programming 1 Lecture 12: Javascript I Fundamentals of Web Programming Lecture 12: Introduction to Javascript.
CONTROL STRUCTURE The if, elseif, and else & switch Statements 1.
PHP Conditional Statements Conditional statements in PHP are used to perform different actions based on different conditions. Conditional Statements Very.
PHP Programming with MySQL Slide 4-1 CHAPTER 4 Functions and Control Structures.
Slide 1 PHP Operators and Control Structures ITWA 133.
Class 2Intro to Databases Goals of this class Include & Require in PHP Generating Random Numbers in PHP Arrays – Numerically Indexed and Associative Program.
Chapter 2 Functions and Control Structures PHP Programming with MySQL 2 nd Edition.
Control Structures By Shyam Gurram. Control Structure In this chapter we have two different types of structures. Conditional Structure Iterative Control.
JavaScript Syntax and Semantics. Slide 2 Lecture Overview Core JavaScript Syntax (I will not review every nuance of the language)
Saeed Ghanbartehrani Summer 2015 Lecture Notes #5: Programming Structures IE 212: Computational Methods for Industrial Engineering.
Chapter 3 Functions, Events, and Control Structures JavaScript, Third Edition.
4.1 Object Operations operators Dot (. ) and new operate on objects The assignment operator ( = ) Arithmetic operators + - * / % Unary operators.
Lesson - 5. Introduction While programming, we usually need to decide the path of the program flow according to the parameters and conditions. Actually.
JavaScript, Fourth Edition
©Colin Jamison 2004 Shell scripting in Linux Colin Jamison.
 In computer programming, a loop is a sequence of instruction s that is continually repeated until a certain condition is reached.  PHP Loops :  In.
PHP Programming with MySQL Slide 3-1 CHAPTER 3 Working with Data Types and Operators.
OperatorstMyn1 Operators An operator is something that you feed with one or more values (or expressions, in programming jargon) which yields another value.
Expressions and Control Flow. Expressions An expression is a combination of values, variables, operators, and functions that results in a value y = 3(abs(2x)
BOOLEAN OPERATIONS AND CONDITIONALS CHAPTER 20 1.
Basic Scripting & Variables Yasar Hussain Malik - NISTE.
Chapter 1 Java Programming Review. Introduction Java is platform-independent, meaning that you can write a program once and run it anywhere. Java programs.
0 Chap.2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations 2.5Arithmetic Operators 2.6Relational.
Chapter 7 Conditional Statements. 7.1 Conditional Expressions Conditions - compare the values of variables, constants and literals using one or more relational.
PHP Reusing Code and Writing Functions 1. Function = a self-contained module of code that: Declares a calling interface – prototype! Performs some task.
Session 2: PHP Language Basics iNET Academy Open Source Web Development.
PHP Tutorial. What is PHP PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
PHP Workshop ‹#› أطلق إبداعك 2 أطلق إبداعك 2 مدرس معتمد من مركز زووم PHP: Moving On..
PHP Condtions and Loops Prepared by Dr. Maher Abuhamdeh.
Operators and Expressions
University of Central Florida COP 3330 Object Oriented Programming
University of Central Florida COP 3330 Object Oriented Programming
JavaScript: Control Statements I
Scripts & Functions Scripts and functions are contained in .m-files
Expressions and Control Flow in JavaScript
JavaScript: Control Statements.
String Conversion and Type Juggling
Operators and Expressions
Arithmetic operations, decisions and looping
Chapter 8 JavaScript: Control Statements, Part 2
PHP.
Objectives In this chapter, you will:
ENERGY 211 / CME 211 Lecture 5 October 1, 2008.
Lesson 3. Controlling program flow. Loops. Methods. Arrays.
Presentation 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.