Hudzilla.org - the homepage of Paul Hudson
Contents > Functions > Playing with strings Wish List | Report Bug | About Me ]

4.7.3     Converting to and from ASCII: chr() and ord()

This is NOT the latest copy of this book; click here for the latest version.

string chr ( int ascii)

int ord ( string string)

The American Standard Code for Information Interchange is a special set of 255 numbers that evaluate to letters, symbols, and actions used in most computers. For example, 74 is "J", 106 is "j", 123 is {, and 32 is a space. To convert to ASCII from textual characters, you should use the chr() function, which takes an ASCII value as its only parameter and returns the text equivalent if there is one. The ord() function does the opposite - it takes a string and returns the equivalent ASCII value.

For example:

<?php
    $mystr
= "ASCII is an easy way for computers to work with strings\n";

    if (
ord($mystr{1}) == 83) {
        print
"The second letter in the string is S\n";
    } else {
        print
"The second letter is not S\n";
    }

    
$letter = chr(109);

    print
"ASCII number 109 is equivalent to $letter\n";
?>

That should output the following:

The second letter in the string is S
ASCII number 109 is equivalent to m




<< 4.7.2 Replacing parts of a string: str_replace() and str_ireplace()   4.7.4 Measuring strings: strlen(), count_chars(), and str_word_count() >>
Table of Contents
Want to see this stuff in print? PHP in a Nutshell takes the core topics covered here, adds in thousands of edits from the editorial team and myself, and combines them to make an unbeatable reference for PHP programmers at all levels.



My latest book has hundreds more tips on how to use PHP, Apache, and MySQL, plus Perl, Python, shell scripts, performance tuning, and more!



Top-right shadow
 
Bottom-left shadow Bottom shadow

Comments from other readers
yoy.. - 06 Sep 2008

So thank you!

Yet another yank. - 06 Sep 2008

naah ordinal. gotta be.

Got I hate HLL.
In assembly language, the chore of doing things yourself is SO much easier than the drudgery of finding the sintax of something inane. (over HOURS!) :)

A |\| 3 \/\/ 13 | E PHP User - 06 Sep 2008

"Might I ask, where does "ord" come from? It doesn't trigger any thoughts of ASCII in my mind. Is it from "ordinal" as in enumeration"

I Think It Means Ordinary :\

A PHP User - 06 Sep 2008

Might I ask, where does "ord" come from? It doesn't trigger any thoughts of ASCII in my mind. Is it from "ordinal" as in enumeration



Add comment
Please note that by posting a comment here you are committing it to the public domain. This is important so that others can make use of your code themselves, and also so that I can incorporate helpful notes directly into the main text. Comments are limited to 2000 characters in length.

If you are reporting an error in the content, please tell me directly.

Your name/email address:
Your comment:
 
Now, in order to verify that you're a real person, please answer this simple question: what is two plus nine?
The answer is:
(please write in
numbers, eg 19)


Top-right shadow
 
Bottom-left shadow Bottom shadow