Jump to content

letter incrementer?


onedumbcoder

Recommended Posts

don't know if there is a built in function,

 

but if I had to make one:

 

A through Z

0 through 25

 

<?php

function incrementLetter($letter)
{
     $letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
     $postion = strpos ($letter, $letters);

     $position++;
     return substr ($letters,$position,1);
}

?>

 

UPDATE: if end is reached, jump to A (0)

 

<?php

function incrementLetter($letter)
{
     $letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
     $postion = strpos ($letter, $letters);

     $position++;
     
     if ($position == 26)
     {
     	// if you've reached the last letter "Z", and want to go one further, it'll jump back to "A"
	$postion = 0;
 }
     
     return substr ($letters,$position,1);
}

?>

Link to comment
https://forums.phpfreaks.com/topic/106678-letter-incrementer/#findComment-546854
Share on other sites

You can actually use the ++ increment operator -

 

$letter = "A";
echo $letter;
$letter++;
echo $letter;

 

Man PHP is awesome :P

 

BTW, figured out mine doesn't work.

strpos returns a BOOLEAN response, not the actual position :(

 

How do you get the position a letter is at in a string then? :s

Link to comment
https://forums.phpfreaks.com/topic/106678-letter-incrementer/#findComment-546861
Share on other sites

Mine failed because of a typo lol... very professional  ::)

 

<?php

function incrementLetter($letter)
{
     $string = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
     $position = strpos ($string, $letter);

     $position++;
     
     if ($position == 26)
     {
     	// if you've reached the last letter "Z", and want to go one further, it'll jump back to "A"
	$postion = 0;
 }
     
     return substr ($string,$position,1);
}

$originalLetter = "A";
echo "The original letter is " . $originalLetter;
echo "<br />";
echo "<br />";

$temp = $originalLetter;

for ($i=0; $i<50; $i++)
{
$temp = incrementLetter($temp);
echo $temp . " ";
}

?>

 

But if it works with just using the increment operator, what's the use haha

Link to comment
https://forums.phpfreaks.com/topic/106678-letter-incrementer/#findComment-546867
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.