Jump to content

substr () Question


ComputerColumbus
Go to solution Solved by Ch0cu3r,

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

Edited by Barand
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];
Edited by Ch0cu3r
Link to comment
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
Link to comment
Share on other sites

  • Solution

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:

Edited by ComputerColumbus
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.