Saturday, September 26, 2020

PHP Functions



Hello, dear readers! welcome back to another section of my tutorial on PHP. In this tutorial guide, we will be discussing about Functions in PHP.

PHP functions is quite similar to other Programming languages. A function can be described as a piece of code which takes one or more inputs from the user in form of parameters, then does some processing and returns a value.

From our previous tutorials, 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.

RECOMMENDED POST: PHP File Handling (I/O)

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

  • Creating a PHP Function
  • Calling a PHP Function

Truth is, in PHP you rarely need to create your own function and that is 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

Its very easy to create your own PHP function. Assume you want to create a PHP function which will write a simple message on your browser when you will call it.

Example

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

Note - while creating a function, its name should always begin with the keyword function 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 gives you an option to pass parameters inside a function. you can pass as many parameters as you like. These parameters work like variables inside your function.

Example

The following example takes two integer parameters and adds them together and then 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 is possible to pass arguments by reference. What this simply means is that a reference to the variable is manipulated by the function instead of a copy of the variable's value.

Any changes that is made to an argument in these cases is going to 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 

RECOMMENDED: PHP Basic Syntax

PHP Functions returning value

A function can return a value or set of values by using the return statement in conjunction with a value. return statement halts 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 add them together and 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

RECOMMENDED: PHP Constants


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

Example
The 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 my next tutorial post, we will be discussing about Cookies in PHP. 

Feel free to ask your questions where necessary and i 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.
Share:

0 comments:

Post a Comment

Hello dear readers! Please kindly try your best to make sure your comments comply with our comment policy guidelines. You can visit our comment policy page to view these guidelines which are clearly stated. Thank you.