TechnoDiver Posted June 23, 2021 Share Posted June 23, 2021 (edited) $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 June 23, 2021 by TechnoDiver Quote Link to comment https://forums.phpfreaks.com/topic/312968-quickie-about-substr/ Share on other sites More sharing options...
mac_gyver Posted June 23, 2021 Share Posted June 23, 2021 because you haven't specified a length of 1 (one) - https://www.php.net/manual/en/function.substr.php 1 Quote Link to comment https://forums.phpfreaks.com/topic/312968-quickie-about-substr/#findComment-1587506 Share on other sites More sharing options...
TechnoDiver Posted June 23, 2021 Author Share Posted June 23, 2021 (edited) 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 June 23, 2021 by TechnoDiver Quote Link to comment https://forums.phpfreaks.com/topic/312968-quickie-about-substr/#findComment-1587507 Share on other sites More sharing options...
kicken Posted June 23, 2021 Share Posted June 23, 2021 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]; Quote Link to comment https://forums.phpfreaks.com/topic/312968-quickie-about-substr/#findComment-1587510 Share on other sites More sharing options...
maxxd Posted June 24, 2021 Share Posted June 24, 2021 (edited) 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 June 24, 2021 by maxxd Quote Link to comment https://forums.phpfreaks.com/topic/312968-quickie-about-substr/#findComment-1587512 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.