Php Functions with Example


PHP Functions

A function is a block of statements that can be used repeatedly in a program. 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 will not execute immediately when a page loads.

A function will be executed by a call to the function.

There are two types of functions:

User defined Functions

A user defined function declaration starts with the word function. Information can be passed to functions through arguments.

An arguments is just like a variable. Araguments are specified after the function name, inside the parentheses.

Functions reduces the repetition of code within a program


Example 1:


  Welcome to aimtocode!

Example 2:


  Today is Thrusday

Built-in functions

PHP is very rich in terms of Built-in functions. there are more than 1000 built-in functions in PHP. To use those functions we just need to call them as per our requirement like, var_dump, fopen(), print_r(), gettype() and so on.

Read in Details

PHP Functions with Parameters

The parameters work like placeholder variables within a function; they're replaced at run time by the values (known as argument) provided to the function at the time of execution


Sum of the two numbers 18 and 2 is : 20

Passing Arguments by Reference

This means that a reference to the variable is manipulated by the function rather than a copy of the variable's value


Original Value is 13
Original Value is 18

PHP Functions returning value

The return keyword is used to return value back to the part of program, from where it was called.a


  The product is: 10