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 6 of 6
  1. #1
    Regular Coder ajetrumpet's Avatar
    Join Date
    Jul 2009
    Location
    Iowa City, IA
    Posts
    407
    Thanks
    44
    Thanked 5 Times in 5 Posts

    the assignment operator

    all,

    what is the meaning of "=>" in a for each loop? If I remember right, code can be something like this:
    PHP Code:
    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:
    Code:
    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!

  • #2
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,502
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    This explains it better that I can ...

    http://php.net/manual/en/control-structures.foreach.php

  • #3
    Regular Coder ajetrumpet's Avatar
    Join Date
    Jul 2009
    Location
    Iowa City, IA
    Posts
    407
    Thanks
    44
    Thanked 5 Times in 5 Posts
    ml,

    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?

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    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.

    Edit:
    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?
    Last edited by Fou-Lu; 11-25-2010 at 11:24 PM.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #5
    Regular Coder ajetrumpet's Avatar
    Join Date
    Jul 2009
    Location
    Iowa City, IA
    Posts
    407
    Thanks
    44
    Thanked 5 Times in 5 Posts
    what do you mean PHP doesn't have arrays? if I use something like:
    Code:
    <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.

  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    No, thats HTML, but would still sorta apply.
    I mean that this:
    PHP Code:
    $myArray = array(); 
    Is not the same as this:
    PHP Code:
    Object[] myObjs = new Object[2]; 
    PHP doesn't have arrays, only hashtables. This:
    PHP Code:
    $myArray = array(); 
    Is sorta equivalent to:
    PHP Code:
    Hashtable<ObjectObjectmyObjs = new Hashtable<ObjectObject>(); 
    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:
    PHP Code:
    $myArray = array();
    $myArray['first'] = 'cat';
    $myArray['second'] = 1503;
    foreach (
    $myArray AS $key => $value)
    {
        
    printf("%s => %s"$key$value);
    }

    // Java
    HashMap<StringObjectmyHash = new HashMap<StringObject>();
    myHash.put("first""cat");
    myHash.put("second", new Integer(1503));
    for (
    Entry<StringObjectitem 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.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)


  •  

    Posting Permissions

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