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

Ultimate guide to RESTFul API Versioning in PHP Laravel: 2 solutions
Feb 15, 2023 programming

Ultimate guide to RESTFul API Versioning in PHP Laravel: 2 solutions

API versioning is what you need if you want to change the behavior of your API or the structure and format of requests and responses. ...

17 Min Read Read More
PHP xml and json
Mar 21, 2023 programming

PHP xml and json

Today we are going to talk about working XML and JSON in PHP. XML and JSON are the most common formats that are used in APIs. knowing how to work with them is essential for any web developer. ...

9 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 Array Diff
Feb 14, 2023 programming

PHP Array Diff

Today we are going to talk about array diff functions in PHP. If you want to find the items in your array that are not present in the other arrays, array diff is your friend. ...

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