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

PHP Advanced Series 2 - PHP Functions

The document discusses PHP functions including invoking functions, creating functions, passing arguments to functions by value, and returning values from functions. Functions allow code to be reused, reduce errors, and simplify maintenance. The key aspects covered are the syntax for functions, how to call or invoke existing functions, and how to define new functions.

Uploaded by

Jerick Almeda
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
200 views

PHP Advanced Series 2 - PHP Functions

The document discusses PHP functions including invoking functions, creating functions, passing arguments to functions by value, and returning values from functions. Functions allow code to be reused, reduce errors, and simplify maintenance. The key aspects covered are the syntax for functions, how to call or invoke existing functions, and how to define new functions.

Uploaded by

Jerick Almeda
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 21

Lesson 2:

PHP Functions
Overview
repetitive processes are likely to exist
For example:
in an e-commerce application, you might need to query a
customer’s profile information numerous times: at login, at
checkout, and when verifying a shipping address
repeating the profile querying process throughout the application
would be not only error-prone, but also a nightmare to maintain
if a new field has been added to the customer’s profile, You might
need to shift through each page of the application, modifying the
query as necessary, which would likely introducing errors in the
process

Prepared by: Ma'am Daisy 2


Overview
Solution: use Functions
a named section of code, which can be invoked as
many times as necessary
grants the convenience of a singular point of
modification if the embodied process requires
changes in the future, which greatly reduces both the
possibility of programming errors and maintenance
overhead

Prepared by: Ma'am Daisy 3


Invoking a Function
More than 1,000 standard functions are built into the
standard PHP distribution.
You can invoke the function you want simply by
specifying the function name, assuming that the function
has been made available either through the library’s
compilation into the installed distribution or via the
include() or require() statement.

Prepared by: Ma'am Daisy 4


Invoking a Function
For example:
suppose you want to raise 5 to the third power.
You could invoke PHP’s pow() function like this:

<?php
$value = pow(5,3); // returns 125
echo $value;
?>

Prepared by: Ma'am Daisy 5


Invoking a Function
If you simply want to display the output of the function, you can
forego assigning the value to a variable, like this:
<?php
echo pow(5,3);
?>

If you want to display the output of the function within a larger string,
you need to concatenate it like this:

echo "Five raised to the third


power equals ".pow(5,3).".";

Prepared by: Ma'am Daisy 6


Creating a Function
Syntax:

function function_name (parameters)


{
function-body
}

Prepared by: Ma'am Daisy 7


Creating a Function
Example of a function:
function generate_footer() {
echo "<p>Copyright &copy; 2006 W. Jason
Gilmore</p>";
}
Once a function is defined, you can then call this function as you
would any other.
<?php generate_footer(); ?>

Output:
<p>Copyright &copy; 2005 W. Jason Gilmore</p>

Prepared by: Ma'am Daisy 8


Here isanexample ofafunctionthatcancalculatetheaverage oftwo
numbers:
Prepared by: Ma'am Daisy 9
Passing Arguments by Value
Often, it’s useful to pass data into a function.
Example:
A function that calculates an item’s total cost by determining
its sales tax and then adding that amount to the price:

function salestax($price,$tax) {
$total = $price + ($price * $tax);
echo "Total cost: $total";
}

Prepared by: Ma'am Daisy 10


Passing Arguments by Value
The function accepts two parameters, $price and $tax, which are
used in the calculation.
Although these parameters are intended to be floats, because of
PHP’s loose typing, nothing prevents you from passing in variables
of any data type, but the outcome might not be as one would expect.
In addition, you’re allowed to define as few or as many parameters
as you deem necessary; there are no language-imposed constraints
in this regard.
Once you define the function, you can then invoke it. Example:
salestax(15.00,.075);

Prepared by: Ma'am Daisy 11


Passing Arguments by Value
Variables can also be passed to a function.
Example:
<?php
$pricetag = 15.00;
$salestax = .075;
salestax($pricetag, $salestax);
?>
When you pass an argument in this manner, it’s called passing by
value. This means that any changes made to those values within
the scope of the function are ignored outside of the function. If you
want these changes to be reflected outside of the function’s scope,
you can pass the argument by reference.

Prepared by: Ma'am Daisy 12


Passing Arguments by Value
Note:
You don’t necessarily need to define the function before it’s
invoked, because PHP reads the entire script into the engine
before execution.
Therefore, you could actually call salestax() before it is
defined, although such haphazard practice is not
recommended.

Prepared by: Ma'am Daisy 13


Returning Values from a Function
simply relying on a function to do something is
insufficient;
a script’s outcome might depend on a function’s
outcome, or on changes in data resulting from its
execution
Yet variable scoping prevents information from easily
being passed from a function body back to its caller, so
how can we accomplish this?
You can pass data back to the caller by way of the
return keyword.

Prepared by: Ma'am Daisy 14


Returning Values from a Function
The return() statement returns any ensuing value
back to the function caller, returning program control
back to the caller’s scope in the process.
If return() is called from within the global scope, the
script execution is terminated.

Prepared by: Ma'am Daisy 15


Returning Values from a Function
Revising the salestax() function again, suppose you
don’t want to immediately echo the sales total back to
the user upon calculation, but rather want to return the
value to the calling block:
function salestax($price,$tax){
$total = $price + ($price * $tax);
return $total;
}

Prepared by: Ma'am Daisy 16


Returning Values from a Function
Alternatively, you could return the calculation directly without even
assigning it to $total, like this:

function salestax($price,$tax=.0575) {
return $price + ($price * $tax);
}
Here’s an example of how you would call this function:
<?php
$price = 6.50;
$total = salestax($price);
?>

Prepared by: Ma'am Daisy 17


Returning Multiple Values
suppose that you’d like to create a function that retrieves
user data from a database, say the user’s name, e-mail
address, and phone number, and returns it to the caller.
Accomplishing this is much easier than you might think,
with the help of a very useful language construct,
list().
The list() construct offers a convenient means for
retrieving values from an array, like so:

Prepared by: Ma'am Daisy 18


Returning Multiple Values
<?php
$colors = array("red","blue","green");
list($red,$blue,$green) = $colors;
// $red="red", $blue="blue", $green="green"
?>
Building on this example, you can imagine how the
three prerequisite values might be returned from a
function using list():

Prepared by: Ma'am Daisy 19


Returning Multiple Values
<?php
function retrieve_user_profile() {
$user[] = "Jason";
$user[] = "jason@example.com";
$user[] = "English";
return $user;
}
list($name,$email,$language)=retrieve_user_profile();
echo "Name: $name, email: $email, preferred language:
$language";
?>

Prepared by: Ma'am Daisy 20


Returning Multiple Values
Executing this script returns:

Name: Jason,
email: jason@example.com,
preferred language: English

Prepared by: Ma'am Daisy 21

You might also like