kellz Posted November 17, 2007 Share Posted November 17, 2007 hey all I don't know why this prints "ab": $a="abc"; $b=substr($a, 0, -1); echo $b; I found it on a php quiz somewhere (and got the answer wrong) how can it start at 0 and end at -1 ??? thats like.. freakish! Quote Link to comment https://forums.phpfreaks.com/topic/77689-solved-soo-weird-substr/ Share on other sites More sharing options...
pocobueno1388 Posted November 17, 2007 Share Posted November 17, 2007 The negative is telling it to go backwards on the string. So it's going one space back, which covers the 'c', therefor only printing the 'ab'. If you put a -2 instead of -1, it would only show 'a' because it is going backwards two spots covering the last two letters. Hopefully that makes a little bit of sense =] Just play around with it and you will figure out how it works. Quote Link to comment https://forums.phpfreaks.com/topic/77689-solved-soo-weird-substr/#findComment-393275 Share on other sites More sharing options...
kellz Posted November 17, 2007 Author Share Posted November 17, 2007 omg I didn't know that! that's useful! thx Quote Link to comment https://forums.phpfreaks.com/topic/77689-solved-soo-weird-substr/#findComment-393277 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.