Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 35

Week 8

Functions

Prepared by:

Ryan B. Azur
PHP
Key Skills and Concepts
Learn the benefits of encapsulating code into functions and
classes
Define and use your own functions, arguments, and return
values
Understand local and global variables
Learn about recursion and variable-length argument lists
Define and use your own classes, methods, and properties
Understand OOP concepts of visibility, extensibility, and
reflection
Functions
Defined
user-defined functions are reusable code segments that are
created by the programmer to perform specific tasks. It is a
block of statements that can be used repeatedly in a program. It
will not execute immediately when a page loads but it will be
executed by a call to the function
classes provide a more formal, object-oriented approach to
reusability and encapsulation.
Functions
Advantages of Using Functions
It reduces duplication within a program, by allowing you to
extract commonly used routines into a single component.
A function is created once but used many times, often from
more than one program. If the function code changes, the
changes are implemented in one spot (the function definition)
while the function invocations remain untouched.
Debugging and testing a program becomes easier when the
program is subdivided into functions, as it becomes easier to
trace the source of an error and correct it with minimal impact
on the rest of the program.
Functions encourage abstract thinking, because packaging
program code into a function is nothing more or less than
understanding how a specific task may be encapsulated into a
generic component.
Functions
User defined functions
A user defined function declaration starts with the word
"function":
Syntax:
function functionName() {
    code to be executed;
}
A function name can start with a letter or underscore (not a
number).
Give the function a name that reflects what the function does!
Function names are NOT case-sensitive.
Functions
Components of Every Function
There are three components to every function:
Arguments, which serve as inputs to the function
Return values, which are the outputs returned by the function
The function body, which contains the processing code to turn
inputs into outputs
Functions
User Defined Functions

Html code for simple function


Functions
User Defined Functions

php code for simple function


Functions
Using Arguments and Return Values
Arguments are “placeholder” variables within a function
definition; they’re replaced at run time by values provided to
the function from the main program. The processing code within
the function then manipulates these values to return the
desired result. Since the input to the function will differ each
time it is invoked, the output will necessarily differ too.
Functions
Using Arguments and Return Values

Output:
Functions
Using Arguments and Return Values

Output:
Functions
Function data in php

php code for function data in


Functions
Function data out php

php code for function data out


Functions
Function data in out php
html input

Php output
Functions
Function data in out php

html code
Functions
Function data in out php

php code 1/5


Functions
Function data in out php

php code 2/5


Functions
Function data in out php

php code 3/5


Functions
Function data in out php

php code 4/5


Functions
Function data in out php

php code 5/5


Functions
Understanding Variable Scope
A key concept related to user-defined functions in PHP is
variable scope: the extent of a variable’s visibility within the
space of a PHP program. By default, variables used within a
function are local—their impact is restricted to the function
space alone, and they cannot be viewed or manipulated from
outside the function in which they exist.
Functions
Understanding Variable Scope
Functions
Understanding Variable Scope
Functions
Functions That Returns Multiple Values
Functions
Functions That Returns Multiple Values
Functions
Functions That Returns Multiple Values
Functions
Functions That Returns Multiple Values
Functions
Functions That Returns Multiple Values
Functions
Functions That Returns Multiple Values
Functions
Functions – Using Includes

Html file
Functions
Functions – Using Includes

Php file
Functions
Functions – Using Includes

Common Functions file 1/4


Functions
Functions – Using Includes

Common Functions file 2/4


Functions
Functions – Using Includes

Common Functions file 3/4


Functions
Functions – Using Includes

Common Functions file 4/4

You might also like