Jump to content

Quickie about substr()


TechnoDiver

Recommended Posts

$num = 27;

$first_digit = substr($num, 0);
$last_digit = substr($num, -1);

echo $first_digit; //this returns 27
echo $last_digit; //this returns 7

In the above example why is the first echo statement returning the full number whereas the second returns the last digit?

(I know why the last digit is returned in the second the real question is why wouldn't the first return 2?)

Edited by TechnoDiver
Link to comment
Share on other sites

Right, Right. I have that linked manual page open too and read about that, it didn't click what it meant. Thank you

 

Out of curiosity, why is specifying a length required when an index was specifically used? Is there a use case where this is preferred or necessary?

I'm only a very basic level with programming but I originally learned in Python and I've never seen this be necessary there

Edited by TechnoDiver
Link to comment
Share on other sites

2 hours ago, TechnoDiver said:

why is specifying a length required when an index was specifically used?

substr returns a string that consists of $length characters starting at $offset.  If you don't specify what length, then it defaults to the end of the input string.

If you want a single character at a specific index then you must specify a $length of 1, or use array syntax on the string to access the desired offset.

$str = 'Hello';

$firstLetter = substr($str, 0, 1);
//or
$firstLetter = $str[0];

 

Link to comment
Share on other sites

6 hours ago, TechnoDiver said:

why is specifying a length required when an index was specifically used

Just to avoid any confusion as it's not clear if you're referring to your $first_digit or $last_digit assignment, your second line will assume the third parameter is the length of the string - as kicken pointed out:

3 hours ago, kicken said:

If you don't specify what length, then it defaults to the end of the input string.

Or, as stated in the manual:

Quote

If offset is negative, the returned string will start at the offset'th character from the end of string.

So, if your $last_number assignment was this:

$last

$last_number = substr($num, -2);

it would also return 27.

Edited by maxxd
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.