PHP Recursive Functions In Hindi

📔 : PHP 🔗

C /C++ की तरह ही PHP भी हमें recursive functions define करना allow  करती है। Function को उसी Function के अंदर एक specified condition  तक call करने को ही recursive function कहते हैं , और इस process को recursion कहते हैं।

PHP Use Of Recursive Function

1. Recursive Functions का use tree structure (read या tree structure बनाने में ) में सबसे ज्यादा  होता है , जहां पर हमें ये नहीं मालूम होता की Node का कोई children है या नहीं अगर है तो call the function.

2. दूसरा File Directories को readकरने के लिए , क्योंकि हमें नहीं मालूम होता है कि एक directory के अंदर सभी files होगीं या sub directory, और फिर उसके अंदर sub-directories और फिर उसके अंदर etc..

PHP Recursive Functions Example

recursive function के through 1 to 100 तक Print करना।

File : rec_fun.php

Copy Fullscreen Close Fullscreen
<?php
  function test_rec($number)
  {
    echo $number.' , ';
    $number++;
    if($number <= 100)
    {
      test_rec($number);
    }
  }

  test_rec(1);
?>
Output
1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 , 12 , 13 , 14 , 15 , 16 , 17 , 18 , 19 , 20 , 21 , 22 , 23 , 24 , 25 , 26 , 27 , 28 , 29 , 30 , 31, 32 , 33 , 34 , 35 , 36 , 37 , 38 , 39 , 40 , 41 , 42 , 43 , 44 , 45 ,46 , 47 , 48 , 49 ,50, 51 , 52 , 53 , 54 , 55 , 56 , 57 , 58 , 59 , 60 , 61 , 62 , 63 , 64 , 65 , 66 , 67 , 68 , 69 , 70 , 71 , 72 , 73 , 74, 75 , 76 , 77 , 78 , 79 , 80 , 81 , 82 , 83 , 84 , 85 , 86 , 87 , 88 ,89 , 90 , 91 , 92 , 93 , 94 , 95 , 96 , 97 , 98 , 99 , 100 ,

ऊपर दिए गए example में 100 बार same function call हुआ है। example को आप ध्यान से देखेंगे तो पायंगे कि एक condition दी हुई है कि agar variable की value 100 से काम या बराबर है तभी function को call किया गया है।


Note
देखा जाए तो Recursive Functions का work flow same as For Loop की तरह ही होता है , क्योंकि For Loop में भी execution एक specified condition तक repeat होता है। Condition false होने पर Loop end हो जाता था।

Recursive Functions को और अच्छे से समझने के लिए हम एक और common example देखते हैं - get factorial of any number .

File : rec_fun2.php

CopyFullscreenClose Fullscreen
<?php
  function find_fact($n) 
  {
    if ($n == 0) 
    {
      return 1;
    }
    echo $n.' * ';
    /* Recursion */
    $result = ( $n * find_fact( $n-1 ) );
    return $result;
  }

  echo "The factorial of 5 is: " . find_fact( 5 ).'<br>';
  echo "The factorial of 10 is: " . find_fact( 10 );
?>
Output
5 * 4 * 3 * 2 * 1 * The factorial of 5 is: 120
10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1 * The factorial of 10 is: 3628800

पिछले example की तरह इसमें भी एक condition दी है कि variable की value 0 होने पर सिर्फ 1 ही return करता है , otherwise same function call होता रहेगा।


Recursive Function में Loop की तरह ही condition देना mandatory है , otherwise function infinite call करता रहेगा और page breach हो सकता है।

PHP Disadvantages Of Recursive Function

Recursive Functions , iterative program (like For Loop / While Loop ) के तुलना में काफी memory और time consume करते हैं। इसलिए ज्यादा जरूरी होने पर ही Recursive Functions का use करें।

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