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 Method Chaining
Mar 22, 2023 programming

PHP Method Chaining

Today we are going to talk about chaining methods in PHP. It’s cleaner and more readable and I don’t have to refer to my object every time I want to run one of the methods. ...

7 Min Read Read More
PHP Array Replace
Feb 14, 2023 programming

PHP Array Replace

Today we are going to talk about array replace function in PHP. Array replace is another array function that is very simple yet a very powerful function. ...

8 Min Read Read More
PHP Simple Web Crawler
Mar 22, 2023 programming

PHP Simple Web Crawler

Today we are going to build a simple web crawler in PHP and parse the box office movies from IMDB. We can do it with PHP easily. We don’t even need any extra package. ...

14 Min Read Read More
React Beginner to Advance 2025: A Complete Guide to Building Modern Web Applications
Jul 18, 2025 programming

React Beginner to Advance 2025: A Complete Guide to Building Modern Web Applications

React JS has firmly established itself as one of the most widely adopted JavaScript libraries for building dynamic user interfaces. Whether you're crafting a small personal website or architecting a large-scale enterprise dashboard, React's component-based architecture and flexible ecosystem allow developers to create highly performant, scalable, and maintainable applications. ...

29 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 ...