dcsimg
www.webdeveloper.com
Results 1 to 4 of 4

Thread: Weird output

  1. #1
    Join Date
    Sep 2006
    Posts
    689

    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
    21,426
    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.
    "Well done....Consciousness to sarcasm in five seconds!" ~ Terry Pratchett, Night Watch

    How to Ask Questions the Smart Way (not affiliated with this site, but well worth reading)

    My Blog
    cwrBlog: simple, no-database PHP blogging framework

  3. #3
    Join Date
    Sep 2006
    Posts
    689
    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