Problem with global variables

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Stephane Pointu

    Problem with global variables

    Hi All,

    I'm turning mad with global variables...
    In created a simple test script with 2 functions which worked fine but the
    other one i need doesn't work. Register_global s is On.

    Here are the relevant parts of my script:
    //Save form data in an array
    function add_ota_element s($form_data)
    {
    $otavars = $GLOBALS["otavars"];
    $otavars[ota_name] = $form_data[name];
    }


    function create_new_gprs _dialup()
    {
    $otavars = $GLOBALS["otavars"];
    echo $otavars[ota_name]; //To see if the form data is well saved
    }

    switch ($action)
    {
    case add_ota_element s:
    add_ota_element s($form_data);
    break;
    case create_new_gprs _dialup:
    create_new_gprs _dialup();
    break;
    }

    $otavars[ota_name] in the second function is empty. Can anybody explain to
    me why it is so. I guess it is related to the switch instruction but i
    can't understand why.

    Thanks in advance for you help.
    Stephane
  • Rainer Herbst

    #2
    Re: Problem with global variables

    Stephane Pointu schrieb:[color=blue]
    > Hi All,
    >
    > I'm turning mad with global variables...
    > In created a simple test script with 2 functions which worked fine but the
    > other one i need doesn't work. Register_global s is On.
    >
    > Here are the relevant parts of my script:
    > //Save form data in an array
    > function add_ota_element s($form_data)
    > {
    > $otavars = $GLOBALS["otavars"];
    > $otavars[ota_name] = $form_data[name];
    > }
    >
    >
    > function create_new_gprs _dialup()
    > {
    > $otavars = $GLOBALS["otavars"];
    > echo $otavars[ota_name]; //To see if the form data is well saved
    > }
    >
    > switch ($action)
    > {
    > case add_ota_element s:
    > add_ota_element s($form_data);
    > break;
    > case create_new_gprs _dialup:
    > create_new_gprs _dialup();
    > break;
    > }
    >
    > $otavars[ota_name] in the second function is empty. Can anybody explain to
    > me why it is so. I guess it is related to the switch instruction but i
    > can't understand why.
    >
    > Thanks in advance for you help.
    > Stephane[/color]

    The second function is called if the $action is set to
    "create_new_gpr s_dialup". This function does not fill the $otavars with
    the values from the form as the first function do.

    I assume that add_ota_element s() is not called in this case.

    When $otavars should be filled in this case? Have you stored the
    $otavars into a session? Don't forget that any variable is lost after
    the server finished your script, if you didn't save them into a session!

    HTH!
    Rainer


    --
    ------------------------------------------------
    Rainer Herbst Linux - Registered
    ZEIK User #319157
    Universität Potsdam Usual disclaimers applies!
    ------------------------------------------------

    Comment

    • Stephane Pointu

      #3
      Re: Problem with global variables

      Le Fri, 19 Sep 2003 11:48:24 +0200, Rainer Herbst a écrit :
      [color=blue]
      > The second function is called if the $action is set to
      > "create_new_gpr s_dialup". This function does not fill the $otavars with
      > the values from the form as the first function do.
      >
      > I assume that add_ota_element s() is not called in this case.
      >
      > When $otavars should be filled in this case? Have you stored the
      > $otavars into a session? Don't forget that any variable is lost after
      > the server finished your script, if you didn't save them into a session![/color]

      I did not put the variable into a session. I thought that i did not have
      to as i call the same script... I did not realize that there were two
      transactions. Thanks for your answer.

      Stephane

      Comment

      • hexkid

        #4
        Re: Problem with global variables

        Stephane Pointu wrote...[color=blue]
        > //Save form data in an array
        > function add_ota_element s($form_data)
        > {[/color]

        ### create a local scope (for this function) variable
        ### named otavars and set its value to the current
        ### value of $GLOBALS["otavars"]
        [color=blue]
        > $otavars = $GLOBALS["otavars"];[/color]

        ### if it doesn't exist create the index ota_name
        ### (did you forget the quotes?)
        ### of the local otavars and set its value to
        ### the value of form_data[name]
        ### again ... did you forget the quotes?
        [color=blue]
        > $otavars[ota_name] = $form_data[name];[/color]

        ### why don't you simply do
        ### $GLOBALS["otavars"][ota_name] = $form_data[name];
        ### ?
        ### or with quotes
        ### $GLOBALS["otavars"]["ota_name"] = $form_data["name"];
        [color=blue]
        > }
        >
        >
        > function create_new_gprs _dialup()
        > {
        > $otavars = $GLOBALS["otavars"];
        > echo $otavars[ota_name]; //To see if the form data is well saved[/color]

        ### echo $GLOBALS["otavars"][ota_name];
        [color=blue]
        > }[/color]

        Comment

        • Gary Petersen

          #5
          Re: Problem with global variables

          On Fri, 19 Sep 2003 04:29:07 -0500, Stephane Pointu created an award-winning
          crop circle
          <pan.2003.09.19 .09.29.06.72531 6@free.fr>, which, when translated into
          English, means this:
          [color=blue]
          > Hi All,
          >[/color]

          Hello
          [color=blue]
          > I'm turning mad with global variables...
          > In created a simple test script with 2 functions which worked fine but the
          > other one i need doesn't work. Register_global s is On.
          >
          > Here are the relevant parts of my script:
          > //Save form data in an array
          > function add_ota_element s($form_data)
          > {
          > $otavars = $GLOBALS["otavars"];[/color]

          $otavars = & $GLOBALS["otavars"];
          [color=blue]
          > [...][/color]



          Comment

          Working...