...

View Full Version : Assigning values to an Array



Blue_Jeans
10-28-2007, 03:34 AM
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.

//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'],
);

//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.

Inigoesdr
10-28-2007, 03:42 AM
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.

Blue_Jeans
10-28-2007, 03:53 AM
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.

Inigoesdr
10-28-2007, 06:28 PM
No, not if you intend to use the value of one index in another like that.

CFMaBiSmAd
10-28-2007, 07:07 PM
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 -

$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 -

$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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum