Ask a Question related to PHP Development, Design and Development.
-
Michael Kochendoerfer #1
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
-
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... -
#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... -
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... -
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... -
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... -
Tom Thackrey #2
Re: Function parameter
On 26-Sep-2003, Michael Kochendoerfer <mk@isp.hem.de> wrote:
Are you sure you get firstcall=0 from the first iteration?> 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.
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



Reply With Quote

