Now I have some variables collected from POST

$a = $_POST['a'];
$b = $_POST['b'];
$c = $a * $b

I need to use them in 2 functions: hook_form() and the callback mail_send() the function that format and sends my email with form elements.

I have implemented the following hooks:

function MYMODULE_page() {...}
function MYMODULE_form() {...}
function MYMODULE_form_submit($form, &$form_state) {

//a custom callback mail function call in the submit.
function mail_send($form_values) {}


Now I don't know where to place those variables to have maximum visibility:

  • if I place them in MYMODULE_form() the show up in the form but they are not available in the mail_send().
  • if I place them outside everything they are not available inside functions.
  • if i place them inside a hook_init() the same as above.

I need a place where all variables are visible. Thanks

closed as off-topic by kiamlaluno Nov 28 '15 at 18:08

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions on programming, PHP, SQL, etc. that do not relate directly to Drupal are off-topic here, but can be asked on Stack Overflow." – kiamlaluno
If this question can be reworded to fit the rules in the help center, please edit the question.


For the maximum visibility use global variables like below

$a = $_POST['a'];
$b = $_POST['b'];
global $c;
$c = $a * $b

and you can anywhere like below,

function mail_send($form_values) {
  global $c;

There is no special methods exists in drupal, it is just like php.

  • 2
    Sorry, but I really need to down-vote a suggestion to use global. Use drupal_static instead. – Letharion Feb 29 '12 at 12:06
  • ohh sorry, i forgot that.But global also a correct suggestion, Please correct me If i am wrong? – sathishkumar Feb 29 '12 at 12:11
  • 1
    It is absolutely correct, as in "It can solve the problem", yes. It's just that globals are generally considered a really bad idea, and should be avoided if you want your code to hold up to some level of scrutiny. – Letharion Feb 29 '12 at 12:16
  • Thank you for your explanation!. This is best suggestion why don't you add as next answer? – sathishkumar Feb 29 '12 at 12:20

Not the answer you're looking for? Browse other questions tagged or ask your own question.