Jump to content
ComputerColumbus

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

 

 Any advice would be appreciated...

 

 Regards,

 

 ComputerColumbus :pirate:

Edited by ComputerColumbus

Share this post


Link to post
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 this post


Link to post
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);

 

 Any advice would be appreciated...

 

 Regards,

 

 ComputerColumbus :pirate:

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

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
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

Edited by Barand

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
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 this post


Link to post
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];
Edited by Ch0cu3r

Share this post


Link to post
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'";

Edited by ComputerColumbus

Share this post


Link to post
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}";
Edited by Psycho

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites

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

Share this post


Link to post
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 this post


Link to post
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! :pirate:

Edited by ComputerColumbus

Share this post


Link to post
Share on other sites

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.