Title: CSCI 116 Week 3 Functions
1CSCI 116 Week 3 Functions Control Structures
2Topics
- Using functions
- Variable scope and autoglobals
- Decision logic
- if statements
- if...else statements
- switch statements
- Loops
- while statements
- do...while statements
- for and foreach statements
3Defining Functions
- Functions
- Groups of statements that you can execute as a
single unit - Defining a function
- lt?php
- function name_of_function(parameters)
-
- statements
-
- ?gt
4Function Parameters
- A parameter is an input to the function
- Parameters are placed within the parentheses that
follow the function name - Not all functions accept parameters
function average(num1, num2)
2 parameters
function printHello()
0 parameters
function calcTax(price)
1 parameter
5Function Example
- function printPhrase(phrase)
-
- echo phraselt/ brgt
-
- printPhrase(Silly goose!)
6Returning Values
- A return statement returns a value to the
statement that called the function - A function does not have to return a value
- function averageNumbers(a, b, c)
-
- sumOfNumbers a b c
- average sumOfNumbers / 3
- return average
-
7Function Practice
- Write a function that takes a name and prints
Hello, name! - Write a function that returns the maximum of two
values. - Write a function that converts celsius to
fahrenheit (FC1.832). - Write a function that takes a radius and returns
the circumference of a circle (CPIDiameter).
8Understanding Variable Scope
- Variable scope
- Where in your program a declared variable can be
used - Can be either global or local
- Global variable
- Declared outside a function and available to all
parts of your program - Local variable
- Declared inside a function and only available
within that function
9Variable Scope
- lt?php
- globalVar "Global"
- function scopeExample()
-
- echo "ltbgtInside function ltbr /gtlt/bgt"
- localVar "Local"
- echo "localVarltbr /gt"
- global globalVar
- echo "globalVarltbr /gtltbr /gt"
-
- scopeExample()
- echo "ltbgtOutside function ltbr /gtlt/bgt"
- echo "localVarltbr /gt"
- echo "globalVarltbr /gt"
- ?gt
global keyword used inside function to
reference global variable
10Autoglobals
- PHP includes predefined global arrays, called
autoglobals or superglobals - Autoglobals contain client, server, and
environment information - Autoglobals are associative arrays
- Elements are referred to with an alphanumeric key
instead of an index number - Example _SERVERPHP_SELF
11PHP Autoglobals
See http//us.php.net/reserved.variables for more
information.
12Using GLOBALS
lt?php globalVar "Global" function
scopeExample() echo "ltbgtInside functionltbr
/gtlt/bgt" localVar "Local" echo
"localVarltbr /gt" echo GLOBALS"globalVar" ,
"ltbr /gtltbr /gt" scopeExample() echo
"ltbgtOutside functionltbr /gtlt/bgt" echo
"localVarltbr /gt" echo "globalVarltbr /gt" ?gt
13Using _SERVER
- lt?php
- echo "The name of the current script is ",
_SERVER"PHP_SELF", "ltbr /gt" - echo "The absolute path of the current script
is ", _SERVER"SCRIPT_FILENAME", "ltbr /gt" - echo "The name of the server on which this
script is executing is ", _SERVER"SERVER_NAME"
, "ltbr /gt" - echo "This script was executed with the
following server software ", _SERVER"SERVER_SOF
TWARE", "ltbr /gt" - echo "This script was executed with the
following server protocol ", _SERVER"SERVER_PRO
TOCOL" - ?gt
14Using _GET and _POST
- _GET and _POST allow you to access the values
of forms that are submitted to a PHP script - _GET appends form data as one long string to the
URL - _POST sends form data as a transmission separate
from the URL
15_GET Example
ltform method"get" action"processForm.php"gt N
amenbspltinput type"text" name"name" size"20"
/gtltbr /gt Hometownnbspltinput type"text"
name"town" size"20" /gtltbr /gt ltinput
type"submit" value"Submit"gt lt/formgt
lt?php echo "Hello, ", _GET"name", " from ",
_GET"town", "!" ?gt
Note the values in the URL
16_POST Example
ltform methodpost" action"processForm.php"gt
Namenbspltinput type"text" name"name"
size"20" /gtltbr /gt Hometownnbspltinput
type"text" name"town" size"20" /gtltbr
/gt ltinput type"submit" value"Submit"gt lt/formgt
lt?php echo "Hello, ", _POST"name", " from ",
_POST"town", "!" ?gt
No values in the URL
17if Statements
- Used to execute specific programming code if a
condition returns true - Syntax
- if (conditional expression)
- statement
- Example
- num 5
- if (num 5) // CONDITION IS TRUE
-
- echo The condition evaluates to true.lt/ brgt
- echo 'num is equal to ', num.lt/ brgt
-
- echo This statement always executes.lt/ brgt
18if...else Statements
- An else clause executes when the condition in an
if...else statement evaluates to false - Syntax
- if (conditional expression)
- statement
- else
- statement
- Example
- Today Tuesday
- if (Today Monday)
- echo Today is Mondaylt/ brgt
- else
- echo Today is not Mondaylt/ brgt
Curly braces are not required when there is only
one statement after the if or else.
19Nested if Statements
- One decision-making statement may be contained
within another - if (_GETSalesTotal gt 50)
-
- if (_GETSalesTotal lt 100)
-
- echo Sales total is between 50 and 100.lt/
brgt -
-
20switch Statements
- Controls program flow by executing a specific set
of statements depending on the value of an
expression - Compares the value of an expression to a value in
a case label - case label can be followed by one or more
statements - Each set of statements is usually followed by the
keyword break - The default label contains statements that
execute when the value of the expression does not
match any other case label
21switch Statements
- Syntax
- Switch (expression)
-
- case label statement(s)
- break
- case label
- statement(s)
- break
- ...
- default
- statement(s)
-
- Example
- Switch (day)
-
- case 1
- print partridge
- break
- case 2
- print turtle doves
- break
- ...
- default
- print Invalid
22Decision Practice
- Write a statement that prints even if a
variable num is even. - Write a statement that prints even if a
variable num is even, and odd otherwise. - Write a statement that prints A for a grade of
90-100, B for 80-89, C for 70-79, D for
60-69 and F otherwise. - Write a switch statement that takes a number and
prints the corresponding month, e.g. January
for 1.
23Repeating Code
- A loop statement is a control structure that
repeatedly executes a statement or a series of
statements - Each repetition of a loop is called an iteration
- Loop may execute while a condition is true or
until a condition becomes true - Four types of loops
- while statements
- do...while statements
- for statements
- foreach statements
24while Statements
- Repeats one or more statements while a
conditional expression evaluates to true - Syntax
- while (conditional expression)
-
- statement(s)
-
25while Statement Example
- Count 1
- while (Count lt 5)
-
- echo Countltbr /gt
- Count
-
- echo You have printed 5 numbers.lt/ brgt
26while Statement Example
- Count 10
- while (Count gt 0)
-
- echo Countltbr /gt
- --Count
-
- echo We have liftoff.lt/ brgt
27while Statements Example
- Count 1
- while (Count lt 100)
-
- echo Countltbr /gt
- Count 2
-
28Infinite Loops
- In an infinite loop, a loop statement never ends
because its conditional expression is never false - Count 1
- while (Count lt 10)
-
- echo The number is Count
-
29do...while Statements
- Executes a statement or statements at least once
- Repeats execution as long as conditional
expression is true - Syntax
- do
-
- statement(s)
- while (conditional expression)
30do...while Example
- Count 2
- do
-
- echo The count is equal
- to Countlt/ brgt
- Count
- while (Count lt 2)
31do...while Example
- DaysOfWeek array(Monday, Tuesday,
Wednesday, Thursday, Friday, Saturday,
Sunday) - Count 0
- do
-
- echo DaysOfWeekCount, ltbr /gt
- Count
- while (Count lt 7)
32for Statements
- Used to repeat one or more statements as long as
a given condition is true - Includes code that initializes a counter and
changes its value with each iteration - Syntax
- for (counter declaration and initialization
condition update statement) -
- statement(s)
-
33for Statements Example
- FastFoods array(pizza, burgers, french
fries, tacos, fried chicken) - for (Count 0 Count lt 5 Count)
-
- echo FastFoodsCount, ltbr /gt
-
34foreach Statements
- Used to iterate or loop through the elements in
an array - Does not require a counter
- Specify an array expression within a set of
parentheses following foreach - Syntax
- foreach (array_name as variable_name)
-
- statements
35foreach Example
- animals array(lion, tiger, monkey)
- foreach (animals as animal)
-
- echo animalltbr /gt
36Loop Practice
- Using a do loop, dowhile loop, and for loop,
perform each of the following tasks - Print the numbers from 10 to 1 and then
Blastoff! - Add up the numbers from 1 to 10 and then display
the result.
37Summary
- Functions are groups of statements that you can
execute as a single unit - Autoglobals contain client, server, and
environment information - Flow control determines the order in which
program statements execute - Decision logic includes if, ifelse, and switch
- Loops include while, dowhile, for, and foreach