Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


Increment/Decrement Letters

Posted on 2014-04-15
Medium Priority
Last Modified: 2014-04-30
How do you have a variable "E" and change it to either "D" or "F"?
Question by:breeze351
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 35

Expert Comment

by:Dan Craciun
ID: 40002237
LVL 111

Expert Comment

by:Ray Paseur
ID: 40002862
If you mean "a variable with a value of character 'E'" then the ord() and chr() are your friends.  If you mean a variable with the name "E" you have a different situation.  PHP variable names are always started with the dollar sign.  A script that refers to an unquoted E in the code refers to a constant (or potentially to an undefined constant).  If you want to post an example showing how you want to use this code in a script we could disambiguate and show you a test case.  But I expect that Dan's answer is right for what you want. :-)
LVL 34

Expert Comment

ID: 40004177
greetings breeze351, , I do not see enough information in your question to give you a suggestion for your problem. As has already been said here, you might could use the PHP ord( ) to get a SINGLE text character as it's "Binary" storage numeric value. But ord( ) can only operate on Just One text character of a "string", AND the text characters have a limited range of numeric values, usually 32 to 126, , , as a value of 31 OR 127, will NOT be a valid text character, for beginners this can be difficult to understand (the "number" value of a character). If you could tell us why you ask this question, or the code problem you need to solve, then our answers might be better for you.
Ask questions if you need more information.
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Author Comment

ID: 40007442
What's wrong with this code?  From everything I've read, it should work.

$West_Button_West = chr(ord($West_Button_West) + 1);

It should return the next letter.

LVL 111

Expert Comment

by:Ray Paseur
ID: 40007508
There may or may not be anything wrong about the code.  But in order to know we need to see the value of $West_Button_West, right?  You can print that out with


please do that and post the result here, thanks.
LVL 34

Expert Comment

ID: 40008066
you ask about this code line-
$West_Button_West = chr(ord($West_Button_West) + 1);

and as the code is written, it can work, but ONLY if the string in the $West_Button_West variable is a Single text character, AND likely for your purposes (using only a through z), if it is NOT a 'z' or a 'Z' .

You might consider a function to TEST the input string and see if it is going to be a useable string length.
My version of possible code -

function nextLetter($letter) {
// Get ONLY the first text character if strlen is more than 1
if (strlen($letter) > 1) {
  $letter = $letter[0];
// rotate to first letter if end letter z
if ($letter == 'z') return 'a';
if ($letter == 'Z') return 'A';
$letter = ord($letter)+1;
// rotate to first character if end $letter number too big
if ($letter > 126) return ' ';// space character Number 32
return chr($letter);

$str1 = 'G';
$str1 = nextLetter($str1);

$str2 = 'bl';
$str2 = nextLetter($str2);

$str3 = 'z';
$str3 = nextLetter($str3);

echo 'nextLetter Results are '.$str1.', '.$str2.', '.$str3;

Open in new window

This works on my server and echos out -
     nextLetter Results are H, c, a
LVL 111

Accepted Solution

Ray Paseur earned 2000 total points
ID: 40008549
Glenn: I think the underlying issue in your question goes to whether a character is exactly one byte.  That is the way PHP was written (back in the 1990's) but it's not the way the world works today.  There are many more alphabetic characters than can be represented by 8-bit bytes and the common, but obsolete encoding assumption of 1 byte == a character is no longer valid.  In particular, Western European characters above code point 127 are different numeric values if you choose UTF-8 versus ISO-8859-1.  That's why we need to see the output of this function:


Simply adding the number 1 to an integer may or may not get you the code point of the "next" letter.  It may produce an invalid UTF-8 encoding.  And if the data ever needs to be transported (for example by JSON) valid UTF-8 is required.  So it's worth a little extra effort to understand modern character encoding and build your application correctly.

It's not exactly easy to get around this issue in PHP, and it's impossible if you do not understand character encoding.  You may want to read these man pages carefully.
LVL 111

Expert Comment

by:Ray Paseur
ID: 40032838
Thanks for the points and thanks for using EE, ~Ray

Featured Post

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.
Suggested Courses

604 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question