Functions in PHP | PHP Tutorial Free

Functions in PHP

Functions in PHP or in any other programming language is a block of statements that are written to perform a specific task.

– Functions once defined can be used again and again with in a program

– Functions do not execute automatically whenever a page loads.

– A function call is needed to execute a function.

There are mainly two types of functions in any programming language –

Built-in functions

Built-in functions are already defined within the programming languages and can be used anywhere in the script. PHP has over 1000 built-in functions.

User-defined functions

Like any other programming, PHP allows you to define your own customised functions based on your requirement. Such functions are known as user-defined functions.

Creating functions in PHP

While creating a function in PHP, following rules must be followed –

– Functions declaration starts with keyword “function”

– Function name must start with a letter or an underscore only.

– Function name are NOT case-sensitive in PHP


Example –


Welcome to Coding Lessons

In the above example, we’ve created a function name welcomeMsg. The above declared function is then called using weclomeMsg(); statement.

Functions with arguments in PHP

Arguments are used to pass values or data to the functions. Arguments are like variables, they can be used inside the functions –

Arguments are mentioned inside the parenthesis after the function name. A function can have multiple elements, the elements needs to be separated by comma.

In the above function firstName, we’ve an argument $fname, in which we’re passing different names. So in the output the last-name will remain same with different first names.

Also Read – IF Statement in PHP

Author: Deepak Kumar

This is Deepak Kumar, founder of True Infosystems and of-course the chief content creator at Started my carrier as a Freelancer and now a top-rated Freelancer at Upwork, delivered over 200 successful projects with highly positive user ratings and counting.