Professional Documents
Culture Documents
Week 8 Functions
Week 8 Functions
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
Output:
Functions
Using Arguments and Return Values
Output:
Functions
Function data in php
Php output
Functions
Function data in out php
html code
Functions
Function data in out php
Html file
Functions
Functions – Using Includes
Php file
Functions
Functions – Using Includes