Breadcrumbs

PHP Functions

Like most programming languages, PHP provides functions built in and you can make your own. Making your own allows you to create a function and when it is called executes a block of code. Let's start with the proper framework of a function:

Code: PHP

<?php
function functionname() {
    
Code to be executed
}
?>

Then when you call the function you use the code:

Code: PHP

<?php
functionname
();
?>

Now when you call a function the code within the curved braces is executed ({ }). Using functions allows you to keep code more organized and conserve disk space for code that you may use multiple times. You can also use functions to create reusable blocks of code that you use often in many programs, such as a function that does a simple mathematical equation and returns the value. Functions are a great thing to use and can make use when doing advanced PHP work with things such as creating Content Management Systems and you may have a code to display the users username via their user id so you can make a function to do the needed operations and use it whenever you need to.

Now lets make a simple function that prints out a simple string:

Code: PHP

<?php
function myfunction() {
    echo 
"This is a function";
}
?>

When you call this function it will print the string "This is a function" to the screen. Note that the keyword function declares the start of a function declaration, so forgetting it could cause an error in your code.

Variable declared within a function aren't global, meaning that you cannot use them outside of the function. So this code won't work as expected.

Code: PHP

<?php
function myfunction() {
    
$variable "This is a function";
}

myfunction();
echo 
$variable;
?>

As the variable named $variable is declared and assigned a value within a function the variable will not have the value you intend that it was assigned in the function. Though like most programming language PHP provides a way to return the values of variables within functions. Here is an example of how it is done:

Code: PHP

<?php
function myfunction() {
    
$variable "This is a function";
    return 
$variable;
}

$string myfunction();
echo 
$string;
?>

As you see the return value of myfunction() is assigned to a variable $string and then printed out, this is the way to pass back variables from functions. You can also make the variables within the function globals by declaring them global variables.

You can also pass values to the function via parameters. The parameters are held within the parentheses (). In the declaration of a function it looks like this:

Code: PHP

<?php
function add($num1$num2) {
    
$sum $num1 $num2;
    return 
$sum;
}
?>

This declares that for the function to properly work if must have two parameters. Then the function goes on to add those two numbers and return the sum. This serves the same function as just adding variables, but yet is a good demonstration of using parameters in functions. Now to this code we add a function calling:

Code: PHP

<?php
function add($num1$num2) {
    
$sum $num1 $num2;
    return 
$sum;
}

$all add(12);
echo 
$all;
?>

When you call the function you are required to have the needed parameters and for it to work properly you have to have the correct values in the order that the parameters are declared.

Unlike some programming languages in PHP you cannot override a function meaning have the same function with different numbers of parameters and different actions also if you want. So make sure to only declare a function once or it may cause errors in your pages.

Along with making your own functions, PHP has built it ones that you will learn as you continue in your PHP programming, but now you are able to keep your code much more organized using functions.

Tags: