tags:

views:

38

answers:

3
 if (isset ($_POST['somethingA']))
 {
      //code for doing something A
 }
 elseif (isset ($_POST['somethingB']))
 {
      //code for doing something B
 }

I will need to access some data from somethingA code, into somethingB code.

How can I do that in a proper way?

Should I declare a variable outside the conditionals, work inside the conditionals, and later (bottom) I use that?

Should I work with them inside the conditionals, and, somehow, pull them out after the conditional lines?

Thanks in advance, MEM

A: 

Hello, you may do something like you said:

$innervar = null;

if (isset ($_POST['somethingA']))
 {
      $innervar = new A();
      //code for doing something A
 }
 elseif (isset ($_POST['somethingB']))
 {
      $innervar = new B();
      //code for doing something B
     }

$innervar->CommonMethod();

If you are accesing $_POST directly consider using some sort of framework like symfony, or Zend, unless you are learning or working in a very simple(house) project.

Best of luck David

David Conde
A: 

You should just declare the variable outside. So you can use and access the resources in both

Daniel Hanly
A: 

According to your clarification the comments, you want to share data between two successive executions of the PHP script. That's not a question of where to put your variables in the ocde. You want the code to run once, compute some value, and then have this value available when the code is run again.

There are two ways of doing this:

  • Put the value into a hidden input field on the webpage
  • Put the value into the user session (which means it's stored "somewhere" by the PHP runtime and made available for all subsequent requests with the same session ID)
Michael Borgwardt
I only need this value on this single page execution. Would the session be the proper thing here? Hidden field forces me to have yet another field to deal with, and I would like to avoid it. True. I want to access this value again. I have just found that, I will keep this value on the input field, clearly visible anyway, so I will place that value there, and use it on the second conditional. dconde value as answer the question. But you actually understand what was behind my question. thanks for clear thing out. Btw, if any of my above sentences, reflects any misunderstanding, let me know. :)
MEM