variables that change session variables

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

    variables that change session variables

    Hi, I'm currently writing a mulit-page form app that uses a session to
    retain data from each form element in order for the user to jump
    between pages, then the final data is passed to a calculation script.

    However, I've noticed that if I assign a session variable to another
    variable, which then performs a mathematical calculation, the session
    variable changes.

    i.e. $_SESSION["inflation"] = 4;


    $temp_inf = $_SESSION["inflation"];
    if ($_SESSION["inflation"] =="RPI") {
    $inflation = $rpi;}
    else {$inflation = ($temp_inf/100.0);}


    now it equals = 0.04

    How can I take a value from a session and perform operations on it
    without altering the original session value.


    Rgds
    Neil.
  • Per Gustafsson

    #2
    Re: variables that change session variables

    sentinel wrote:[color=blue]
    > However, I've noticed that if I assign a session variable to another
    > variable, which then performs a mathematical calculation, the session
    > variable changes.
    >
    > i.e. $_SESSION["inflation"] = 4;
    >
    >
    > $temp_inf = $_SESSION["inflation"];
    > if ($_SESSION["inflation"] =="RPI") {
    > $inflation = $rpi;}
    > else {$inflation = ($temp_inf/100.0);}
    >
    >
    > now it equals = 0.04
    >
    > How can I take a value from a session and perform operations on it
    > without altering the original session value.[/color]

    This happends because you have the register_global s directive turned on
    and thus, $_SESSION['inflation'] and $inflation contain the same value
    and if you manipulate one the other changes as well.

    There's two solutions to your problem, either change your naming (i.e.
    switch from $_SESSION['inflation'] to $_SESSION['foo'] or from
    $inflation to $bar) or turn register_global s off.


    Per Gustafsson

    --



    Comment

    Working...