Variable variables in function

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

    Variable variables in function

    Everything works fine if I don't use this as a function and just place it
    inside my code. When I use it as a function it will return the variables
    that are in the "else" portion, but not the if statement. I can see the
    variables and echo them inside the function, but not outside of it, but just
    on the "if" portion, the "else" part is working fine.

    I must be missing something here, any help would be appreciated!

    function SetSessionVars ($postresults)
    {
    foreach ($postresults as $key => $val)
    {
    if (is_array($val) )
    {
    foreach ($val as $subkey => $subval)
    {
    $prefix = $key . "_";
    $newvar = $prefix . $subkey;
    $_SESSION['payroll'][$key][$subkey] = $postresults[$key][$subkey];
    $$newvar = $_SESSION['payroll'][$key][$subkey];
    }
    }
    else
    {
    $_SESSION['payroll'][$key] = $postresults[$key];
    $$key = $_SESSION['payroll'][$key];
    }
    }
    }


  • Eraserhead

    #2
    Re: Variable variables in function

    I was messing around with global and didn't have it in the right place, now
    I do and it seems to be working fine. Sorry for the post.


    "Eraserhead " <eraserhead99@h otmail.com> wrote in message
    news:rKNge.3162 54$4J5.240736@f e04!news.easyne ws.com...[color=blue]
    > Everything works fine if I don't use this as a function and just place it
    > inside my code. When I use it as a function it will return the variables
    > that are in the "else" portion, but not the if statement. I can see the
    > variables and echo them inside the function, but not outside of it, but
    > just on the "if" portion, the "else" part is working fine.
    >
    > I must be missing something here, any help would be appreciated!
    >
    > function SetSessionVars ($postresults)
    > {
    > foreach ($postresults as $key => $val)
    > {
    > if (is_array($val) )
    > {
    > foreach ($val as $subkey => $subval)
    > {
    > $prefix = $key . "_";
    > $newvar = $prefix . $subkey;
    > $_SESSION['payroll'][$key][$subkey] = $postresults[$key][$subkey];
    > $$newvar = $_SESSION['payroll'][$key][$subkey];
    > }
    > }
    > else
    > {
    > $_SESSION['payroll'][$key] = $postresults[$key];
    > $$key = $_SESSION['payroll'][$key];
    > }
    > }
    > }
    >
    >[/color]


    Comment

    • Justin Koivisto

      #3
      Re: Variable variables in function

      Eraserhead wrote:
      [color=blue]
      > function SetSessionVars ($postresults){
      > foreach ($postresults as $key => $val){
      > if (is_array($val) ){
      > foreach ($val as $subkey => $subval){
      > $prefix = $key . "_";
      > $newvar = $prefix . $subkey;
      > $_SESSION['payroll'][$key][$subkey] = $postresults[$key][$subkey];
      > $$newvar = $_SESSION['payroll'][$key][$subkey];
      > }
      > }else{
      > $_SESSION['payroll'][$key] = $postresults[$key];
      > $$key = $_SESSION['payroll'][$key];
      > }
      > }
      > }[/color]

      Instead of having:
      $$key = $_SESSION['payroll'][$key];

      Why not have $ar[$key}=... At the top of the function do:
      $ar=array();

      And at the end to:
      return $ar;

      Then if you want to make the variables out of the array keys, you can
      always call extract() - otherwise, you have a nice array to play with in
      case you want to do something else with it... Something like that may
      even help in debugging so it is more obvious where variables are coming
      from. (Not that I'd play with variables like this unless it was
      /absolutely/ *unavoidable*.. .)

      --
      Justin Koivisto - justin@koivi.co m

      Comment

      • Justin Koivisto

        #4
        Re: Variable variables in function

        Justin Koivisto wrote:
        [color=blue]
        > Eraserhead wrote:
        >
        >[color=green]
        >>function SetSessionVars ($postresults){
        >> foreach ($postresults as $key => $val){
        >> if (is_array($val) ){
        >> foreach ($val as $subkey => $subval){
        >> $prefix = $key . "_";
        >> $newvar = $prefix . $subkey;
        >> $_SESSION['payroll'][$key][$subkey] = $postresults[$key][$subkey];
        >> $$newvar = $_SESSION['payroll'][$key][$subkey];
        >> }
        >> }else{
        >> $_SESSION['payroll'][$key] = $postresults[$key];
        >> $$key = $_SESSION['payroll'][$key];
        >> }
        >> }
        >>}[/color]
        >
        >
        > Instead of having:
        > $$key = $_SESSION['payroll'][$key];[/color]

        ....and $$newvar = $_SESSION['payroll'][$key][$subkey];

        (sorry hit post too quickly)

        --
        Justin Koivisto - justin@koivi.co m

        Comment

        Working...