Jump to content

substr () Question


ComputerColumbus

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:

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

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

Link to comment
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.

Link to comment
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];
Link to comment
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

Link to comment
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.

Link to comment
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:

Link to comment
Share on other sites

Archived

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

×
×
  • 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.