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 2 of 2
  1. #1
    Regular Coder ninnypants's Avatar
    Join Date
    Apr 2008
    Location
    Utah
    Posts
    504
    Thanks
    10
    Thanked 47 Times in 47 Posts

    Automating Variables

    Is there a way to use one variables value as a variable name without code like
    PHP Code:
    if($row['user_name'] == 'Jim'){
        
    $jim $row['user_id'];
    }
    if(
    $row['user_name'] == 'ninnypants'){
        
    $ninnypants $row['user_id'];

    This is all well and good since I would know what the usernames I'm pulling out are, but is there a way to automate this so that I wouldn't have to write so many if statements?
    Last edited by ninnypants; 03-10-2009 at 05:41 PM.

  • #2
    Regular Coder Iszak's Avatar
    Join Date
    Jun 2007
    Location
    Perth, Western Australia
    Posts
    332
    Thanks
    2
    Thanked 58 Times in 57 Posts
    Something like...
    PHP Code:
    <?php

    $row
    ['user_id'] = 1;
    $row['user_name'] = 'Jim';

    ${
    strtolower($row['user_name'])} = $row['user_id'];

    echo 
    $jim// Output 1
    or
    PHP Code:
    <?php

    $row
    ['user_id'] = 1;
    $row['user_name'] = strtolower('Jim');

    $
    $row['user_name'] = $row['user_id'];

    echo 
    $jim// Output 1
    Last edited by Iszak; 03-09-2009 at 08:39 PM. Reason: damn php tags


  •  

    Posting Permissions

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