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

05 Functions

This document discusses PHP functions. It explains that functions allow you to reuse blocks of code by calling the function whenever needed. It provides examples of creating basic functions without parameters and more advanced functions that accept parameters. Functions can also return values using the return statement. The document includes examples of writing functions to output names with and without parameters, as well as a function that adds two numbers and returns the result.

Uploaded by

Jothi Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

05 Functions

This document discusses PHP functions. It explains that functions allow you to reuse blocks of code by calling the function whenever needed. It provides examples of creating basic functions without parameters and more advanced functions that accept parameters. Functions can also return values using the return statement. The document includes examples of writing functions to output names with and without parameters, as well as a function that adds two numbers and returns the result.

Uploaded by

Jothi Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Functions

PHP Functions
In this lesson we will show you how to create your own functions.
For a reference and examples of the built-in functions, please visit the W3C functions
list,
http://w3schools.com/php/default.asp

Functions
Create a PHP Function
A function is a block of code that can be executed whenever we need it.
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

Functions
Create a PHP Function
A simple function that writes my name when it is called:
<html>
<body>
<?php
function writeMyName()
{
echo "Marcus Oliver Graichen";
}
writeMyName();
?>
</body>
</html>

Functions
Use a PHP Function
Now we will use the function in a PHP script:

<?php
function writeMyName()
{
echo "Marcus Oliver Graichen";
}
echo "Hello world!<br />";
echo "My name is ";
writeMyName();
echo ".<br />That's right, ";
writeMyName();
echo " is my name.";
?>

Functions
Use a PHP Function

The output of the code will be:


Hello world!
My name is Marcus Oliver Graichen.
That's right, Marcus Oliver Graichen is my name.

Functions
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 (brackets!) after the function name, like:
writeMyName(). The parameters are specified inside the parentheses.

Functions
PHP Functions - Adding parameters
The following example will write different first names, but the same last name:

<?php
function writeMyName($fname)
{
echo $fname . " Smith.<br />";
}
echo "My name is ";
writeMyName("John");
echo "My name is ";
writeMyName("Sarah");
echo "My name is ";
writeMyName("Smith");
?>

Functions
PHP Functions - Adding parameters
The output of the code will be:

My name is John smith.


My name is Sarah Smith.
My name is Smith Smith.

Functions
PHP Functions - Adding parameters
The following function has two parameters:

<?php
function writeMyName($fname,$punctuation)
{
echo $fname . " Smith" . $punctuation . "<br />";
}
echo "My name is ";
writeMyName("John",".");
echo "My name is ";
writeMyName("Sarah","!");
echo "My name is ";
writeMyName("Smith","...");
?>

Functions
PHP Functions - Adding parameters
The output of the code will be:

My name is John Smith.


My name is Sarah Smith!
My name is Smith Smith...

Functions
PHP Functions - Return values
Functions can also be used to return values.
<html>
<body>
<?php
function add($x,$y)
{
$total = $x + $y;
return $total;
}
echo "1 + 16 = " . add(1,16);
?>
</body>
</html>

Functions
PHP Functions - Return values
The output of the code will be:

1 + 16 = 17

You might also like