php 为什么基本超级全局失败?

发布于 2024-12-07 22:41:57 字数 1096 浏览 1 评论 0原文

我正在使用全局变量在这样的函数之间共享变量

<?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); 
}

但是变量

fname,lname,学习年份

空得惊人!请帮我找出原因。 我收到类似错误

Notice: Undefined index: whatyear in course_registration_user_insert() (line 110 of C:\wamp\www\drupal-7.1\sites\all\modules\course_registration\course_registration.module).

i am using globals to share variables between functions like this

<?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); 
}

But the variables

fname,lname,yearofstudy

are shockingly empty! please help me figure out why.
am getting errors like

Notice: Undefined index: whatyear in course_registration_user_insert() (line 110 of C:\wamp\www\drupal-7.1\sites\all\modules\course_registration\course_registration.module).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

黑白记忆 2024-12-14 22:41:57

尝试使用这样的全局变量:

<?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副本,并且您将丢失环境变量。

Try using global variables like this:

<?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); 
}
?>

If this doesn't work, make sure these functions are called in the same php instance in the right order. If the first one is called on one page, and then insert is called on another page, a new copy of php will be opened, and you will lose your environment variables.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文