Ask a Question related to PHP Development, Design and Development.

  1. #1

    Default Function parameter

    Hi,

    I'm new to PHP and have a small problem. Assume you define a function
    that is called with a constant as parameter. Within that function, based
    on some condition it calls itself recursively. Example:

    <?php

    function Test($firstcall) {
    echo "(enter) firstcall=$firstcall<br>\n";

    if (some_condition) {
    Test(0);
    }

    echo "(leave) firstcall=$firstcall<br>\n";
    }

    Test(1);

    ?>

    I need that variable/parameter before and after the main part of the
    function. For the first call, the (enter) output shows that $firstcall=1,
    but the (leave) always shows $firstcall=0, if the function has been
    called recursively. I don't use parameters by reference but by value. So
    how must I declare $firstcall correctly to maintain its content within
    subsequent calls?
    As tha parameter name says, it only should be 1 for the first (= the
    outside) call.

    Thanks for enlightening a newbie :-)

    - Michael

    Michael Kochendoerfer Guest

  2. Similar Questions and Discussions

    1. How to get parameter names from defined function
      Dear develoopers, I know i can reveal the defined functions with: <?php function myrow($id, $data) { return...
    2. #40430 [NEW]: timeout parameter for function file()
      From: krid@php.net Operating system: All PHP version: 4.4.4 PHP Bug Type: Feature/Change Request Bug description: timeout...
    3. Databinding: How do I use the data as a parameter for a method or function?
      I am using a SortedList as my DataSource. However, one of the things I am using the data for is to generate the URL for the HyperLinks in my...
    4. Class as a Function Parameter...
      Hello, I'm having a problem with a custom object that I have setup in a Utilities DLL for my application call UserInfo. I use this custom Object...
    5. variable in function parameter
      Hello. Let's say I have something like this: function aa($test) { $a = 8; echo $test; } $a = 2; aa("a is ". ($a>5?"greater":"equal to or...
  3. #2

    Default Re: Function parameter


    On 26-Sep-2003, Michael Kochendoerfer <mk@isp.hem.de> wrote:
    > I'm new to PHP and have a small problem. Assume you define a function
    > that is called with a constant as parameter. Within that function, based
    > on some condition it calls itself recursively. Example:
    >
    > <?php
    >
    > function Test($firstcall) {
    > echo "(enter) firstcall=$firstcall<br>\n";
    >
    > if (some_condition) {
    > Test(0);
    > }
    >
    > echo "(leave) firstcall=$firstcall<br>\n";
    > }
    >
    > Test(1);
    >
    > ?>
    >
    > I need that variable/parameter before and after the main part of the
    > function. For the first call, the (enter) output shows that $firstcall=1,
    > but the (leave) always shows $firstcall=0, if the function has been
    > called recursively. I don't use parameters by reference but by value. So
    > how must I declare $firstcall correctly to maintain its content within
    > subsequent calls?
    > As tha parameter name says, it only should be 1 for the first (= the
    > outside) call.
    Are you sure you get firstcall=0 from the first iteration?

    I tried your code, after changing the condition to i($firstcall==1), and got
    the following:
    <?php

    function Test($firstcall) {
    echo "(enter) firstcall=$firstcall<br>\n";

    if ($firstcall==1) {
    Test(0);
    }

    echo "(leave) firstcall=$firstcall<br>\n";
    }

    Test(1);

    ?>

    (enter) firstcall=1
    (enter) firstcall=0
    (leave) firstcall=0
    (leave) firstcall=1


    --
    Tom Thackrey
    [url]www.creative-light.com[/url]
    Tom Thackrey Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

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 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139