# substr () Question

## Recommended Posts

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);

Regards,

ComputerColumbus

##### Share on other sites

As it said - zero-indexed, therefore 6 characters index 0 - 5

``` | 0 | 1 | 2 | 3 | 4 | 5 |
+---+---+---+---+---+---+
| A | l | i | s | o | n |```
##### Share on other sites

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);

Regards,

ComputerColumbus

CodeAcademy says that the letter "n" should be at position -1

##### Share on other sites

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.

##### Share on other sites

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?

##### Share on other sites

"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

##### Share on other sites

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.

##### Share on other sites

Thanks Psycho, that helped!

##### Share on other sites

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);

##### Share on other sites

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];
```
##### Share on other sites

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'";

##### Share on other sites

```
\$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}";```
##### Share on other sites

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;

##### Share on other sites

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

##### Share on other sites

This is what it last gave me.

##### Share on other sites

\$myname needs to be \$myName

##### Share on other sites

Okay, so it finally printed a letter. Yay!

But it also prints a random number...The lesson requires me to print a letter only.

##### Share on other sites

Look at the four lines of code. What line do you think is displaying the number?

##### Share on other sites

Then remove that bit of the code.

##### Share on other sites

I'm really not sure what to change. Maybe echo \$randomPosition? But that expression should echo one letter between paramaters 0 and 5...

##### Share on other sites

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.

##### Share on other sites

Okay people, I got it.

I commented out the //echo (\$randomPosition)

Thank you so much for your help!

ComputerColumbus

##### Share on other sites

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!

#### Archived

This topic is now archived and is closed to further replies.

×

• #### Activity

• Chat
×
• Create New...