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.

Launch PHP Websites and Applications in seconds through our 1-Click App Install Launch Now!

Getting started with PHP: User Defined Functions with Parameters

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

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

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

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

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

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

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

Launch PHP websites without the worry of Server Management.

Pre-Installed Optimized Stack with Git, Composer & SSH

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

THERE’S MORE TO READ.