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

Courses