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 Loops
Mar 21, 2023 programming

PHP Loops

Today we are going to learn about loops in PHP. Loop is another important concept in programming and we are going to learn everything about loops in PHP. ...

11 Min Read Read More
PHP Read and Write to Files
Mar 21, 2023 programming

PHP Read and Write to Files

Today we are going to learn about working with files in PHP.There are many functions that can help you read the contents of a file. we are going through the most common functions. ...

11 Min Read Read More
PHP Static
Mar 22, 2023 programming

PHP Static

Today we are going to talk about Static methods and properties in PHP. You can access the methods and properties of a class without create a new object but for that you need to know about static. ...

5 Min Read Read More
PHP calculate distance between 2 places in 9 easy steps
Feb 14, 2023 programming

PHP calculate distance between 2 places in 9 easy steps

Today we are going to talk about calculating the distance between two place in PHP in a few easy steps. ...

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