Advanced Search

Results 1 to 6 of 6

Thread: php

  1. #1
    Join Date
    Jan 2008
    Posts
    441
    Thanks
    67
    Thanked 4 Times in 4 Posts

    Default php

    what's the difference between these 2 symbols

    -> (reference to a method?)
    =>
    Last edited by ggalan; 05-02-2011 at 05:59 PM.

  2. #2
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,154
    Thanks
    260
    Thanked 690 Times in 678 Posts

    Default

    PHP Code:
    <?php

    $myclass
    ->mymethod();
    $myclass->myvar;

    $array = (
       
    'key1'=>'value1',
       
    'key2'=>'value2'
    );
    foreach (
    $array as $key=>$value) { /*...*/ }

    ?>
    -> is used to access things within a class.
    => is used for arrays to relate a key to a value
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  3. The Following User Says Thank You to djr33 For This Useful Post:

    ggalan (05-01-2011)

  4. #3
    Join Date
    Jan 2008
    Posts
    441
    Thanks
    67
    Thanked 4 Times in 4 Posts

    Default

    thank you. in your example, how would you use the variable $key ?
    Last edited by ggalan; 05-01-2011 at 02:56 AM.

  5. #4
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,154
    Thanks
    260
    Thanked 690 Times in 678 Posts

    Default

    PHP Code:
    $array = (
       
    'key1'=>'value1',
       
    'key2'=>'value2'
    );
    foreach (
    $array as $key=>$value) {
       echo 
    $key.'=>'.$value.'<br/>';

    Assuming your keys and values are all strings (or integers), that will display them in pairs, one per line.

    A foreach loop is a special kind of loop that goes through each entry in an array and gives you access to that value as well as it's corresponding key. This can often be useful. Sometimes you don't need the key, though, so you may want to use the second, alternative, method posted below:
    PHP Code:
    //alternative:
    foreach ($array as $value) {
       echo 
    $value.'<br/>';
    }
    ?> 

    Note that the key is required if during the loop you want to alter the original value stored in the array.
    PHP Code:
    //add 1 to each entry in an array:
    foreach ($array as $key=>$value) {
       echo 
    $array[$key] = $value+1;
    }
    ?> 
    (of course that assumes the values are all numbers-- the keys can be any valid key you'd like)
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  6. #5
    Join Date
    Jan 2008
    Posts
    441
    Thanks
    67
    Thanked 4 Times in 4 Posts

    Default

    so the key is an extra variable to alter while preserving the original array value?

  7. #6
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,154
    Thanks
    260
    Thanked 690 Times in 678 Posts

    Default

    A 'key' is the term used for the named index in an array. The 'value' is the value associated with that key. These are standard terms. More generally, you should think of an array as a set of pairs, and that for each pair the first entry is called a key and the second entry is called a value.

    You can do anything you want with them. But often, yes, you will use the key to refer back to the original entry in the array.

    Note that $key and $value will be different with each rotation through the loop, but afterward they will still be available (with values from the last rotation) just like any other variable.


    Also, there is no reason that you must name them "key" and "value". I'm just doing that for clarity. Here's an example where that is not the case:
    PHP Code:
    $ages = array(
       
    'John' => 44,
       
    'Mary' => 52,
       
    'Bill' => 37
    );
    foreach(
    $ages as $name=>$age) {
       echo 
    $name.' is '.$age.' years old.<br/>';
    }

    //or, if for some reason you want to discuss next year:
    foreach($ages as $name=>$age) {
       
    $ages[$name] = $age+1;
    }
    foreach(
    $ages as $name=>$age) {
       echo 
    $name.' will be '.$age.' years old.<br/>';
    }
    //Note: you could easily combine the two loops above into a single loop,
    //but I'm separating them specifically to show you what foreach loops can do. 
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

Bookmarks

Posting Permissions

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