/    Sign up×
Community /Pin to ProfileBookmark

Hi All

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

[code]
$a = “print”;

echo $a[4];

//outputs t
[/code]

Thanks

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@NogDogSep 20.2013 — 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.
Copy linkTweet thisAlerts:
@phantom007authorSep 20.2013 — Thanks.
Copy linkTweet thisAlerts:
@priyankagoundSep 26.2013 — 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.
×

Success!

Help @phantom007 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...