---
title: "PHP User Defined Functions"  
description: "In this article I will show you how to create your own function. To keep the script from being executed when the page loads, you can put it into a fun"  
author: "Anonymous User"  
published: 2011-09-11  
updated: 2019-11-24  
canonical: https://www.mindstick.com/articles/725/php-user-defined-functions  
category: "php"  
tags: ["php"]  
reading_time: 2 minutes  

---

# PHP User Defined Functions

In this [article](https://yourviews.mindstick.com/view/81489/kashmir-now-after-an-year-of-abrogation-of-article-370) I will show you how to create your own [function](https://www.mindstick.com/articles/43970/purchase-suitable-wedding-dresses-for-your-wedding-function). To keep the [script](https://www.mindstick.com/interview/2280/is-jquery-replacement-of-java-script) from being executed when the [page](https://yourviews.mindstick.com/view/83591/the-complete-guide-page-speed-optimisation-and-seo) loads, you can [put](https://answers.mindstick.com/qa/37572/should-schools-put-tracking-devices-in-students-id-cards) it into a function. A function will be executed by a call to the function. You may call a function from anywhere within a page.\

Let’s we have an example, how to implement [PHP](https://www.mindstick.com/articles/12149/php-constant-and-php-variables) functions.

##### Syntax:

```
function function_Name() {   code to be executed; }
```

Note: The function [name](https://answers.mindstick.com/qa/38468/name-the-bollywood-actor-who-has-been-named-by-the-indian-olympic-association-as-the-country-s-goodwill-ambassador-representing-the-indian-contingent-at-the-2016-rio-olympics) can start with a letter or underscore (not a number).

**Example:** Creating function with no [parameter](https://www.mindstick.com/blog/450/parameter-class-in-c-sharp)

```
<html>   <head>        <title> Function Test</title>   </head>   <body>                      <?php                //  Creating function with AdditionNum name for adding two integer value                            function AdditionNum()                            {                               $num1   = 45;                                  $num2   = 45;                                   print "Addition of two number is : ".($num1+$num2);                            }                                                        echo "This is function value : "."</br>";                     // Calling function                            AdditionNum();                                          ?>     </body>  </html>
```

##### Output:

![PHP User Defined Functions](https://www.mindstick.com/mindstickarticle/192928ef-1d2d-457e-9272-0fbae26af17a/images/3b123085-43ec-4cdb-96f0-4c3734dd867b.png)

Example: Creating function with parameter.

Let’s we have an example, how to create a function with parameter.

##### Syntax:

```
function functionName(param1, param2,...) {   code to be executed; }<html>   <head>        <title> Function with parameter</title>   </head>   <body>        <?php               functionAdditionNum($num1,$num2)                 {                      print "Addition of two number is : ".($num1+$num2);                 }                                           echo "This is function with parameter : "."</br>";       // Calling function               AdditionNum(12,13);                                          ?>     </body>  </html>
```

##### Output:

![PHP User Defined Functions](https://www.mindstick.com/mindstickarticle/192928ef-1d2d-457e-9272-0fbae26af17a/images/63798e74-a32a-4ca1-b621-c48207a56cc1.png)

##### Example: Function with ‘return’ statement.

Let’s we have an example, how to implement function with return type statement in PHP.

```
<html>   <head>        <title> Function with parameter</title>   </head>   <body>               <?php                      functionAddNum($num1,$num2)                      {                        return ($num1+$num2);                      }                // calling AddNum function                              echo "Addition of(12 + 13) : ".AddNum(12,13);              ?>     </body>  </html>
```

##### Output:

![PHP User Defined Functions](https://www.mindstick.com/mindstickarticle/192928ef-1d2d-457e-9272-0fbae26af17a/images/8fb72ce0-b994-401d-9b4d-8cb60409370a.png)

\

---

Original Source: https://www.mindstick.com/articles/725/php-user-defined-functions

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
