View Full Version : the assignment operator

11-25-2010, 10:01 PM

what is the meaning of "=>" in a for each loop? If I remember right, code can be something like this:
foreach ($value as $temp => $output) {
//do something
}first, why is the AS keyword needed? and second, why is the "=>" operator needed in these kind of loops? I'm fluent in visual basic, and it's not nearly as complicated in that language. Just FOR and NEXT statements, that's all. the same loop in vb would be:
for each value in collection
'do something
next value

Is there any way to simplify this type of syntax? can I leave portion of it out? thanks!

11-25-2010, 10:08 PM
This explains it better that I can ...


11-25-2010, 10:42 PM

that is useful. so am I to understand that the 'AS' keyword is absolutely necessary in a foreach statement? it looks like it is.

thanks again. I did not know that a simple 'foreach' statement creates an actual copy of the array instead of manipulating the original. that's useful too. thanks!

so the 'AS' keyword has to be part of the syntax, right? because of the copying that PHP does?

11-26-2010, 12:19 AM
The AS is required for PHP foreach. In the comparison, its the same as the IN keyword in the vb foreach.
The => is not required however. Don't forget that PHP doesn't have arrays, and most of the datastructures all relate directly to the hashtable used in place of the array. The use of $key => $value is to separate the key/value combination's on the map.
To do the same in languages like C#, java and so forth generally require you to use a foreach to retrieve an entryset, which then can be separated into key/value combinations (2 steps instead of 1), OR you can retrieve either the key or value combination's. I don't think that VB, C#, java etc allow you to break key => value combination's from their hashtables directly in a foreach loop.

Missed a part of your comment too.
Objects (php5+, not that fake php4 oo) are always pass by 'reference' in php (or close enough, we're not going into this). In a foreach loop, the result of a $value should be modifiable if its a non-primitive collection of data. The by-reference appending for a foreach value should only be required in the event you are modifying primitive data.
Is this not the same behaviour in VB's primitive collections?

11-26-2010, 03:34 AM
what do you mean PHP doesn't have arrays? if I use something like:
<select name="options[]">...doesn't that specify an array? I've used that array and contents of <option> tags to correctly query data from a mysql database, so what are you trying to say about the language there? thanks.

11-26-2010, 06:50 PM
No, thats HTML, but would still sorta apply.
I mean that this:

$myArray = array();

Is not the same as this:

Object[] myObjs = new Object[2];

PHP doesn't have arrays, only hashtables. This:

$myArray = array();

Is sorta equivalent to:

Hashtable<Object, Object> myObjs = new Hashtable<Object, Object>();

This is why a comparison between a standard array to PHP's array iteration is not a fair comparison. I'll use Java since I'm most familiar with it, but the idea in C# is very similar (which you can easily compare to the VB). The same things are:

$myArray = array();
$myArray['first'] = 'cat';
$myArray['second'] = 1503;
foreach ($myArray AS $key => $value)
printf("%s => %s", $key, $value);

// Java
HashMap<String, Object> myHash = new HashMap<String, Object>();
myHash.put("first", "cat");
myHash.put("second", new Integer(1503));
for (Entry<String, Object> item : myHash.entrySet())
System.out.println(item.getKey() + " => " + item.getValue());

The biggest difference being that arrays are a sequential block of memory that is sizeof(datatype) * elements, while most other collections are simply maps of pointers to values - hashtables being a lookup off of a table using the key which provides a pointer to the location of the data (and maps have a theoretical O(1) lookup, which isn't really accurate). So PHP arrays are substantially more complicated than a standard array.