You are on page 1of 41

PHP Basics (Part 3)

Functions, Forms
Functions
What are Functions?

• A function is a self-contained block of code that


performs a specific task.

• In PHP, there exists internal or built-in functions as


well as user-defined functions; with or without
parameters.
Types of Functions

• There are 2 types of functions:

Built-in Functions
• PHP has a huge collection of internal or built-in
functions that you can call directly within your PHP
scripts to perform a specific task, like date(),
print_r(), var_dump(), etc.
Types of Functions

• There are 2 types of functions:

User-defined Functions
• PHP also allows you to define your own functions. It
is a way to create reusable code packages that
perform specific tasks.
Advantage of Functions

• Here are some advantages of functions:

Reduces the repetition of code within a program


• Perform the same task by calling the function
without copy pasting the same block of code again
and again.
Advantage of Functions

• Here are some advantages of functions:

Codes are easier to maintain


• Any changes made inside a function is automatically
implemented at all the places without touching
several files.
Advantage of Functions

• Here are some advantages of functions:

Easier to eliminate errors


• If any error occurs, you know exactly what function
is causing the error and where to find it. Therefore,
fixing errors becomes much easier.
Advantage of Functions

• Here are some advantages of functions:

Can be reused in other applications


• By including the PHP file containing the functions
you want to use, it's easy to reuse the same
function in other applications.
Creating/Invoking Functions

• The basic syntax


of creating a
custom function
can be done by:
Creating/Invoking Functions

• Example of
creating and
invoking the
function
whatIsToday():
Functions with Parameters

• You can specify parameters when you define your


function to accept input values at run time.

• The parameters work like placeholder variables within


a function; they're replaced at run time by the values
(known as argument) provided to the function at the
time of invocation.
Functions with Parameters

• You can define as many parameters as you like. However


for each parameter you specify, a corresponding
argument needs to be passed to the function when it is
called.
Functions with Parameters

• Example of invoking
the function
getSum():
Functions with Optional Parameters &
Default Values

• In creating optional
parameters — just
insert the parameter
name, followed by an
equals (=) sign,
followed by a default
value, like this.
Returning Values

• A function can return a


value back to the script
that called the function
using the return
statement. The value may
be of any type, including
arrays and objects.
Returning Values

• A function can not return multiple values. However,


you can obtain similar results by returning an array.
Returning Values

• Example of
returning
multiple
values
trough an
array:
Passing Arguments to a Function by
Reference

• In PHP there are two ways you can pass arguments to


a function: by value and by reference.

• By default, function arguments are passed by value so


that if the value of the argument within the function
is changed, it does not get affected outside of the
function.
Passing Arguments to a Function by
Reference

• However, to allow a function to modify its arguments,


they must be passed by reference.

• Passing an argument by reference is done by


prepending an ampersand (&) to the argument name
in the function definition
Passing Arguments to a Function by
Reference

• Example of
changing
argument
values by
reference:
Variable Scope

• We can declare the variables anywhere in a PHP script.


But, the location of the declaration determines the
extent of a variable's visibility within the PHP program
i.e. where the variable can be used or accessed. This
accessibility is known as variable scope.
Variable Scope
Local Variables

• By default, variables declared within a function are


local and they cannot be viewed or manipulated
from outside of that function.
Variable Scope
Local Variables

• Example of a
function with a
local variable
declared:

• What will be
printed?
Variable Scope
Local Variables

• Example of a
function with a
local variable
declared:

• What about this


one?
Variable Scope
Global Variables

• We can also use the global keyword before the


variables inside a function.

• This keyword turns the variable into a global


variable, making it visible or accessible both inside
and outside the function, as show in the example:
Variable Scope
Global Variables

• Example of a function
with a global variable
declared:

• What do you think will


be printed on this
example?
Forms & Form Processing
What are Forms?

• Forms are required to collect different kinds of user


inputs, such as contact details like name, email
address, phone numbers, or details like credit card
information, etc.
What are Forms?

• Forms contain special elements called controls like


input box, checkboxes, radio-buttons, submit
buttons, etc.

• Users generally complete a form by modifying its


controls e.g. entering text, selecting items, etc. and
submitting this form to a web server for processing.
What are Forms?

The <form> tag is used to create an HTML form.


Here's a simple example of a login form.
Input Element

• This is the most commonly used element within


HTML forms.

• It allows you to specify various types of user input


fields, depending on the type attribute. An input
element can be of type text field, password field,
checkbox, radio button, submit button, etc.
Forms
Text Fields

• Text fields are one line areas that allow the user to
input text.
Forms
Text Area

• Textarea is a multiple-line text input control that


allows a user to enter more than one line of text.
Forms
Password Fields

• Password fields are similar to text fields. The only


difference is; characters in a password field are
masked, i.e. shown as asterisks or dots.

•••••••
Forms
Radio Buttons

• Radio buttons are used to let the user select exactly


one option from a pre-defined set of options.
Forms
Checkboxes

• Checkboxes allows the user to select one or more


options from a pre-defined set of options.
Forms
Checkboxes

• If you want to make a radio button or checkbox


selected by default, you can add the attribute
checked to the input element:
Forms
Select Boxes

• Select box is a dropdown list of options that allows user to select


one or more option from a pull-down list of options.
Forms
Submit/Reset Buttons

• A submit button is used to send the form data to a


web server. When submit button is clicked the form
data is sent to the file specified in the form's action
attribute to process the submitted data.

• A reset button resets all the forms control to default


values.
Forms
Submit/Reset Buttons

You might also like