• Tutorial List

PHP Functions

Last updated Mar 31, 2020

PHP Functions

A PHP Function is a block of code, and it will execute the block of code when you call the Function.


There are two major types of functions in PHP –

  1. 📥 Built-in Functions
    • The Built-in Functions are already coded by PHP creators and stored as functions.
    • PHP provides us with a very large collection of built-in functions and these functions are very helpful for developing PHP applications.
  2. 👷 User-Defined Functions
    • Apart from the built-in functions, PHP also allows us to create our own custom functions and when we create our own custom functions it is called user-defined functions.

🤔 Why should you use functions?

  • You can make your code reusable by using functions.
  • Functions make your code easy to understand, easy to maintain, and easy to debug.
  • If you use functions in your application, you can easily understand which function is created for which task. But, by using functions you are not only separating the tasks, as well as you are separating the errors.

How to create a PHP Function / Function Syntax?

The PHP Functions are made by four things –

  1. Function Keyword
    • Whenever you create a PHP function you must first type the function keyword.
  2. Name of the Function
    • After typing the keyword, you need to give one space, and then give a unique name with parentheses for identifying the function. my_function_name()
  3. Code that will be executed
    • After being named, now add curly braces and inside the curly braces write the code that will be executed.
  4. Parameters or Arguments
    • The function parameters or arguments are used to make a Function dynamic.

📝 The function name must follow the function naming conventions.

PHP Function Naming Convention
Function Name must start with a letter or underscore ( _ ).
Do not use reserved keywords or predefined constants. function else(){} This is an incorrect example because else is a reserved keyword.
Note: – Function names are case-insensitive. syhello() & sayHello() Both are the same.

Syntax of a PHP function

<?php
  function function_name() {
      //Code To Be Executed;
  }
?>

How to Call a PHP Function?

We already know that to execute a function we need to call the function. So, to call a function you need to write the function name with parentheses.

<?php
  // calling a PHP Function
  function_name();
?>

You can call a function multiple times

<?php
  // calling a PHP Function Multiple Times
  function_name();
  function_name();
  function_name();
?>

PHP Function Example

<?php
  // creating a function
  function sayHello(){
      echo "Hello!";
  }

  // calling the function
  sayHello();
?>
Browser Output
Hello!

PHP Function Parameters or Arguments

The parameters or arguments of a function are used to make the same function dynamic.


What are the parameters of a function?

Parameters are the variables that are declared inside the parentheses of the function. And the function parameters are only accessible inside the function.

PHP Function Parameter or Arguments

How to pass multiple parameters inside a function?

Using comma (,) you can pass multiple parameters inside a function. The comma is used to separate multiple parameters.

Pass multiple parameters inside a PHP function

How to pass values ​​to the function parameters?

After adding the parameters inside the function, now time to pass values to the parameters.

We can pass values to the parameters when we call the Function. See the following Image –

pass values to the function parameters

📝 The order of the parameters and values ​​should be the same.


Example of a dynamic PHP Function

<?php
  function showUser($name, $email, $age){
      echo "<table>
      <tr>
      <td><strong>Name:</strong></td>
      <td>$name</td>
      </tr>
      <tr>
      <td><strong>Email:</strong></td>
      <td>$email</td>
      </tr>
      <tr>
      <td><strong>Age:</strong></td>
      <td>$age</td>
      </tr>
      </table>";
  }

  showUser("John", "[email protected]", 21);

  echo "<br>";
  showUser("Mark", "[email protected]", 28);

  echo "<br>";
  showUser("Barry", "[email protected]", 25);
?>
Browser Output
Name:John
Email:[email protected]
Age:21