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 5 of 5
  1. #1
    New Coder
    Join Date
    Aug 2007
    Posts
    58
    Thanks
    2
    Thanked 6 Times in 6 Posts

    Assigning values to an Array

    I need to know exactly what the difference between these two syntax are. I was taught that there was no difference by several people. I looked through php.net and could not find my answer.
    PHP Code:
    //This doesn't work as intended;
        
    $server=array(
            
    'name' => getenv('SERVER_NAME'),
            
    'url' => 'http://' $server['name'],
            
    'requested_url' => getenv('REQUEST_URI'),
            
    'current_url' => $server['url'] . $server['requested_url'],
        ); 
    PHP Code:
    //This does;
        
    $server['name'] = getenv('SERVER_NAME');
        
    $server['url'] = 'http://' $server['name'];
        
    $server['requested_url'] = getenv('REQUEST_URI');
        
    $server['current_url'] = $server['url'] . $server['requested_url']; 
    What happens in the first code is that $server['url] will return only http://, not the rest. $server['current_url'] returns a blank. I believe what is going on is that you can't access a key until after the array() is closed. I need to confirm this. It would make no sense to me for this to be the case. I am hoping that I am doing something incorrectly, but it gets through the parser somehow.

    If you could point me to something somewhere that says you can't do what I did in the first example (somewhere like php.net), I would appreciate it. I would much rather do things the way I want the first example to function. This example would be relatively easy to change each value to the one I want, but for readability's sake I don't want to. I do have much more complicated variables built on a similar fashion that would be a bugger to do.

  • #2
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,642
    Thanks
    2
    Thanked 405 Times in 397 Posts
    The first doesn't work because $server['name'] doesn't have a value yet when you try to use it. The array hasn't been defined yet and you're trying to call it. You have to use the second method if you want to define the array using those values.

  • #3
    New Coder
    Join Date
    Aug 2007
    Posts
    58
    Thanks
    2
    Thanked 6 Times in 6 Posts
    Great... just great...
    Is there a similar syntax I can use to the first to accomplish what I want? I don't think there is but I'm hoping... It is much easier to use the first syntax because if I ever change the name of the variable, the first syntax makes it easy. Change one tag, you have them all. The second... I'd have to edit a quite a lot of lines of code.
    Last edited by Blue_Jeans; 10-28-2007 at 02:55 AM.

  • #4
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,642
    Thanks
    2
    Thanked 405 Times in 397 Posts
    No, not if you intend to use the value of one index in another like that.

  • #5
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,020
    Thanks
    2
    Thanked 313 Times in 305 Posts
    The first form does not satisfy your argument (assuming it worked) of changing the name in one place either. You must change it three places inside of the array() statement in addition to the left-hand side of the assignment -
    Code:
        $server=array( 
            'name' => getenv('SERVER_NAME'), 
            'url' => 'http://' . $server['name'], 
            'requested_url' => getenv('REQUEST_URI'), 
            'current_url' => $server['url'] . $server['requested_url'], 
        );
    For a truly "change it in one place" version -
    Code:
        $server=array( 
            'name' => getenv('SERVER_NAME'), 
            'url' => 'http://' . getenv('SERVER_NAME'), 
            'requested_url' => getenv('REQUEST_URI'), 
            'current_url' => 'http://' . getenv('SERVER_NAME') . getenv('REQUEST_URI'), 
        );
    Otherwise, create a user defined function that returns the array and assign it to your variable.

    Edit: BTW, the first form generates messages like the following to let you know that it won't work -
    Notice: Undefined variable: server in ...\yourfile.php on line x
    Last edited by CFMaBiSmAd; 10-28-2007 at 06:17 PM.
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.


  •  

    Posting Permissions

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