PHP Functions

Functions in PHP


Hello folks! welcome back to a new edition of our tutorial on PHP. In this tutorial guide, we will be studying about Functions in PHP.

PHP functions are similar to the functions found in other programming languages. A function is a reusable piece of code which takes one or more inputs from the user in the form of parameter, and performs some processing and returns a value.

So far, you have already come across many functions like fopen() and fread(). They are built-in functions but PHP provides you the option to also create your own functions.


There are two parts of this tutorial which should be clear to you -

  • Creating a PHP Function
  • Calling a PHP Function

In fact you hardly need to create your own function because there are already more than 1000 of the built-in functions created for different area and you just need to call them according to your needs.

Creating a PHP Function

Creating a PHP Function

It is very easy to create your own function in PHP. Suppose you want to create a PHP function which will write a simple message on your browser when you will call it.

Example

The following example creates a function called writeMessage() and then calls it just after creating it.

Note - while creating a function, its name should begin with the keyword function and every of the PHP code should be put inside {and} braces as shown below -

<html>
   
   <head>
      <title>Writing PHP Function</title>
   </head>
   
   <body>
      
      <?php
         /* Defining a PHP Function */
         function writeMessage() {
            echo "You are really a nice person, Have a nice time!";
         }
         
         /* Calling a PHP Function */
         writeMessage();
      ?>
      
   </body>
</html>

Output

When the above code is executed, it will produce the following result -

You are really a nice person, Have a nice time!


PHP Functions with Parameters

PHP provides you with option to pass your parameters inside a function. you can pass as many parameters as you want to. These parameters works like variables inside your function.

Example

The following example takes two integer parameters and adds them together and prints them -

<html>
   
   <head>
      <title>Writing PHP Function with Parameters</title>
   </head>
   
   <body>
   
      <?php
         function addFunction($num1, $num2) {
            $sum = $num1 + $num2;
            echo "Sum of the two numbers is : $sum";
         }
         
         addFunction(30, 20);
      ?>
      
   </body>
</html>

Output

When the above code is executed, it will produce the following result -

Sum of the two numbers is : 50


Passing Arguments by Reference

It's possible to pass arguments to functions by reference, which means that a reference to the variable is controlled by the function instead of a copy of the variable's value.

Any changes made to an argument in these cases will change the value of the original variable. You can easily pass an argument by reference by adding an ampersand (&) to the variable name in either the function call or the function definition.

Example

The following example depicts both cases -

<html>
   
   <head>
      <title>Passing Argument by Reference</title>
   </head>
   
   <body>
      
      <?php
         function addFive($num) {
            $num += 5;
         }
         
         function addSix(&$num) {
            $num += 6;
         }
         
         $orignum = 10;
         addFive( $orignum );
         
         echo "Original Value is $orignum<br />";
         
         addSix( $orignum );
         echo "Original Value is $orignum<br />";
      ?>
      
   </body>
</html>

Output

When the above code is executed, it will produce the following result -

Original Value is 10
Original Value is 16 


PHP Functions returning value

A PHP function can return a value making use of the return statement in conjunction with a value. return stops the execution of the function and then sends the value back to the calling code.

You can return more than one value from a function using the return array(1, 2, 3, 4).

Example

Following example below takes two integer parameters and adds them together and it then returns their sum to the calling code.

Note - the return keyword is used to return a value from the function.

<html>
   
   <head>
      <title>Writing PHP Function which returns value</title>
   </head>
   
   <body>
   
      <?php
         function addFunction($num1, $num2) {
            $sum = $num1 + $num2;
            return $sum;
         }
         $return_value = addFunction(30, 20);
         
         echo "Returned value from the function : $return_value";
      ?>
      
   </body>
</html>

Output

When the above code is executed, it will produce the following result -

Returned value from the function : 50


Setting Default Values for Function Parameters

You can set a parameter to have a default value if the function's caller does not have it.

Example

Following function prints NULL in case use does not pass any value to this function.

<html>
   
   <head>
      <title>Writing PHP Function which returns value</title>
   </head>
   
   <body>
      
      <?php
         function printMe($param = NULL) {
            print $param;
         }
         
         printMe("This is test");
         printMe();
      ?>
      
   </body>
</html>

Output

When the above code is executed, it will produce the following result -

This is test


PHP Call Function

Dynamic Function Calls

It is possible to assign function names as strings to variables and then handle these variables in the same manner as you would handle the function name itself.

Example

Following example depicts this behaviour -

<html>
   
   <head>
      <title>Dynamic Function Calls</title>
   </head>
   
   <body>
      
      <?php
         function sayHello() {
            echo "Hello<br />";
         }
         
         $function_holder = "sayHello";
         $function_holder();
      ?>
      
   </body>
</html>

Output

When the above code is executed, it will produce the following result -

Hello


Alright guys! This is where we are rounding up for this tutorial post. In our next tutorial post, we will be discussing about Cookies in PHP

Feel free to ask your questions where necessary and we will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.

Follow us on our various social media platforms to stay updated with our latest tutorials. You can also subscribe to our newsletter in order to get our tutorials delivered directly to your emails.

Thanks for reading and bye for now.