Jump to content

Isn't it recursive?


YigalB

Recommended Posts

Something bothers me:

I am writing a PHP code to implement "step by step" form. In order to do that, I copied a method which looks like:

 

Program name: doit.php :

 

if (!isset($_SESSION['para_stage'])) { ... first time to the code .... initialize variables ...display first form which calls doit.php when submit.....}

else {already been to this code, variables are initialized, do what you have to do and recall doit.php ....}

 

It works fine, but: isn't there a problem with calling the same program again and again from itself? isn;t it like a recursive, which means the stack will be used until overflow?

 

 

 

Link to comment
Share on other sites

What stack?  HTTP is stateless, meaning each time you visit the script it's a new script.

What happen to the previous variables? are they kept? erased?

sometimes the call to the HTML form is done from a procedure. There must be a stack that remembers where to return when the return command is executed.

Link to comment
Share on other sites

the previous variables are discarded. if you want to keep variables, you'll need to store them in session variables and/or a database.

i use the session variables.

I am just confused about how it works: when exactly are the variables discarded? when the user is pressing the submit button on the client side?

Link to comment
Share on other sites

But the script never actually reach the end. Consider the following php line that calls function, such as:

....

$b=do_it($a1, $a2, &a3)

if ($b==0) .....

......

 

 

The do_it function is supposed to return a value and the PHP code is expected to continue to the following if condition.

 

What will happen if the do-It function will creat a form on the client side and this form calls again the same PHP code?

The previous PHP entity is waiting for the do_it function to return a value, yet a new entity of this PHP code starts to run.

 

What did I miss?

Link to comment
Share on other sites

But the script never actually reach the end. Consider the following php line that calls function, such as:

....

$b=do_it($a1, $a2, &a3)

if ($b==0) .....

......

 

 

The do_it function is supposed to return a value and the PHP code is expected to continue to the following if condition.

 

What will happen if the do-It function will creat a form on the client side and this form calls again the same PHP code?

The previous PHP entity is waiting for the do_it function to return a value, yet a new entity of this PHP code starts to run.

 

What did I miss?

 

do_it will output the form and the script will continue until the end of file.  The form itself is merely rendered in the browser.  Once it's displayed on the screen, it's done - there's nothing running in the background waiting for user interaction.  When someone submits a form, a HTTP request is made to the server.  The server reads the action URL and serves up the correct script based on that value.  In all cases, it's a new copy of the script.

 

Put another way, it looks like you're expecting do_it to be able to read user submitted form values and use them to continue processing.  Neither PHP nor HTTP works that way.  There's no persistent MyScript.php process running in the background.

Link to comment
Share on other sites

i agree that "In all cases, it's a new copy of the script".

i also understand that " There's no persistent MyScript.php process running in the background".

 

perhaps what I miss is what is going on at the server side after the form was displayed at the client. I think I understand now that the PHP code continues running. In that case the picture is getting clear.

 

i think I can change it by changing a SESSION variable after that point, and see the new value when re-entering the PHP code. I will give it a try.

 

Thanks for the answers!

Link to comment
Share on other sites

perhaps what I miss is what is going on at the server side after the form was displayed at the client. I think I understand now that the PHP code continues running. In that case the picture is getting clear.

 

Not necessarily.  Again, the script only runs until it runs out of code to execute (end of file).  If you output your form at the end of the script, NOTHING will be running.  Moreover, again, HTTP is stateless.  Even if you have more code in your script, it can't obtain values entered into the form automatically.  It takes a form submission, which is an HTTP request, which necessitates a new copy of the page being served.

 

What are you trying to do?  And, more importantly, what results are you expecting?  I'm concerned that you're not grasping the fundamentals of what's really going on because the word 'running' is a loaded term.

Link to comment
Share on other sites

What I try to do works well. Actually I am happy i could build a simple working PHP code and install on home Ubuntu server so fast.

 

I am just bothered because it seems I don't understand the way the code works - after the form was submitted. So allow me to ask step by step: if we look at the do_it function mentioned before, which includes an HTML form, does the server ctually continues to execute the code lines after the lines which built the form?

 

Or - should I, as the code writer, add a "terminate the code here" line after the form, because I know that once the code reached here, no point of further executing?

Link to comment
Share on other sites

Let's say you have something like:

 

function makeForm()
{
   $form = '<form action="" method="post"><input type="text" name="data" /><input type="submit" name="submit" value="Submit" />';

   echo $form;
}

$numbers = (1, 4, 90, 33, 234);

makeForm();

foreach($numbers as $num)
{
   echo $num;
}

 

The form will be displayed, as well as the numbers in the $numbers array.  However, if you try to do something like:

 

echo $_POST['data'];

 

It will fail unless you check to see if the form has been submitted.  That's because although you've outputted a form, an incoming request with the form data hasn't yet been made, and you're trying to obtain a value from a variable that doesn't exist.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.