PHP is one of the most commonly used underlying languages of the web. A server-side scripting language, PHP has been around since the early days of the internet and continues to be useful in both static and dynamic web pages and web apps. One of the main building blocks of PHP is a function. Let’s explore what a PHP function is and how it works.

What is a function?

A function, in any language, is a discrete block of code that performs a specific task. 

It may: 

Two people in office in front of computer screens

What is a PHP function? 

The user-defined function

First, you write — or declare, or define — the function. Then, you call — or invoke, or run — the function. You can write the entire function and then call it in the file, or you can call the function in the file and then write it out later. In PHP defining functions ordering doesn’t matter.

2024 PHP for Beginners: every line used is fully explained

Last Updated June 2024

  • 149 lectures
  • Beginner Level
4.8 (1,517)

PHP for Beginners: Learn to Code in PHP. Every line of code explained in detail. A true PHP for Beginners Course 2024 | By Coding Academy

Explore Course

A function is always declared with the word function. This is followed by the function name with a set of parentheses that can contain one or more variables. A function name must be unique, is not case sensitive, and cannot start with a number.

Let’s take a look at some simple examples of function code. You can try them yourself using a text editor. Don’t forget the opening <?php and closing ?> tags to ensure the editor knows you’re working in PHP.

Note: Arguments (aka parameters) are PHP variables or values that are used as inputs to the function.

Defining a function without any arguments

Syntax:

function myFunction(){
 executable code;
 }

You can tell we are declaring a function, as it has the keyword function at its start.

For example:

function printHelloWorld(){
 echo “Hello World!”;
 }

To run this function we simply write:

printHelloWorld();

Which will output:

Hello World!

A function with arguments

This is when variables or values are passed to the function.

Syntax:

function myFunction($variable1, $variable2, ...){
 executable code;
 }

For example:

function printMyText($myText) {
 echo “$myText”;
 }

To run this function we write:

 printMyText(“I love PHP!”);

Which outputs to the screen:

I love PHP!

A function with typed arguments

Syntax:

function myFunction(type $variable1, type $variable2, ...){
 executable code;
 }

An example with integer arguments:

function addTwoNumbers(int $a, int $b){
 echo $a, " + ", $b, " = ", $a + $b;
 }

To run this function we write:

 addTwoNumbers(5,3);

Which outputs to the screen:

5 + 3 = 8

A function with a default argument

Syntax:

function myFunction($myVariable1 = value){
     executable code;
 }

An example with a default argument can take an argument or use the default value instead:

 function multiplyBy10(int $a =5){
 echo $a, " x 10 = ", $a * 10;
 }

To run this function we write:

 multiplyBy10(3);
 multiplyBy10(5);

Which outputs to the screen:

3 x 10 = 30
5 x 10 = 50

A function that returns a value

Syntax:

function myFunction() {
 ...
 return myValue;
 }

The line that starts with return is known as the return statement.

An example that will return a value:

function divideTwoNumbers($a, $b) {
 $c = $a / $b;
 return $c;
 }


To run this function we write:

 echo divideTwoNumbers(10, 5);
 echo divideTwoNumbers(10, 3);

Which outputs to the screen:

2
3.3333333333333

Functions may only return one value. If you want to return more than one value, you must use an array or pass a number of arguments by reference; see just below on how to do this.

A function that passes arguments by reference (so that the argument is changed outside of the function)

Syntax:

function myFunction(&$myVariable) {
...
 do something to $myVariable;
 }

An example to add 4 to a number:

function addFour(&$a) {
 $a += 4;
 }

To run this function we write:

 $myNum = 9;
 addFour($myNum);
 echo $myNum;

Which outputs to the screen:

13

Versus the following:

function addFour($a) {
 $a += 4;
 }
 $myNum = 9;
 addFour($myNum);
 echo $myNum;

Which outputs to the screen:

9

In the first case, we are passing arguments (variables) by reference, which updates the original variable. In the second case, we are passing by value, which leaves the original variable unchanged.

Why do we use functions?

What are PHP built-in functions?

PHP built-in functions are native to the language, no need to write these functions yourself. There are thousands of built-in PHP functions to explore.

Examples include:

Can a function call another function?

Yes, it is very common for a function to call another function. 

Syntax:

function myFunction() {
 anotherAlreadyDefinedFunction();
 }

What are num1 num2?

In PHP, num1 and num2 are simply variables you forgot to start with a dollar sign ($)

What is a recursive function?

A recursive function is when someone creates a function that calls itself.

For example:

function addOneUntil10($myNumber){ 
if ($myNumber < 10) {
      echo "$myNumber\n";
     addOneUntil10($myNumber + 1);
 }
 }

To run this function we write:

addOneUntil10(3);

Which outputs to the screen:

3 4 5 6 7 8 9 

What is variable scope within a function?

If we define a variable, such as $myVariable = 5, within a function we cannot access it outside of a function. It only exists while that function is running. Trying to use $myVariable outside of the function will result in an error message.

What is a method and how is it different from a function?

A method is simply a function that belongs to an object. Since PHP can be used as an Object-Oriented language, you may use object methods.

How can you learn PHP development?

Since nearly 8 out of 10 websites use PHP in some way, it remains a server-side language that’s useful to have on your resume. To get started in PHP consider enrolling in PHP for Beginners 2020 by Patrick Morrow to understand functions or learn how to build a content management system from scratch with PHP for Beginners by Tim Buchalka.

Top courses in PHP (programming language)

APIs in PHP: from Basic to Advanced
Dave Hollingworth
4.7 (1,092)
PHP From Scratch 2024 | Beginner To Advanced
Brad Traversy
4.6 (1,029)
Bestseller
PHP for Beginners - Become a PHP Master - CMS Project
Edwin Diaz | 900,000+ Students, Coding Faculty Solutions
4.4 (24,817)
PHP with Laravel for beginners - Become a Master in Laravel
Edwin Diaz | 900,000+ Students, Coding Faculty Solutions
4.4 (12,809)
Object Oriented PHP & MVC
Brad Traversy
4.4 (4,990)
PHP Unit Testing with PHPUnit
Dave Hollingworth
4.6 (2,387)
Bestseller
PHP for Beginners
Tim Buchalka's Learn Programming Academy, Dave Hollingworth
4.6 (3,891)

More PHP (programming language) Courses

PHP (programming language) students also learn

Empower your team. Lead the industry.

Get a subscription to a library of online courses and digital learning tools for your organization with Udemy Business.

Request a demo