Free cookie consent management tool by TermsFeed Generator PHP function | Amir Kamizi
AMIR KAMIZI
Home Blog Courses Books Newsletter Store Membership Buy me a coffee
PHP function

PHP function

Last Updated on Mar 21, 2023

What is a function?

A function is a block of code that you can write once and use repeatedly

In PHP, like javascript and python, the function will not be executed when you run the program unless you call the function

// define the function
function nameOfTheFunction(){
     // what it does
}

// call the function
nameOfTheFunction();

Function Arguments

a function can have no argument, one argument or many arguments.

// define the function
function nameOfTheFunction($argumentA,$argumentB){
     // what it does
}
// call the function
nameOfTheFunction("a","b");

Argument's data type

By default you don’t need to specify the data type of the arguments but if you want you can easily add it as well.

function nameOfTheFunction(int $argumentA){
     // what it does
}

when you specify the datatypes if the value that is passed is not the same data type you will get an error for example for the function above this will cause an error:

nameOfTheFunction("hello");
// Uncaught TypeError: Argument 1 Passed to nameOfTheFunction() must be of the type int, string given

Argument's default value

if a function has argument and when we call the function, we don’t pass any arguments to the function it will show us an error. You can add a default value so if the argument was not passed to the function your function would still work.

function addNumbers($argumentA,$argumentB){
     echo $argumentA + $argumentB;
}
addNumbers(); // error

and if we add default values:

function addNumbers($argumentA = 2,$argumentB = 5){
     echo $argumentA + $argumentB;
}
addNumbers();        // 2+5=7
addNumbers(3);      // 3+5=8
addNumbers(3,3);   // 3+3=6

Function return

If your function doesn’t return anything you don’t need to specify that, it's not like Java that you have to write void. And even if it returns something still you don’t need to specify it, but if you want to specify it you can.

add ":" after the ")" and before the "{" and write the data type that is going to be returned. now if your function doesn't return anything or it returns something that doesn't have the same data type of what you specified PHP will give you an error.

function addNumbers($argumentA = 2,$argumentB = 5) : int
{
     return $argumentA + $argumentB;
}

2 Issues

Now let’s talk about 2 issues that programmers who have experienced javascript and python will face.

Function arguments vs javascript

In javascript you have an argument variable that you can check. So even if your function gets 100 argument you can get them with argument[100] and don’t even specify 100 argument in your function definition

Solution

if you want to have such behavior in PHP pass your arguments as an array to the function and then work with it. Something like this:

function addNumbers(array $arguments = []){
     return $arguments[0] + $arguments[1];
}

Function arguments vs python

The issue that python developers might face is that in PHP function arguments are not named so they have to be passed in correct order.

Solution

If you want to have such behaviour in php you can use an associative array as your argument. and then when your call the function with the array argument you will get the same result:

function divide(array $arguments = []){
     return $arguments['x'] / $arguments['y'];
}
echo divide(['x' => 6, 'y' => 2]);  // 3
echo divide(['y' => 2, 'x' => 6]);  // 3

https://youtu.be/yDGVb5Y9wYg

Conclusion

Now you know about functions in PHP.

I recommend you to open a PHP files and try define multiple functions. with default argument values and without it. with return type and without it. with argyment type and without it.

If you have any suggestions, questions, or opinions, please contact me. I’m looking forward to hearing from you!

Key takeaways

  • functions in PHP
  • argument types
  • default argument value
  • return type
  • php functions vs python
  • php functions vs javascript

 

Category: programming

Tags: #php

Join the Newsletter

Subscribe to get my latest content by email.

I won't send you spam. Unsubscribe at any time.

Related Posts

PHP range
Feb 15, 2023 programming

PHP range

Today we are going to talk about range function in PHP. It's a very simple yet very powerful tool and can save you a lot of time. it's also more readable than the alternatives. ...

3 Min Read Read More
PHP Array Merge
Feb 14, 2023 programming

PHP Array Merge

Today we are going to talk about array merge function in PHP. This function is very useful and merges all the arrays that you pass as arguments and returns an array. ...

3 Min Read Read More
New PHP Tools You’ve Probably Never Heard Of
Jul 15, 2025 programming

New PHP Tools You’ve Probably Never Heard Of

The PHP ecosystem is constantly evolving, with fresh packages, libraries, and tools emerging that aim to solve old problems in new ways. There’s a growing landscape of lesser-known tools quietly gaining traction within the PHP community. ...

10 Min Read Read More
AngularJS Beginner to Advance 2025: The Complete Guide for Modern Web Developers
Jul 18, 2025 programming

AngularJS Beginner to Advance 2025: The Complete Guide for Modern Web Developers

In today’s web development landscape, AngularJS may no longer dominate headlines, but it still powers countless legacy applications. Many organizations—especially in finance, healthcare, and enterprise software—rely on AngularJS systems that require ongoing maintenance and enhancements. ...

43 Min Read Read More

Recommended Courses

Introduction to Machine Learning in PHP

Introduction to Machine Learning in PHP

Learn to Build Different Machine Learning Models Easily ...

PHP Tutorial Beginner to Advanced

PHP Tutorial Beginner to Advanced

Learn everything you need to start a successful career as a PHP developer ...