Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Jun 2010
    Posts
    22
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Session Variable variable

    session_start();
    $_SESSION['jobs_username']="john";

    $pox='jobs_username';

    $dox="_SESSION['" . $pox . "']";

    echo $$dox;



    N.B. The output should be 'john'. But I did not get the expected result. Can anyone please help me? I am grateful to this site because I got the first free help on php from this site.

  • #2
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,642
    Thanks
    2
    Thanked 405 Times in 397 Posts
    You don't need a variable variable for this; you can simply use the string as a key:
    PHP Code:
    echo $_SESSION[$pox]; 

  • #3
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    variable variables are always a terrible idea.
    This is an array, not a variable. You cannot pull a variable variable directly from an array for that you need eval. You can pull $_SESSION[$pox] though, so there is absolutely no reason to use a variable variable (as there never is anyway).


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •