group-icon
PHP Programming
Group for all things PHP, Coding Help, Problems and Solutions
104 Members
Join this group to post and comment.
AdhikariHarika Reshma
AdhikariHarika Reshma
Computer Science
01 Feb 2019

Functions in PHP

The real power of PHP comes from its functions as it has more than 1000 built in functions.

PHP User Defined Functions: Besides the built in PHP functions,we can create our own functions.As we know " A function is a block of statements that can be used repeatedly in a program and a function will not excute immediately when a page loads it will be executed by a call to the function.

Create a user defined function in PHP: A user defined function declaration starts with the word 'function':

Syntax- function functionName(){

 Code to be executed;

}

A function name can start with a letter or underscore (not a number).

PHP function arguments: Information can be passed to functions through arguments.An argument is just like a variable.Arguments are specified after the function name,inside the paranthesis.We can add as many arguments as we want but should be seperated with comma.

We also have PHP default argument value which takes the default value as arguments.

Shruthi Pillai
Shruthi Pillai
Information Technology
2mos ago

A function is a piece of code which takes one more input in the form of parameter and does some processing and returns a value.

A function can return a value using the return statement in conjunction with a value or object. return stops the execution of the function and sends the value back to the calling code.

eg: function addfunction($n1,$n2)

{

$sum =$n1+$n2;

return $sum;

}


DHIRAJ LOTLIKAR
DHIRAJ LOTLIKAR
Information Technology
2mos ago

PHP functions are similar to other programming languages. A function is a piece of code which takes one more input in the form of parameter and does some processing and returns a value.

You already have seen many functions like fopen() and fread() etc. They are built-in functions but PHP gives you option to create your own functions as well.

There are two parts which should be clear to you −

1)Creating a PHP Function

2)Calling a PHP Function

In fact you hardly need to create your own PHP function because there are already more than 1000 of built-in library functions created for different area and you just need to call them according to your requirement.

Its very easy to create your own PHP function. Suppose you want to create a PHP function which will simply write a simple message on your browser when you will call it.

PHP also gives you option to pass your parameters inside a function. You can pass as many as parameters your like. These parameters work like variables inside your function.

It is possible to pass arguments to functions by reference. This means that a reference to the variable is manipulated by the function rather than a copy of the variable's value.

A function can return a value using the return statement in conjunction with a value or object. return stops the execution of the function and sends the value back to the calling code.

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

It is possible to assign function names as strings to variables and then treat these variables exactly as you would the function name itself.