www.webdeveloper.com
Results 1 to 4 of 4

Thread: Weird output

  1. #1
    Join Date
    Sep 2006
    Posts
    655

    Question Weird output

    Hi All

    Can someone pls explain me why does the following code prints "t"?

    Code:
    $a = "print";
    
    echo $a[4];
    
    //outputs t

    Thanks

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,408
    Because you can treat a string sort of like an array of characters, and since it's a zeroth based array, index 4 is the 5th character.
    "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
    Sep 2006
    Posts
    655
    Thanks.

  4. #4
    Join Date
    Sep 2013
    Posts
    221
    This is because you are using array. array[4] means it starts from '0' and then continue till '4' which total is '5' numbers. So the in word "print" it will count form '0' and will print the 5th character as in the array you have mentioned [4].
    Hope your problem is solved.
    Thnxs.
    strad solutionswww.stradsolutions.com

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