PHP Lessons - Lesson 7 - PHP Functions and Functional Programming
I think we've reached the point where it's time... it's time to finally start programming. After this lesson, you can say you've written code in PHP. Quite often, you'll need to reuse pieces of code on different pages or in different PHP files. To avoid duplicating code, PHP provides functions.
A function is an independent block of code that performs some computation. We've already encountered built-in functions in previous lessons:
time()
— returns the number of seconds since the Unix epoch.print()
,echo()
— output HTML content.floor()
— returns the integer part of a number.
Functions are typically written as: function_name()
. Like variables, function names must follow naming rules, but unlike variables, functions don’t start with a $
sign. They always include parentheses.
Defining a Function
<?php function plus_one($x) { // function definition $x = $x + 1; // function body return $x; // return value } $y = 1; $y = plus_one($y); // function call print $y; ?>
Function Components
Name: Like variable names, function names:
- Must begin with a letter or underscore.
- May contain letters, numbers, and underscores.
- Cannot contain spaces. Multi-word names use underscores or camelCase.
Body: Code block enclosed in curly braces { }
.
Return value: The value sent back with the return
keyword.
Parameters: Variables inside parentheses that receive input when calling the function.
<?php function myFunction($x1, $x2, $x3) { } $y = myFunction($z, 34, 'Hello, World!'); ?>
Parameters passed must match in number. Otherwise, an error occurs:
<?php function myFunction($x1, $x2, $x3) { } $y = myFunction(34, 'Hello, World!'); // ERROR! ?>
Example: Simple Math Function
Let’s create a function y = -2x + 4
and find the values for different x
inputs:
<?php function myFunction($x) { $y = -2 * $x + 4; return $y; } $z1 = myFunction(1); $z2 = myFunction(2); $z3 = myFunction(3); print $z1 . '<br />' . $z2 . '<br />' . $z3; ?>
This will output: 2, 0, -2
Functional Programming in PHP
<?php function incr($x) { $x++; return $x; } function decr($y) { $y--; return $y; } $z = 0; $z = incr($z); $z = incr($z); $z = incr($z); $z = incr($z); $z = decr($z); print $z; ?>
The result will be 3. You can also place function definitions below the main logic and PHP will still recognize them:
<?php $z = 0; $z = incr($z); $z = incr($z); $z = incr($z); $z = incr($z); $z = decr($z); print $z; function incr($x) { $x++; return $x; } function decr($y) { $y--; return $y; } ?>
Splitting Functions into Separate Files
Create two files:
incr.php:
<?php function incr($x) { $x++; return $x; } ?>
decr.php:
<?php function decr($y) { $y--; return $y; } ?>
Now include them in your index.php file:
<?php include 'incr.php'; include 'decr.php'; $z = 0; $z = incr($z); $z = incr($z); $z = incr($z); $z = incr($z); $z = decr($z); print $z; ?>
The result is still 3. This keeps your main code clean and organized.
There's much more to functional programming in PHP. We'll explore it further in upcoming lessons.