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

Updated on March 31, 2022

3 Min Read
php user defined functions

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.

A function is a piece of statements that can be used over and over in a program. A function will not execute itself automatically when the code is executed, a function will alwasys be executed only when call to the function.

What is PHP Function

PHP features a huge number of built-in functions such as mathematical, string, date, cluster functions etc. It is additionally conceivable to characterize a function as per specific requirement. Such a function is called client characterized work. A function is a reusable block of statements that performs a particular assignment

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

A PHP function name must begin with a letter or an underscore. Function names are NOT case-sensitive. Tip: Allow the work a title that reflects what the function does!

Download List of Tools for Developers Now

We’ll send a download link to your inbox.

Thank You

Your Ebook is on it’s Way to Your Inbox.

PHP Defined() Function

The PHP defined() work is an inbuilt function in PHP code which checks whether a constant is defined or not. Sentence structure: bool defined($constant_name); Parameter: This work acknowledges a single parameter as specified over and depicted underneath.

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

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

There is another type of function known as PHP Parameterized functions, these are the functions with pre defined parameters. You’ll pass any number of parameters inside a function. These passed parameters act as variables in your function. They are declared inside the brackets, after the function name.

A parameter is a value you pass to a function or strategy. It can be a few value put away in a variable, or a literal value you pass on the fly. They are moreover known as arguments. some_function($some_param, “a few other param”);

These parameters are used to acknowledge inputs during runtime. Whereas passing the values during a function call, they are called arguments. An argument may be a value passed to a function and a parameter is used to hold those arguments

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

Stop Wasting Time on Servers

Cloudways handle server management for you so you can focus on creating great apps and keeping your clients happy.

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

Conclusion

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

Customer Review at

“Cloudways hosting has one of the best customer service and hosting speed”

Sanjit C [Website Developer]

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 [email protected]

×

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

Thankyou for Subscribing Us!

×

Webinar: How to Get 100% Scores on Core Web Vitals

Join Joe Williams & Aleksandar Savkovic on 29th of March, 2021.

Do you like what you read?

Get the Latest Updates

Share Your Feedback

Please insert Content

Thank you for your feedback!

Do you like what you read?

Get the Latest Updates

Share Your Feedback

Please insert Content

Thank you for your feedback!