PHP in Hindi Functions




PHP मे Function का उपयोग Code के Reduplication को कम करने के लिए किया जाता है. Function के उपयोग से Code की Clarity मे सुधार किया जाता है Function सरल Pieces मे Complex Problems को भी कम कर देता है.

Function Statement का एक Section होता है जिसका उपयोग किसी कार्यक्रम में बार-बार किया जा सकता है. यह Page के Load होने पर तत्काल Execute नहीं होता है इसे Function पर Call करके Execute किया जाता है.

Function Code का एक Piece है जो Parameter के रूप में Input लेता है और Processing के बाद एक Value Return देता है. PHP मे Function अन्य Programming Language के कार्यों के समान होती है. इसको Declare करने के लिए Function Keywords की आवश्यकता होती है.

Function को Body मे Define या Execute किया जाता है जो Curly Bracket के बीच मे होता है.

PHP Language में हजारों built-in Functions हैं.

Functions With Parameters

PHP आपको एक functions के अंदर अपने Parameters को पास करने का विकल्प देता है जिससे आप ज्यादा Parameters को पास कर सकते है.

For Example

<!DOCTYPE html>
<html>   
   <head>
      <title>Function with Parameters Example</title>
   </head>   
   <body>
   <body>      
      <?php
         function addNumbers($param1, $param2)
         {
            $total = $param1 + $param2;
            echo "Total of the two numbers is : $total";
         }         
         addNumbers(35, 60);     
      ?>      
   </body>
</html>

Output

Total of the two numbers is : 95

Passing Arguments by Reference

Functions को Reference द्वारा Arguments भेजना संभव है. इसका मतलब है कि Variable के Reference में Functions के द्वारा Variable की Value को कापी के बजाय हेरफेर किया जाता है.

For Example

<!DOCTYPE html>
<html>   
   <head>
      <title>Passing Arguments by Reference Example</title>
   </head>   
   <body>     
      <?php
         $original = 30;
         function changeValue(&$param)
         {
            $param = 25;
         }    
         echo "Original Value is $original <br />";
 
         changeValue( $original );
         
         echo "Changed Original Value is $original <br />";        
      ?>     
   </body>
</html>

Output

Original Value is 30
Changed Original Value is 25

Dynamic Function

Function के नाम को Variables के रूप में Assign करना मुमकिन है और फिर इन Variables को उसी तरह से व्यवहार करना चाहिए जैसा आप Function नाम ही करेंगे.

For Example

<!DOCTYPE html>
<html>   
   <head>
      <title>Dynamic Function Example</title>
   </head>   
   <body>     
      <?php
         function printText()
         {
            echo "You are calling printText function dynamically.";
         }
         $function_string = "printText";
         $function_string();        
      ?>     
   </body> 
</html>

Output

You are calling printText function dynamically.

Function Returning Value

एक Function Value या Object के साथ संयोजन के साथ Return Statement का उपयोग करके एक Value को वापस कर सकता है. Return Function के निष्पादन को बंद कर देता है और Value वापस कॉलिंग कोड को भेजता है.

For Example

<!DOCTYPE html>
<html>   
   <head>
      <title>Function Returning Value Example</title>
   </head>   
   <body>      
      <?php
         function addNumbers($param1, $param2)
         {
            $total = $param1 + $param2;
            return $total;
         }         
         $sum = addNumbers(46, 31);
         echo "Total of the two numbers is : $sum";        
      ?>      
   </body>
</html>

Output

Total of the two numbers is : 77