Function reusable piece of code या block of code होता है जो कि कोई specific task perform करता है।  एक बार  define करने के बाद हम इन्हें  कितनी ही बार use कर सकते हैं।  PHP में 1000+ predefined useful function हैं जिन्हें  हम Built In Functions कहते  हैं।     


हालाँकि PHP हमें ये facility provide करती है कि user खुद के function define कर सके जिनहे User Defines Functions कहते हैं । Web page load होते समय कोई भी  function automatically run नहीं होता है जब तक कि हम उसे call न करें।

PHP Function Syntax

function function_name()
{
  //perform task here
  return something /*it's optional*/
}

Function में return statement optional होता है , आप चाहे तो कोई value या statement return करा सकते हैं।

PHP Function Example

File : function.php

Copy Fullscreen Close Fullscreen
<?php
  /*define the function */
  function myfun()
  {
    echo "Hell ! this is my first user defined function.";
  }

  /*now it's time to call */
  myfun();
?>
Output
Hell ! this is my first user defined function.

PHP Function : Important Tips

1. आप एक ही name के function define नहीं कर सकते , अगर आप ऐसा करते हैं तो PHP Fatal Error Generate कर देगी।

2. PHP में Functions नाम ASCII characters A to Z के लिए case insensitive होते हैं , means myfun() define किये जाने के बाद इसे आप Myfun() या MYFUN() name से भी call करा सकते हैं।

Note - PHP में functions और methods दोनों अलग - अलग हैं -

Methods  किसी Class   के अंदर define  किये जाते हैं इन्हे हमेशा Class Name या Class Object के साथ access किया जाता है हालाँकि यह method के type पर depend करता है , अगर method static हुआ तो Class Name  के साथ access किया जायेगा और अगर non-static हुआ तो Class Object के साथ   access किया जायेगा।


जबकि function out of the class define किया जाते है , ये Class / Object के context में नहीं होते इसलिए इन्हें कहीं भी access  किया जा सकता है , class के अंदर   भी  और  class के बाहर भी।

PHP Function Advantages

1.  Code Re-usability  :  functions use करने का  सबसे बड़ा advantage यही है कि , हम code को reuse कर सकते हैं।  same processing के लिए एक बार function define  करने के बाद उसे हम कही भी और कितनी बार भी use कर सकते हैं। 

2. Less Code  :  चूंकि हम same code के लिए functions  use करते हैं  जिससे Program की length कम जाती  है।

3. Reduce Coding Time : Function use करने से  coding time reduce होता है , जो कि किसी भी developer के लिए important है।

4. Easy To Understand  :  Code को समझना आसान हो जाता है।

5. Easy To Maintain :  समय के साथ - साथ हमें code update करना पड़  जाता है जिसे हम function की help से आसानी से maintain कर सकते हैं।

PHP Types Of Function

PHP में हम लगभग 7 type के functions use करते हैं।

  1. Returning Values
  2. Parameterized Functions
  3. Call By Value and Call By Reference
  4. Variable Length Argument Functions 
  5. Recursive Functions
  6. Anonymous Functions
  7. Arrow Functions

Related Topics :

Rahul Kumar

Rahul Kumar

Hi ! I'm Rahul Kumar Rajput founder of learnhindituts.com. I'm a software developer having more than 4 years of experience. I love to talk about programming as well as writing technical tutorials and blogs that can help to others. I'm here to help you navigate the coding cosmos and turn your ideas into reality, keep coding, keep learning :)

Get connected with me. :) LinkedIn Twitter Instagram Facebook

b2eprogrammers