IT WS03 Chapter 7 and 8
IT WS03 Chapter 7 and 8
Functions
What is a function and its parts?
In the world of programming,
functions are like superheroes—
powerful and specialized in
completing specific tasks. In PHP, a
function is a self-contained block of
code that performs a particular
action or calculation. Just like a
toolbox, functions allow you to
A function typically consists of three
essential parts:
1. Function Name: Every
function has a unique
name, which acts as its
identity. Think of it as a
nameplate outside a
room, guiding you to the
A function typically consists of three
essential parts:
2. Function Parameters:
Parameters are like variables
within the function that act
as placeholders for values
passed to the function. They
serve as inputs, enabling the
function to work with
A function typically consists of three
essential parts:
3. Function Body: The
function body contains the
actual code that defines
what the function does.
It's like the heart of the
function, where the magic
Let's take a closer look at a simple function:
PHP
Superglobals
PHP Form Handling
html file:
php file:
POST and $_POST
php file:
Keeping the Values in The Form
php file:
Filtering User Input
The `$_SESSION`
superglobal variable is
used to store session data
between requests. Session
data is stored on the
server and can be
Sample Code:
$_COOKIE
Functions
Creating a PHP Function
A function is a
block of code that
can be executed
whenever we
Creating PHP functions:
• All functions start with the word "function()"
• Name the function - It should be possible to
understand what the function does by its
name. The name can start with a letter or
underscore (not a number)
• Add a "{" - The function code starts after
the opening curly brace
• Insert the function code
• Add a "}" - The function is finished by a
closing curly brace
Example | A simple function that writes my name when
it is called:
Example | A simple function that writes my name when
it is called:
Use a PHP Function
Use a PHP Function
Hello world!
My name is Mel Vin.
That's right, Mel Vin is my name.
PHP Functions - Adding
parameters
Our first function (writeMyName()) is a very
simple function. It only writes a static string.