www.webdeveloper.com
Results 1 to 5 of 5

Thread: Is string an array?

  1. #1
    Join Date
    Oct 2009
    Posts
    53

    Is string an array?

    Hello
    This is something strange I remarked while I was making an exercise.

    PHP Code:
            $br '<br />';
            
    $some_text "I am here.";

            echo 
    $some_text[5]; // output 'h'
            //this means $some_text is an array but ....
            
    echo $br;
            echo 
    strlen($some_text) . $br $some_textsizeof($some_text) . $br;
            
    //for ($i = 0; $i < sizeof($some_text);$i++)
            // does not work because sizeof() which we use for arrays gives:1 !!
            
    for ($i 0$i strlen($some_text);$i++) {
                echo 
    $some_text[$i] . $br;
            } 

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,151
    It is not an array, but PHP allows you to use array-like bracket notation to reference a specific character within the string, essentially treating it like a special case of arrays, though you could not use the string as an argument to functions which require an array (e.g. foreach()).
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Mar 2007
    Posts
    946
    "Hippies.They're everywhere. They wanna save the earth, but all they do is smoke pot and smell bad."-Cartman

  4. #4
    Join Date
    Oct 2009
    Posts
    53
    Thank you, but why does sizeof() gives 1 while I have $some_text[0], $some_text[1], $some_text[5], ..

  5. #5
    Join Date
    Mar 2007
    Posts
    946
    Take a look at the php.net site.

    If var is not an array or an object with implemented Countable interface, 1 will be returned. There is one exception, if var is NULL, 0 will be returned.
    http://www.php.net/manual/en/function.count.php
    "Hippies.They're everywhere. They wanna save the earth, but all they do is smoke pot and smell bad."-Cartman

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles