PHP function is a block contains a piece of code which can work in many places in your program. The functions in PHP can take parameters to change the returned result according to the dynamic code inside.

When you call PHP function in many places, it takes a different input values every time and that to display another result.

The PHP user-defined function pattern should be like the following.

<?php
  function function_name_here() {
    // -- All statements here !
  }
?>

The following list shows you the type of functions

  • PHP User-defined Functions
  • PHP Predefined Functions

Anyway, Let’s see more details about it.

How the PHP Function Works?

As I mentioned before, the function is a block of code that can be used in many places inside the project, the following figure shows you how it does work.

User Defined Function—Create PHP Function

This figure shows you a page contains all user functions in PHP, to call a function from this page you have to do an invocation for it by the name. That’s all.

PHP has more than 1000 defined functions which allow you use it directly in the PHP page according to its pattern.

Anyway, in the next section, I will show you more examples for functions. Let’s move to the following part.

Create a New PHP Function

This is PHP User-defined Functions, to create a PHP function you have to use the following code.

<?php
  function name_of_function() {

    echo "Statement 1 \n";
    echo "Statement 2 \n";
    echo "Statement 3 \n";
    if ( true ) {
      echo "Statement 4 \n"; 
    }

  }
?>

The PHP user-defined function starts with predefined keyword function and then beside it the name of function ended with ( … ) braces for the parameters.

The curly braces { … } for code statements.

But, how to pass data inside this function? In the next section you I will focus on that let’s move forward.

PHP Function Parameters

Anyway, the function parameters are data that can be passed into function according to some instructions in usage.

To pass parameters into function you have to specify variables or direct values inside the braces ( … ) The variables or the direct values should be separated by commas.

function ( $var1, $var2, $var3, $var4, ... ) { … }

for example.

<?php
 
  function executing_statements( $name ) {
    echo "Hello {$name}";
  }

?>

Actually, there are no limits for function parameters. You can pass unlimited parameters but that have some instructions to be considered, such as:

  • Parameters are working according PHP data types.
  • The parameters should be separated by commas.
  • You can assign default values for parameters
  • The parameters can work as direct values or using PHP variables.

Anyway, you learned how to define a function with its parameters, but how to execute this block of statements in the PHP project? As you know, you just built the a block of code in PHP but it will not work until you call it.

In the next section I will answer this question.

Calling a PHP Function

The function calling is an invocation for a user defined function or a PHP predefined function which can be executed in its calling place.

The invocation can be happened according to your needs inside the PHP project. So, usually the function calling can be specified with two parts:

  • The function name
  • Braces beside the function name inside it its arguments

name_of_function( $arg1, $arg2 );

For example: I will call the above function example.

<?php
   executing_statements("Peter"); // Executing function in this line
?>
echo " .. Welcome to our PHP tutorial !";

The output:

Hello Peter .. Welcome to our PHP tutorial !

So when you need to print the message of the function block you can to call it in the target place as you need.

Consider that, if the function has parameters you have to pass them when you call the PHP function. Let’s move to the following section to understand more details about that.

Passing Arguments into PHP User Defined Functions

The function arguments are values that can be executed when calling the function to be a part from the function result.

The PHP arguments would be writing inside the braces when call the PHP function.

function_name( $arg1, $arg2, $arg3, ... ).

When you call a function in PHP, you have to make sure from the count of the function parameters because you will pass the same count during the function invocation. And any exception, it will show an error.

For example:

<?php 

  function name_of_func( $arg ) {
    echo "This function will print this {$arg}";
  }
  
  // Calling the function with no argument 
  name_of_func(); // => Error

  // Calling the function with its needed argument 
  name_of_func("value"); // => This function will print this value

?>

But arguments and parameters may cause misapprehension or confusion for some people, So in the following section I will cover the difference between them.

The Difference between the Function Arguments and the Function Parameters

The parameters and arguments are parts in PHP function and two both working together. So if the function has no parameters or has a parameter with default value, that potentially means there are no arguments.

The following figure shows you an example.

Parameter vs Argument

So the arguments are values and the parameters are fields of the function which accepts the values from the top and then it would be executed as the function part.

All the elements are working together in the function to giving us the final result, so it may print the result or returning it.

But how to return the result of the function at a specific point ? Let’s understand that with the following part.

Returning a Value from the PHP Function

Normally, when use or call the PHP function, you already need to execute it at a specific line in PHP. But printing the result will not give you the function purpose.

In addition, if you have an array or compound data you will not able to access it from another place. So the best solution to get the function result is, you have to return the function value. And that can be done using the return statement.

For example.

<?php 
   function users() {
     
     $array = array(
        'Ahmed', 'Michael', 'Peter', 'Khaled'
     );
     
     // return the function value
     return $array;

   }
?>

This will only expose the function result as an array content. For another example.

<?php
  function get_user_info( $username ){
 
    $user_data = [
      "name"     => "Mohammed",
      "username" => "mohammed",
      "age"      => "35 years", 
      "job"      => "Full-Stack Developer"
    ];
 
    if ( $username == 'mohammed' ) {
      return $user_data;
    }
    
    return [];
  }
?>

Once you call this function it would return the user data array. but pay attention, this will exit the function in the same line once it returns the value. So it will not execute the last line of the empty array.

Some developers, like to write HTML markups inside the PHP function, so that will allow them to execute the HTML block in any place inside the PHP script. In the following section I will clarify that, let’s move to the following section.

Embedded HTML Markups with PHP Function

You can write HTML markups inside PHP function by two ways, the first one that you can write it with quotation or as HTML markups. Let’s see that in example.

<?php 
   function html_using_quotation( $page_name ) {
     $markup = "<h1>Welcome to {$page_name}</h1>"; 
     return $markup;
   }
?>

I will use the same example but with HTML markups

<?php 
   function html_using_quotation( $page_name ) {
     ?>
     <h1>Welcome to <?= $page_name; ?></h1>
     <?php
   }
?>

PHP Recursive Function 

The PHP recursive is call the function inside itself. Note that you have to avoid the recursive when you do an invocation over 200 because that will corrupt the script immediately.

<?php 
  $x = 0;
  function Run() {
    global $x;
    
    $x++;
    if ( $x <= 10 ) {
       echo "This is loop number {$x} \n";
       Run();
    }
  }

  Run();
?>

The output would print the numbers from 1 and ending the loop with the 10 number.

PHP Function Inside Function

To defined a function within a function use the following code.

<?php 
  function x_runner() {
    echo "The x_runner function ..";
    function y_runner() {
      echo "The y_runner function ..";
    }
  }
?>

By this method, you will not able to do an invocation for the y_runner() function until you call the parent function. So you have to calling them by the below code.

<?php 
  x_runner(); // => the main function
  y_runner(); // => will not work if the main function wasn't called
?>

Wrapping Up

function is a block of code that can be used in many parts in the PHP script. there are two kinds of functions in PHP:

  • PHP User-Defined Functions
  • PHP Pre-Defined Functions

The difference between arguments and parameters that the arguments are values that can be passed when you call the function, in other hand, the parameters are the specified field of the created function.

The return statement can be used to retrieve the function result