PHP Function() block of code

0
335
php functions
php functions

PHP function is a reusable piece or block of code that performs a specific action.

It can either return values when called or can simply perform an operation without returning any value. PHP has over 800 in-built functions that perform different tasks.

More on PHP defined functions

Besides the built-in PHP functions, it is possible to create your own functions.

  • A function is a block of statements that can be used repeatedly in a program.
  • A function will not execute automatically when a page loads.
  • It will be executed by a call to the function.

Creating PHP user defined function

Its very easy to create your own PHP function. Suppose you want to create a PHP function which will simply write a simple message on your browser when you will call it. Following example creates a function called writeMessage() and then calls it just after creating it.

A user-defined function declaration starts with the word function:

Note that while creating a function its name should start with keyword function and all the PHP code should be put inside { and } braces as we are going to show you in the following example

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

It will display showing this result

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

PHP Functions returning value

A function can equally return a value using the return statement in combination with a value or object. return stops the results of the function and sends the value back to the calling code.

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

Following example takes two digit parameters and add them together and then returns their sum to the calling program. NB: return keyword is used to return a value from a 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(10, 20);
         
         echo "Returned value from the function : $return_value";
      ?>
      
   </body>
</html>

It will display showing this result

Returned value from the function : 30

PHP functions by Passing Arguments by Reference

It is possible to pass arguments to functions by reference. This means that a reference to the variable is manipulated by the function rather than 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 pass an argument by reference by adding a punctuation to the variable name in either the function call or the function definition.

Follow the example below.

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

It will display showing this result

Original Value is 10
Original Value is 16



PHP Functions with Parameters
PHP gives you option to pass your parameters inside a function. You can pass as many as parameters your like. These parameters work like variables inside your function. Following example takes two integer parameters and add them together and then print 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(10, 20);
      ?>
      
   </body>
</html>

It will display showing this result

Sum of the two numbers is : 30

Setting Values for Function Parameters

You can set a parameter to have a default value if the function’s caller doesn’t pass it.

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>

It will display showing this result

This is test

Why we use Functions

  • EXCELLENT CODE ORGANIZATION – functions allow us to group blocks of related code that perform a specific task together.
  • REUSABILITY – once defined, a function can be called by a number of scripts in our PHP files. This saves us time of reinventing the wheel when we want to perform some routine tasks such as connecting to the database
  • EASY MAINTANANCE– updates to the system only need to be made in one place.





LEAVE A REPLY

Please enter your comment!
Please enter your name here