0% found this document useful (0 votes)
2 views

IT WS03 Chapter 7 and 8

The document provides an overview of functions in PHP, detailing their essential parts: function name, parameters, and body. It explains built-in and user-defined functions, emphasizing the importance of type declarations for data safety. Additionally, it covers PHP form handling, including methods for data submission, filtering user input, and practical examples of form processing.

Uploaded by

Arjay Vicencio
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

IT WS03 Chapter 7 and 8

The document provides an overview of functions in PHP, detailing their essential parts: function name, parameters, and body. It explains built-in and user-defined functions, emphasizing the importance of type declarations for data safety. Additionally, it covers PHP form handling, including methods for data submission, filtering user input, and practical examples of form processing.

Uploaded by

Arjay Vicencio
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 64

Lesson 7

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:

In this example, "add" is the function name. The


parameters, `$num1` and `$num2`, allow us to
pass two values to the function. Inside the
function body, we calculate the sum of these two
values and use the `return` statement to send the
result back to the caller.
Built-in Functions

Imagine PHP as a treasure chest


filled with valuable functions just
waiting to be discovered. These
are known as built-in functions,
and they come pre-packaged
with PHP. Built-in functions save
us time and effort, as we can use
them directly without writing the
Built-in Functions

Think of built-in functions as


a collection of specialized
tools, each designed for a
specific task. For example,
the `strlen` function helps
you find the length of a
string, while the `date`
Built-in Functions

In this example, we use the


`strlen` function to find the length
of the string `$name` (which is 4
characters long) and the `date`
function to display the current date
User-Defined Functions

One of the most empowering


features of PHP is the ability to
create your own functions. It's
like being a master chef who
invents unique recipes to delight
their guests. User-defined
functions allow you to tailor your
code to specific needs and add a
User-Defined Functions
To define a user-defined
function, you use the `function`
keyword, followed by a function
name of your choice. Then,
within parentheses, you can list
any parameters your function
requires. Lastly, the function
body—enclosed in curly braces—
User-Defined Functions

In this example, we define a


function called `greet`, which takes
one parameter, `$name`. The
function body uses `echo` to output
a personalized greeting message.

Now, whenever you call the `greet`


function and provide a name as an
Type Declaration

Type declaration is like adding a


signboard to your function,
clearly stating what type of data
it expects and what type of data
it will return. This enhances code
safety and clarity, as it helps
prevent unexpected errors and
misinterpretations.
Type Declaration
Type declaration is like adding a signboard
to your function, clearly stating what type
of data it expects and what type of data it
will return. This enhances code safety and
clarity, as it helps prevent unexpected
errors and misinterpretations.
Imagine a function that only accepts specific
data types and communicates its expected
output. Type declarations serve as a set of
rules for your function, ensuring it plays by
Type Declaration

In this example, we define a function


named `add` that takes two integer
parameters, `$num1` and `$num2`. The
`int` before the parameter names
indicates that we expect integer data
for these inputs. The `: int` after the
Type Declaration

Understanding these key aspects


of functions in PHP lays a strong
foundation for mastering the art
of programming. As you continue
your journey, you'll learn to
harness the power of functions
to build efficient and elegant
code, making your PHP projects
Type Declaration

With type declarations,


you can be confident
that the function
receives the correct
data and provides the
expected result.
Lesson 8

PHP
Superglobals
PHP Form Handling

In PHP, form handling is the


process of collecting and
processing data submitted
through an HTML form. The data
is sent to the server using either
the GET or POST method, and
can be accessed using the
`$_GET` or `$_POST` superglobal
The isset() function

The `isset()` function is used


to check if a variable is set
and not null. It can be used
to check if a form has been
submitted and if the form
data is available in the
`$_GET` or `$_POST`
Sample Code:
GET and $_GET

The GET method is used to


submit form data through the
URL. The form data is appended
to the URL as a query string, and
can be accessed using the
`$_GET` superglobal variable.
The `$_GET` variable is an
associative array that contains
Sample Code:

html file:

php file:
POST and $_POST

The POST method is used to


submit form data through the
request body. The form data is
not visible in the URL, and can be
accessed using the `$_POST`
superglobal variable. The
`$_POST` variable is also an
associative array that contains
POST and $_POST | Sample Code:
html file:

php file:
Keeping the Values in The Form

To keep the values in


the form after
submission, the
values can be set as
the default values of
Keeping the Values in The Form | Sample
Code:
html file:

php file:
Filtering User Input

User input should be filtered


to prevent security
vulnerabilities such as cross-
site scripting (XSS). PHP
provides several functions
for filtering user input, such
as `filter_input()` and
Sample Code for Filtering User Input:
Cross-Site Scripting

Cross-site scripting (XSS) is a


security vulnerability that
allows an attacker to inject
malicious code into a web
page. To prevent XSS attacks,
user input should be filtered
using functions such as
Sample Code for Preventing XSS:
$_SESSION

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

The `$_COOKIE` superglobal


variable is used to store
cookie data. Cookies are
small pieces of data that are
stored on the client's
computer and can be
accessed and modified by the
Sample Code:
PHP Form Handling Practical Examples

We already learned that in PHP, form


handling is an essential aspect of web
development.
It allows users to submit data to the
server, which can then be processed and
used for various purposes, such as
registration, login, search, or feedback
submission. Let's explore some practical
examples of PHP form handling using
Practical Example 1: Simple Login Form using
POST
login_form.php :
login_process.php :
Practical Example 2: Search Form using GET
Seach_results.php :
Practical Example 2: Search Form using GET
Seach_form.php :
Filtering User Input

In these practical examples, we


created simple HTML forms to
collect user input. The first
example is a login form that uses
the POST method to send data
securely to the server. The
second example is a search form
that uses the GET method to
Filtering User Input

Upon form submission, PHP


scripts (`login_process.php` and
`search_results.php`) access the
submitted data using the
`$_POST` and `$_GET`
superglobal variables,
respectively. These variables
allow developers to retrieve user
Filtering User Input

Remember that in real-


world applications,
additional security
measures and data
validation are essential to
ensure data integrity and
Lesson 7

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

The output of the code above will be:

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.

To add more functionality to a function, we


can add parameters. A parameter is just like a
variable.

You may have noticed the parentheses after


the function name, like: writeMyName(). The
parameters are specified inside the
parentheses.
Example 1 | The following example will write different first names, but the
same last name:
Example 1 | The following example will write different first names, but the
same last name:

The output of the code above will be:

My name is Joseph Dela Cruz.


My name is Gloria Dela Cruz.
My name is Pinoy Dela Cruz.
Example 2 | The following function has two
parameters:
Example 2 | The following function has two
parameters:

The output of the code above will be:

My name is Joseph Tadili.


My name is Gloria Tadili!
My name is Pinoy Tadili...
PHP Functions - Return values
Functions can also be used to return values.
Example:
PHP Functions - Return values
Functions can also be used to return values.
Example:

The output of the code above will be:


1 + 16 = 17
The PHP Date() Function
The PHP date() function formats a timestamp
to a more readable date and time.
PHP Date - Format the Date
The first parameter in the date() function
specifies how to format the date/time. It uses
letters to represent date and time formats.
Here are some of the letters that can be used:

• d - The day of the month (01-31)


• m - The current month, as a number (01-12)
• Y - The current year in four digits
Other characters, like"/", ".", or "-" can also be inserted
between the letters to add additional formatting:
PHP Date - Format the Date
PHP Date - Format the Date

The output of the code above could be


something like this:
2006/07/11
2006.07.11
PHP Date - Adding a Timestamp
The second parameter in the date() function
specifies a timestamp. This parameter is
optional. If you do not supply a timestamp, the
current time will be used.

In our next example we will use the mktime()


function to create a timestamp for tomorrow.

The mktime() function returns the Unix


timestamp for a specified date.
PHP Date - Adding a Timestamp |
Syntax
mktime(hour,minute,second,month,day,year,is_
dst)
To go one day in the future we simply add one to the day argument of
mktime():

The output of the code above could be something like this:


PHP Date - Adding a Timestamp |
Syntax
mktime(hour,minute,second,month,day,year,is_
dst)
To go one day in the future we simply add one to the day argument of
mktime():

The output of the code above could be something like this:

You might also like