What is PHP Function and how to use with example?

PHP function work as other programming languages means that PHP Functions also similar to other programming Functions.

Functions we can define as its a piece of code which takes one more input in the form of the parameter and does some processing and finally it returns a value.

here, PHP gives you choose to create own functions as well.

In the programming, PHP Functions used as two parts in which should be clear to you like

Why use Functions?

Better code organization –
PHP functions allow us to perform a specific task together with group blocks of related code.

Reusability –
A function we can be called by a number of scripts in our PHP files program.
This saves us time of reinventing the wheel where we want to perform some routine tasks like as connecting to the database

Easy maintenance-
its updates to the system, when only need to be made in one place.

Built-in Functions

In PHP Package functions have existed installation package because Built-in functions are functions
PHP a very efficient and productive scripting language.

The built-in functions can be categorized into String Functions and Numeric Functions.

String Functions

These are those functions which manipulate string data, refer to the article on strings for implementation examples of string functions

Numeric Functions

Numeric functions are those function which returns numeric results.

Numeric PHP function can be used to return constants, format numbers, perform mathematical computations etc.

create your own functions

Calling a PHP Function

You know, IN PHP have more than 1000 Function which built-in library, functions created for the different area.

so, when you need just call them according to your requirement.

In most cases or area, you have no need to create own function

How to Create a PHP Function?

Its very simple to create own PHP function in programming

Suppose that you want to create an own PHP function which will write a message on your browser when you will call it.

Example:-

Let’s create a function called simpleMessage() after that calls it just creating it.

one thing is important here while creating a function its name should start with keyword function, and put all the PHP code inside { and } braces

Finally, display the following result −

Passing Arguments by Reference

In PHP It is possible to pass arguments to functions by reference.

Therefore, means that when a reference to the variable is manipulated by the function rather than a copy of the variable’s value.

In cases, Any changes made to an argument will change the value of the original variable.

SO, here in PHP, you can pass an argument by reference by adding an ampersand to the variable name in either the function call.

PHP Functions with Parameters

you can pass your parameters inside a function because of PHP Give you the option.

You have the option to pass as many as parameters your choice.

these parameters work as like variables inside the function

Let’s see the example we take three integer parameters and add them together and then print

PHP Functions returning value

In PHP When use return statement then a function return a value in conjunction with a value or object.

Here, in statement return stops the execution of the function and also sends the value back to the calling code.

using return array(1,2,3,4) then you can return more than one value from a function

see given below example here, takes three integer parameters and add them together and then returns their sum to the calling program.

one thing notice here, return keyword is used to return a value from a function.

Dynamic Function Calls

In PHP It’S possible to assign function names as strings to variables after that treat these variables exactly as you would the function name itself.
given below example shows this behavior.

Setting Default Values for Function Parameters

In PHP, we can set a parameter to have a default value when the function’s caller doesn’t pass it.

Here, function prints NULL in this case use does not pass any value to this function.