473,569 Members | 2,611 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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=$firs tcall<br>\n";

if (some_condition ) {
Test(0);
}

echo "(leave) firstcall=$firs tcall<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

Jul 17 '05 #1
1 2220

On 26-Sep-2003, Michael Kochendoerfer <mk@isp.hem.d e> 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=$firs tcall<br>\n";

if (some_condition ) {
Test(0);
}

echo "(leave) firstcall=$firs tcall<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=$firs tcall<br>\n";

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

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

Test(1);

?>

(enter) firstcall=1
(enter) firstcall=0
(leave) firstcall=0
(leave) firstcall=1
--
Tom Thackrey
www.creative-light.com
Jul 17 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
3680
by: Derek Hart | last post by:
I wish to execute code from a string. The string will have a function name, which will return a string: Dim a as string a = "MyFunctionName(param1, param2)" I have seen a ton of people discuss how reflection does this, but I cannot find the syntax to do this. I have tried several code example off of gotdotnet and other articles. Can...
14
2089
by: dover | last post by:
/*Copy the line a token at a time into the output*/ copy(istream_iterator<string>(iss), istream_iterator<string>(), ostream_iterator<string>(oss, " ")); What's the meaning of this statement? Thanks!
4
1832
by: Vish | last post by:
Hi all, I am having a build error in one of the overloaded functions in my class. The function takes either a string as a parameter or a type referenced in another dll as a parameter. My class references the dll where the type of the parameter is defined. I am able to create to create my class in a form without any build errors and without...
16
3145
by: hzmonte | last post by:
Correct me if I am wrong, declaring formal parameters of functions as const, if they should not be/is not changed, has 2 benefits; 1. It tells the program that calls this function that the parameter will not be changed - so don't worry. 2. It tells the implementor and the maintainer of this function that the parameter should not be changed...
4
2484
by: Tony Lownds | last post by:
(Note: PEPs in the 3xxx number range are intended for Python 3000) PEP: 3107 Title: Function Annotations Version: $Revision: 53169 $ Last-Modified: $Date: 2006-12-27 20:59:16 -0800 (Wed, 27 Dec 2006) $ Author: Collin Winter <collinw@gmail.com>, Tony Lownds <tony@lownds.com> Status: Draft Type: Standards Track
10
12998
by: Janus | last post by:
Hi, Is there a way to pass arguments to the callback function used inside an addEventListener? I see that I can only list the name of the callback function. For eg, I use this: var boldLink=document.getElementById('cmtbold');
40
2311
by: Angus | last post by:
Hello I am writing a library which will write data to a user defined callback function. The function the user of my library will supply is: int (*callbackfunction)(const char*); In my libary do I create a function where user passes this callback function? How would I define the function?
5
2408
by: pauldepstein | last post by:
Hi all, I saw some code like this: unsigned short SomeFunc(unsigned short SomeNum, bool SomeBool, const SomeClass& SomeMem, bool(SomeClass::*AmemberFunctionOfSomeClass)(const unsigned short) const)
1
9327
by: Jorge | last post by:
On Oct 23, 10:36 am, Tuxedo <tux...@mailinator.comwrote: Yes: function first_function (PARAMETER) { preload_image = new Image(800,600); preload_image.onload = function () { photo_preloaded(PARAMETER); }; preload_image.src = 'images/photo.jpg'; }
10
2355
by: Constantine AI | last post by:
Hi i am having a little problem with an equation function that was created from all your help previously. The function works fine itself but with a small glitch within it. Here is the function code. Public Function fCalcEquation(strEquation As String) As Long Dim MyDB As DAO.Database Dim rstParameters As DAO.Recordset Dim...
0
7701
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7924
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8130
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7979
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5219
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3653
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3643
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2115
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1223
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.