php为什么基本超级全局失败?
我使用globals在函数之间共享变量,就像这样php为什么基本超级全局失败?,php,global-variables,Php,Global Variables,我使用globals在函数之间共享变量,就像这样 <?php $whatyear; $whatfirstname; $whatlastname; function mycustom_user_register_submit($form, &$form_state) { $GLOBALS["whatyear"]=$form_state['values']['yearofstudy']; $GLOBALS["whatfirstname"]
<?php
$whatyear;
$whatfirstname;
$whatlastname;
function mycustom_user_register_submit($form, &$form_state)
{
$GLOBALS["whatyear"]=$form_state['values']['yearofstudy'];
$GLOBALS["whatfirstname"]=$form_state['values']['firstname'];
$GLOBALS["whatlastname"]=$form_state['values']['lastname'];
}
function course_registration_user_insert(&$edit, $account, $category)
{
$newuserid=$account->uid;
$yearofstudy=$GLOBALS["whatyear"];
$fname=$GLOBALS["whatfirstname"];
$lname=$GLOBALS["whatlastname"];
//now use vars
drupal_set_message('dear '.$fname.' '.$lname.' ,'.'account uid is '.$account->uid);
}
尝试使用如下全局变量:
<?php
function mycustom_user_register_submit($form, &$form_state)
{
global $whatyear;
global $whatfirstname;
global $whatlastname;
$whatyear=$form_state['values']['yearofstudy'];
$whatfirstname=$form_state['values']['firstname'];
$whatlastname=$form_state['values']['lastname'];
}
function course_registration_user_insert(&$edit, $account, $category)
{
global $whatyear;
global $whatfirstname;
global $whatlastname;
$newuserid=$account->uid;
$yearofstudy=$whatyear;
$fname=$whatfirstname;
$lname=$whatlastname;
//now use vars
drupal_set_message('dear '.$fname.' '.$lname.' ,'.'account uid is '.$account->uid);
}
?>
如果这不起作用,请确保在同一个php实例中以正确的顺序调用这些函数。如果在一个页面上调用第一个,然后在另一个页面上调用insert,将打开一个新的php副本,您将丢失环境变量。请添加更多详细信息。您所说的空变量不会显示在上面的代码中。此外,如果不知道
$form\u state
是否实际包含任何内容,则无法说明发生了什么。我们假设您在调用课程注册用户插入()之前,实际上正在调用mycustom\u user\u register\u submit()
函数
function?@Pekka-查看以drupal\u set\u消息开头的最后一行(
@Pekka:变量出现在drupal\u set\u message()函数中。此外,删除第二个函数并将其添加到第一个函数中会使内容工作在drupal\u set\u消息中('您的变量是'.$GLOBALS[“whatyear”]。“…”$GLOBALS[“whatfirstname]”)。“…..”$GLOBALS[“whatlastname”]);上的完整代码应该不会有任何区别。使用$GLOBALS
应该可以很好地工作。有什么方法可以显示函数被调用的位置吗?@xthexter:am使用drupal。只要我的函数以特定的方式命名,drupal就会为我调用它们。在这种情况下,我的FN是drupal调用的钩子。但问题是关于php的不是drupal
<?php
function mycustom_user_register_submit($form, &$form_state)
{
global $whatyear;
global $whatfirstname;
global $whatlastname;
$whatyear=$form_state['values']['yearofstudy'];
$whatfirstname=$form_state['values']['firstname'];
$whatlastname=$form_state['values']['lastname'];
}
function course_registration_user_insert(&$edit, $account, $category)
{
global $whatyear;
global $whatfirstname;
global $whatlastname;
$newuserid=$account->uid;
$yearofstudy=$whatyear;
$fname=$whatfirstname;
$lname=$whatlastname;
//now use vars
drupal_set_message('dear '.$fname.' '.$lname.' ,'.'account uid is '.$account->uid);
}
?>