SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Hybrid View

  1. #1
    SitePoint Enthusiast
    Join Date
    Jan 2006
    Posts
    46
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    strange array behaviour

    PHP Code:
    <?php
    $map
    ['user'] = 'breadcrumb_user()';
    $map['user']['index'] = 'hello';

    print_r($map);

    echo 
    '<br />';

    print_r($map['user']);

    echo 
    '<br />';

    print_r($map['user']['index']);
    ?>
    This snippet of code does not output what I expected it to. If someone could explain it would be greatly appreciated.

    Output is:
    Code:
    Array ( [user] => breadcrumb_user() )
    breadcrumb_user()
    b

  2. #2
    SitePoint Evangelist
    Join Date
    Aug 2005
    Posts
    453
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You enclosed the function in single quotes, the array stores the string "breadcrumb_user()" not the output of the function.
    Computers and Fire ...
    In the hands of the inexperienced or uneducated,
    the results can be disastrous.
    While the professional can tame, master even conquer.

  3. #3
    SitePoint Evangelist
    Join Date
    Aug 2005
    Posts
    453
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Actually I ran your code on my machine, the output is :
    Array ( [user] => hreadcrumb_user() )
    hreadcrumb_user()
    h

    What is happening is you are manipulating the $map['user'] string array, it replaces the "b" with a "h". If you will change either the first variable to something with a second index ( ie.: $map['user']['trail']) or change the name of the second to something other than ['user'] the strange behavior will go away.
    Computers and Fire ...
    In the hands of the inexperienced or uneducated,
    the results can be disastrous.
    While the professional can tame, master even conquer.

  4. #4
    SitePoint Enthusiast
    Join Date
    Jan 2006
    Posts
    46
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by byron3@earthlink View Post
    Actually I ran your code on my machine, the output is :
    Array ( [user] => hreadcrumb_user() )
    hreadcrumb_user()
    h

    What is happening is you are manipulating the $map['user'] string array, it replaces the "b" with a "h". If you will change either the first variable to something with a second index ( ie.: $map['user']['trail']) or change the name of the second to something other than ['user'] the strange behavior will go away.
    sorry typo

    I expect it to store the string not the function but I was expecting it to error on print_r($map['user']['index']); not print the first letter of $map['user']

  5. #5
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    PHP automatically converts ['index'] into [0] when used upon a string. because a string is not an array it cannot have keys so it tries to compensate for your mistakes.

    Could be argued if PHP should do that but thats a different discussion.
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.



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
  •