Unit 5: Functions

4th semester


PHP Functions

To keep the browser from executing a script when the page loads, you can put your script into a function.A function will be executed by a call to the function. You may call a function from anywhere within a page.
Create a PHP Function
A function will be executed by a call to the function.
function functionName()
code to be executed;

PHP function guidelines:

  • Give the function a name that reflects what the function does
  • The function name can start with a letter or underscore (not a number)

A simple function that writes my name when it is called:

Output: My name is Arbind Mandal.
PHP Functions – Adding parameters
 To add more functionality to a function, we can add parameters. A parameter is just like a variable.Parameters are specified after the function name, inside the parentheses.
Example 1
The following example will write different first names, but equal last name:

My name is Arbind Kumar Mandal
My sister’s name is Shreya Mandal
My brother’s name is Manish Mandal
Example 2
 The following function has two parameters:

My name isArbind Kumar Mandal.
My sister’s name is Shreya MAndal!
My brother’s name is Manish?

Pass by value

Pass by reference

WAP  to store a information with an array displaying the value of array using function also display the no. of elements using  an array

PHP Functions – Return values
 To let a function return a value, use the return statement.