ComputerColumbus Posted October 19, 2015 Share Posted October 19, 2015 (edited) Hi there, I'm learning how to code on CodeAcademy. In one of the lessons it says that substr() treats character in a string as a zero-indexed array, and that the first letter of my name "Alison" is at position zero and the last letter at position -1. Why is my last letter name at position -1? Shouldn't it be at position 6? I'm trying to print a random letter from my name with this code. So far I wasn't able to. $myname = "Alison"; $partial = substr($myname, 0, 3); print rand(a, n); Any advice would be appreciated... Regards, ComputerColumbus Edited October 19, 2015 by ComputerColumbus Quote Link to comment https://forums.phpfreaks.com/topic/298694-substr-question/ Share on other sites More sharing options...
Barand Posted October 19, 2015 Share Posted October 19, 2015 As it said - zero-indexed, therefore 6 characters index 0 - 5 | 0 | 1 | 2 | 3 | 4 | 5 | +---+---+---+---+---+---+ | A | l | i | s | o | n | Quote Link to comment https://forums.phpfreaks.com/topic/298694-substr-question/#findComment-1523685 Share on other sites More sharing options...
ComputerColumbus Posted October 19, 2015 Author Share Posted October 19, 2015 Hi there, I'm learning how to code on CodeAcademy. In one of the lessons it says that substr() treats character in a string as a zero-indexed array, and that the first letter of my name "Alison" is at position zero and the last letter at position -1. Why is my last letter name at position -1? Shouldn't it be at position 6? I'm trying to print a random letter from my name with this code. So far I wasn't able to. $myname = "Alison"; $partial = substr($myname, 0, 3); print rand(a, n); Any advice would be appreciated... Regards, ComputerColumbus CodeAcademy says that the letter "n" should be at position -1 Quote Link to comment https://forums.phpfreaks.com/topic/298694-substr-question/#findComment-1523688 Share on other sites More sharing options...
Jacques1 Posted October 19, 2015 Share Posted October 19, 2015 Negative indices are simply a shorthand notation which is useful when you need to access characters from right to left. Yes, the last of 6 characters is at position 5, but it can also be found at index −1. The next-to-last character is at position 4 and −2 etc. Quote Link to comment https://forums.phpfreaks.com/topic/298694-substr-question/#findComment-1523689 Share on other sites More sharing options...
ComputerColumbus Posted October 19, 2015 Author Share Posted October 19, 2015 Sorry, "Alison" has only 5 letters. I meant to ask, shouldn't letter "n" be at position 5? But why does it start from right to left at position -1? Why doesn't it start at position 0? Quote Link to comment https://forums.phpfreaks.com/topic/298694-substr-question/#findComment-1523690 Share on other sites More sharing options...
Barand Posted October 19, 2015 Share Posted October 19, 2015 (edited) "n" is at position 5. (see reply #2 ) $name = 'Alison'; echo $name[5]; //--> n "A" is at position 0 echo $name[0]; //--> A When using substr you give the start index and the number of characters echo substr($name,2,3); //-> iso It also gives you the the option of counting from the end of the string, using negative offset values (as jaques1 stated) echo substr($name,-3,3); //--> son echo substr($name,-1,1); //--> n substr Edited October 19, 2015 by Barand Quote Link to comment https://forums.phpfreaks.com/topic/298694-substr-question/#findComment-1523691 Share on other sites More sharing options...
Psycho Posted October 19, 2015 Share Posted October 19, 2015 Read the manual. The substr() function only takes one parameter for the start position. So, if it was to go from 0 to n from the right side it would then need another parameter to know if the index is from left to right or right to left. Instead, positive numbers indicate left to right and negative numbers mean right to left. Look at all the examples in the manual and it should start to make sense for you. Quote Link to comment https://forums.phpfreaks.com/topic/298694-substr-question/#findComment-1523692 Share on other sites More sharing options...
ComputerColumbus Posted October 19, 2015 Author Share Posted October 19, 2015 Thanks Psycho, that helped! Quote Link to comment https://forums.phpfreaks.com/topic/298694-substr-question/#findComment-1523693 Share on other sites More sharing options...
ComputerColumbus Posted October 19, 2015 Author Share Posted October 19, 2015 Thanks as well, Barand! I'm having trouble printing a random (or letter character) from my name with this source code: $myname = "Alison"; $partial = substr($myname, 0, 5); print rand(0, 5); Quote Link to comment https://forums.phpfreaks.com/topic/298694-substr-question/#findComment-1523696 Share on other sites More sharing options...
Ch0cu3r Posted October 19, 2015 Share Posted October 19, 2015 (edited) To print a random number from $myName you can do $myName = "Alison"; $randomPosition = rand(0, 5); echo "Random letter from $myName: " . substr($myName, $randomPosition, 1); // or as simply as echo "Random letter from $myName: " . $myname[$randomPosition]; Edited October 19, 2015 by Ch0cu3r Quote Link to comment https://forums.phpfreaks.com/topic/298694-substr-question/#findComment-1523697 Share on other sites More sharing options...
ComputerColumbus Posted October 19, 2015 Author Share Posted October 19, 2015 (edited) This is what I have now but it still doesn't work...It prints Random letter from Alison: 'Alison'. $myName = "Alison"; $randomPosition = rand(0, 5); echo "Random letter from $myName: 'Alison'"; Edited October 19, 2015 by ComputerColumbus Quote Link to comment https://forums.phpfreaks.com/topic/298694-substr-question/#findComment-1523699 Share on other sites More sharing options...
Psycho Posted October 19, 2015 Share Posted October 19, 2015 (edited) $myName = 'Alison'; $maxLetters = strlen($myName); $randIndex = rand(0, $maxLetters-1); $randLetter = substr($myName, $randIndex, 1); echo "Random letter from \$myName '{$myName}': Index {$randIndex}, Letter {$randLetter}"; Edited October 19, 2015 by Psycho Quote Link to comment https://forums.phpfreaks.com/topic/298694-substr-question/#findComment-1523700 Share on other sites More sharing options...
ComputerColumbus Posted October 19, 2015 Author Share Posted October 19, 2015 I need a simpler code. I am a beginner and I'm trying to print a random letter of my name for the first time. This should work, right? $myName = "Alison"; $randomPosition = rand(0, 5); substr($myname, $randomPosition, 1) echo $randomPosition; Quote Link to comment https://forums.phpfreaks.com/topic/298694-substr-question/#findComment-1523707 Share on other sites More sharing options...
Solution Ch0cu3r Posted October 19, 2015 Solution Share Posted October 19, 2015 Yes with the highlighted corrections in red $myName = "Alison";$randomPosition = rand(0, 5);echo substr($myname, $randomPosition, 1);echo "<br /> its position is: " . $randomPosition; That'll be pretty much the same code I posted earlier and what Psycho had also posted Quote Link to comment https://forums.phpfreaks.com/topic/298694-substr-question/#findComment-1523710 Share on other sites More sharing options...
ComputerColumbus Posted October 19, 2015 Author Share Posted October 19, 2015 (edited) This is what it last gave me. Edited October 19, 2015 by ComputerColumbus Quote Link to comment https://forums.phpfreaks.com/topic/298694-substr-question/#findComment-1523711 Share on other sites More sharing options...
Ch0cu3r Posted October 19, 2015 Share Posted October 19, 2015 $myname needs to be $myName Quote Link to comment https://forums.phpfreaks.com/topic/298694-substr-question/#findComment-1523713 Share on other sites More sharing options...
ComputerColumbus Posted October 19, 2015 Author Share Posted October 19, 2015 Okay, so it finally printed a letter. Yay! But it also prints a random number...The lesson requires me to print a letter only. Quote Link to comment https://forums.phpfreaks.com/topic/298694-substr-question/#findComment-1523714 Share on other sites More sharing options...
Ch0cu3r Posted October 19, 2015 Share Posted October 19, 2015 Look at the four lines of code. What line do you think is displaying the number? Quote Link to comment https://forums.phpfreaks.com/topic/298694-substr-question/#findComment-1523715 Share on other sites More sharing options...
Barand Posted October 19, 2015 Share Posted October 19, 2015 Then remove that bit of the code. Quote Link to comment https://forums.phpfreaks.com/topic/298694-substr-question/#findComment-1523716 Share on other sites More sharing options...
ComputerColumbus Posted October 19, 2015 Author Share Posted October 19, 2015 I'm really not sure what to change. Maybe echo $randomPosition? But that expression should echo one letter between paramaters 0 and 5... Quote Link to comment https://forums.phpfreaks.com/topic/298694-substr-question/#findComment-1523719 Share on other sites More sharing options...
scootstah Posted October 19, 2015 Share Posted October 19, 2015 ComputerColumbus, you seem to have a very poor understanding of even the most very basic programming concepts. You need to back up in your learning and work on understanding things like variables and function arguments. Make sure that before you move on to a new topic, you fully understand the one you just finished. If you do not understand how variables work you will never be able to create your own programs. Quote Link to comment https://forums.phpfreaks.com/topic/298694-substr-question/#findComment-1523724 Share on other sites More sharing options...
ComputerColumbus Posted October 19, 2015 Author Share Posted October 19, 2015 Okay people, I got it. I commented out the //echo ($randomPosition) Thank you so much for your help! ComputerColumbus Quote Link to comment https://forums.phpfreaks.com/topic/298694-substr-question/#findComment-1523725 Share on other sites More sharing options...
ComputerColumbus Posted October 19, 2015 Author Share Posted October 19, 2015 (edited) I'm trying my best, especially since I'm learning on CodeAcademy! Unfortunately, their idea of learning is to push you into new topics. This excercise was slightly ahead of my knowledge. I'm also reading Paul Hudson's PHP ebook. It's steadier and goes more into detail. However, it is amazing how much I've been learning on this forum! Edited October 19, 2015 by ComputerColumbus Quote Link to comment https://forums.phpfreaks.com/topic/298694-substr-question/#findComment-1523726 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.