Chat with us, powered by LiveChat

This website uses cookies

Our website, platform and/or any sub domains use cookies to understand how you use our services, and to improve both your experience and our marketing relevance.

Getting started with PHP: User Defined Functions with Parameters

September 24, 2015

2 Min Read
Reading Time: 2 minutes

Functions are the methods for performing specific tasks. These are called at specific events within the script. Technically, functions are the lines of code which enables you to call methods whenever required rather than writing the same lines again and again.

php user defined

Functions can be divided into 2 broad classes: Built-in and User-defined.

1. PHP Built-in Functions

PHP contains more than 1,000 built-in functions. We have already discussed a few basic PHP string functions in our last post.

2. User-defined Functions in PHP

User defined functions in PHP enable coders to create custom block of codes for specific events. Function declaration starts with the word function.

Syntax

function functionname()
   {
     Block of code;
   }

Function name should start with a letter or underscore and it can never be a number. While declaring function, the best practice is to give a name which suggests the purpose of the function.

Example

<?php
   function sum() //Declaring User Defined function
    {
      $a=3;
      $b=6;
      $a=$a+$b;
      echo $a;            
    }
      sum();//Calling a function
?>

Output

9

PHP Function Arguments

Argument is like a variable. Information is sent through arguments. You can add as many arguments as you need by separating each with a comma by function. They are specified within the parenthesis inside the function name.

Example

<?php
  function Number($Number)
   {
     echo "Phone Number is $Number"."<br/>";
   }
     Number("123223");
     Number("234324");
     Number("345435");
?>

Output

Phone Number is 123223
Phone Number is 234324
Phone Number is 345435

Passing More Than 1 Argument

You can pass more than 1 argument through a function. Consider the following example.

Example

<?php
  function Number($firstname,$lastname)
   {
    echo "Employee's full name is $firstname $lastname"."<br/>";
   }
    Number("Alex","Anderson");
    Number("John","Walker");
    Number("David","Clark");
?>

Output

Employee’s full name is Alex Anderson
Employee’s full name is John Walker
Employee’s full name is David Clark

PHP Default Argument Value

When we pass variables as parameters in a function and we don’t specify the default argument value, then it takes the default value as argument.

Example

<?php
 function setage($minage = 20) {
  echo "The height is : $minage <br>";
   }
    setage(25);
    setage(); // will use the default value of 20
    setage(40);
    setage(60);
?>

Output

The height is : 25
The height is : 20
The height is : 40
The height is : 60

Functions: Returning Values

A function may return values when we use it with return statement. Hence, it is more useful when making functions for calculations.

Example

<?php
  function Multiplication($x, $y) 
    {
     $z = $x * $y;
     return $z;
    }
     echo "5 * 10 = " . Multiplication(5, 10) . "<br>";
     echo "7 * 13 = " . Multiplication(7, 13) . "<br>";
     echo "2 * 4 = " . Multiplication(2, 4);
?>

Output

50
91
8

Hence, we have discussed here the concept of user-defined functions in PHP. If you have any suggestions regarding our “Getting Started with PHP” series, do let me in the comments section below. Plus, feel free to check Cloudways PHP Cloud Platform for your PHP app deployments.

Share your opinion in the comment section. COMMENT NOW

Share This Article

Launch PHP websites without the worry of Server Management.

Pre-Installed Optimized Stack with Git, Composer & SSH

Ahmed Khan

Ahmed was a PHP community expert at Cloudways - A Managed PHP Hosting Cloud Platform. He is a software engineer with extensive knowledge in PHP and SEO. He loves watching Game of Thrones is his free time. Follow Ahmed on Twitter to stay updated with his works. You can email him at ahmed.khan@cloudways.com

Get Our Newsletter
Be the first to get the latest updates and tutorials.

Do you like what you read?

Get the Latest Updates

Share Your Feedback

Please insert Content

Thank you for your feedback!