Getting the next char in a sequence

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mabersold
Forum Newbie
Posts: 1
Joined: Thu Jan 04, 2007 10:35 am

Getting the next char in a sequence

Post by mabersold »

Greetings all. I'm looking for a way of determining the next character in sequence, given a single character string ranging from a-z.

The way I'm currently doing it:

Given a single-character string $key, I get the next character in the sequence by doing this: $key2 = chr(ord($key) + 1). It works fine for my purposes thus far. I'm just wondering if there's a better/more efficient way of handling this.

For what it's worth, this line will probably only run once every time the script is called.
User avatar
TheMoose
Forum Contributor
Posts: 351
Joined: Tue May 23, 2006 10:42 am

Post by TheMoose »

If you store $key somewhere else, or you've already used $key, you can use $key2 = ++$key

Careful though, using ++$key will overwrite $key as well (they'll be the same character).
Post Reply