---
title: "Functions in PHP"  
description: "In the previous post, we discussed the loop in phpform handling in php. In this article we will talk about function in php.A function is a piece of co"  
author: "Samuel Fernandes"  
published: 2016-06-06  
updated: 2018-03-15  
canonical: https://www.mindstick.com/blog/11142/functions-in-php  
category: "php"  
tags: ["php"]  
reading_time: 3 minutes  

---

# Functions in PHP

In the previous post, we discussed the [loop in php](https://www.mindstick.com/Articles/12154/loop-in-php) & [form handling in php](https://www.mindstick.com/Articles/12155/form-handling-in-php). In this article we will talk about function in php.

A function is a piece of code which takes input in the form of parameter and process these parameter and returns a value .A function is a block of statement that can be used anywhere in the program. PHP has more than 1000 built-in function like fopen(), fclose(),fread(),etc.

PHP gives you option to create your own function. These types of function are called user-[defined functions](https://www.mindstick.com/interview/23510/what-are-all-types-of-user-defined-functions).

[User defined](https://www.mindstick.com/forum/34125/how-to-find-list-of-all-user-defined-tables-using-sql-server) functions

**It consists of two parts:-**

- Creating a PHP function

- Calling a PHP function

##### Creating a function:

Creating your own PHP function is very easy. In the below example we will create a function named Myfunction().The opening braces ‘{‘ indicates the start of the function and closing braces ‘}’ indicates the end of the function.

```
<?php
//creating a php function
function Myfunction()   // Myfunction is the name of the function(user defined).
{  echo("This is my First Function.");
}  Myfunction();  //calling the  Myfunction.
?>
```

Output:

This is my First Function

##### Passing Arguments in function

An argument is just like **variable**. Information are passed to function through arguments. These arguments are specified after the name of the function in brackets. You can pass as many arguments as you can.\

**Note:-**These arguments must be [separated by coma](https://www.mindstick.com/forum/159337/sql-data-in-one-field-separated-by-coma). Below is the example to show how [arguments are passed](https://www.mindstick.com/interview/23477/how-arguments-are-passed-by-value-or-by-reference):

```
<?phpfunction StudentName($student) //$student is the argument. It is a variable.{    echo "$student is a student of class 9.<br>";}StudentName("Ram");StudentName("Neha");StudentName("Kirti");StudentName("Medha");StudentName("Anshika");?>
```

Output:

```
Ram is a student of class 9.Neha is a student of class 9.Kirti is a student of class 9.Medha is a student of class 9.Anshika is a student of class 9.
```

##### PHP default argument values:

Below example will show you how to use a [default parameter](https://www.mindstick.com/forum/159230/is-default-parameter-values-supported-in-java):\

```
<?php
function student($name = "Ram") {    echo "The student name is : $name <br>";}
student("shyam");student(); // will use the default value of Ramstudent("Neha");student("Jagmeet");
?>
```

**Output:**

```
The student name is : shyam The student name is : Ram //Default valueThe student name is : NehaThe student name is : Jagmeet 
```

\

PHP function: Return values

To [return value](https://www.mindstick.com/forum/33675/how-to-convert-return-value-of-abmulivaluecopylabelatindex-into-nsstring-in-ios) use [return statement](https://answers.mindstick.com/qa/82430/why-do-we-use-return-statement-in-c-sharp). Below is an example shows how to use return statement:\

```
<?phpfunction add($a, $b) {
    $c = $a + $b;    return $c;
}
echo "50 + 100 = " . add(50, 100) . "<br>";echo "70 + 130 = " . add(70, 130) . "<br>";echo "20 + 40 = " . add(20, 40);?>
```

Output:

```
50 + 100 = 15070 + 130 = 20020 + 40 = 60
```

---

Original Source: https://www.mindstick.com/blog/11142/functions-in-php

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
