Jump to content

[SOLVED] soo weird.. substr


kellz

Recommended Posts

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!

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.