Changes

Working with Strings and Text in PHP

1,170 bytes added, 13:09, 4 June 2007
Converting to and from ASCII Values
=== Converting to and from ASCII Values ===
 
PHP provides the ability to work with ASCII (American Standard Code for Infomration Interchange) values. ASCII maps numerical character codes to standard, human readable characters. There are 127 ASCII characters representing the letters of the alphabext (including upper and lower case), numbers and various punctuation marks. There is also an extended character set which contains 255 characters.
 
PHP provides two functions for the purpose of converting to and from ASCII codes:
 
* '''ord()''' - Takes a character as an argument and returns the ASCII code corresponding to that character.
 
* '''chr()''' - Takes an ASCII code as an argument and retuns the character equivalent.
 
The following example converts a character to an ASCII code and then reverts it back to the character:
 
<pre>
<?php
$character = 'A';
 
$asciicode = ord($character);
 
echo "The ASCII code for $character is " . $asciicode . '<br>';
 
$char = chr($asciicode);
 
echo "The character for ASCII code for $acsciicode is " . $char . '<br>';
 
?>
</pre>
 
When executed the above code will produce the following output:
 
<pre>
The ASCII code for A is 65
The character for ASCII code for is A
</pre>