PHP Guide 5: Functions
A guide to creating and using functions in PHP.
Index of Contents
»The code
»Real example
»In a function
The code
Here is the basic PHP code you will need to know to create a function in PHP.
<?php
// Creating a function always begins with "function".
// This function has two arguments ($first and $last).
// Arguments are variables, arrays, and other data that are passed to the function for it to use.
function getfullname($first, $last)
{
// Usually we use "return" to return results from a function.
// These results can be used in an echo statement or as part of another function
return $first . ' ' . $last;
}
// A function does not require arguments, but you must still use parentheses.
function writerandomnum()
{
// We do not need to use "return", and can use echo to write data to our page directly.
echo rand(0,10);
}
?>
// Creating a function always begins with "function".
// This function has two arguments ($first and $last).
// Arguments are variables, arrays, and other data that are passed to the function for it to use.
function getfullname($first, $last)
{
// Usually we use "return" to return results from a function.
// These results can be used in an echo statement or as part of another function
return $first . ' ' . $last;
}
// A function does not require arguments, but you must still use parentheses.
function writerandomnum()
{
// We do not need to use "return", and can use echo to write data to our page directly.
echo rand(0,10);
}
?>
Let's use the above examples in a web page.
<?php
function getfullname($first, $last)
{
return $first . ' ' . $last;
}
function writerandomnum()
{
echo rand(0,10);
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>My Database Connection</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="description" content="My Database Connection">
<meta name="keywords" content="mysql, database, connect, php">
</head>
<body>
<?php
// Calling a function is done in the same way you call a built-in PHP function
// Notice that we use echo to get the results of this function.
echo getfullname('John', 'Smith');
// In this example we do not use echo, as the function already prints out data using the echo statement
writerandomnum();
?>
</body>
</html>
function getfullname($first, $last)
{
return $first . ' ' . $last;
}
function writerandomnum()
{
echo rand(0,10);
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>My Database Connection</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="description" content="My Database Connection">
<meta name="keywords" content="mysql, database, connect, php">
</head>
<body>
<?php
// Calling a function is done in the same way you call a built-in PHP function
// Notice that we use echo to get the results of this function.
echo getfullname('John', 'Smith');
// In this example we do not use echo, as the function already prints out data using the echo statement
writerandomnum();
?>
</body>
</html>
We can move the functions into an external file and include it on every page, thus allowing us to use the same function on multiple pages and make changes to it easily.
<?php require('inc_fuctions.php'); ?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>My Database Connection</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="description" content="My Database Connection">
<meta name="keywords" content="mysql, database, connect, php">
</head>
<body>
<?php
echo getfullname('John', 'Smith');
writerandomnum();
?>
</body>
</html>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>My Database Connection</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="description" content="My Database Connection">
<meta name="keywords" content="mysql, database, connect, php">
</head>
<body>
<?php
echo getfullname('John', 'Smith');
writerandomnum();
?>
</body>
</html>
Real example
The functions above are pretty basic. Let's do something useful with a function. This is the exact function I use on this page to display the code blocks.
<?php
function codeblock($input)
{
$string = str_replace('<', '<', $input);
$string = str_replace('>', '>', $string);
$string = str_replace('<?php', '<span class="php"><?php</span><span>', $string);
$string = str_replace('?>', '</span><span class="php">?></span>', $string);
$string = str_replace('/com/', '<span class="comment">//', $string);
$string = str_replace('/ecom/', '</span>', $string);
$string = str_replace('/tab/', ' ', $string);
$string = str_replace(PHP_EOL, '<br>', $string);
echo $string;
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>My Database Connection</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="description" content="My Database Connection">
<meta name="keywords" content="mysql, database, connect, php">
</head>
<body>
<div class="codebox"><?php
codeblock('Here is some code that will appear inside of the code box.
The text will automatically be formatted to replace various symbols with
their HTML number or name, so they do not execute as regular code.
So I can write <img src="http://www.domain.com/img.jpg"> without it
trying to display the actual image.');
?></div>
</body>
</html>
function codeblock($input)
{
$string = str_replace('<', '<', $input);
$string = str_replace('>', '>', $string);
$string = str_replace('<?php', '<span class="php"><?php</span><span>', $string);
$string = str_replace('?>', '</span><span class="php">?></span>', $string);
$string = str_replace('/com/', '<span class="comment">//', $string);
$string = str_replace('/ecom/', '</span>', $string);
$string = str_replace('/tab/', ' ', $string);
$string = str_replace(PHP_EOL, '<br>', $string);
echo $string;
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>My Database Connection</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="description" content="My Database Connection">
<meta name="keywords" content="mysql, database, connect, php">
</head>
<body>
<div class="codebox"><?php
codeblock('Here is some code that will appear inside of the code box.
The text will automatically be formatted to replace various symbols with
their HTML number or name, so they do not execute as regular code.
So I can write <img src="http://www.domain.com/img.jpg"> without it
trying to display the actual image.');
?></div>
</body>
</html>