#45675 [Bgs]: The documentation for ord() is inaccurate

From: Date: Mon, 04 Aug 2008 12:54:42 +0000
Subject: #45675 [Bgs]: The documentation for ord() is inaccurate
References: 1  Groups: php.doc.bugs 
Request: Send a blank email to doc-bugs+get-1055@lists.php.net to get a copy of this message
ID: 45675 Updated by: rquadling@php.net Reported By: mtesta at money-media dot com Status: Bogus Bug Type: Documentation problem Operating System: Windows XP PHP Version: Irrelevant New Comment: The only thing that is potentially missing is that you will get a different response depending upon the encoding of the source file. If you use UTF-8, the first byte (which in PHP5 and lower is synonymous with character) has a value of 197. If you are using Windows-1252, then the character has a value of 154. Both are correct as they are in effect different strings. If you are needing UTF-8 string processing, then the functions mentioned in the user notes as well as other multi-byte string libraries are available. Previous Comments: ------------------------------------------------------------------------ [2008-08-04 12:47:05] mtesta at money-media dot com That's exactly my point. The PHP documentation (http://us.php.net/manual/en/function.ord.php) says: "Returns the ASCII value of the first character of string". This is not true, as we have both shown, and should be updated appropriately. ------------------------------------------------------------------------ [2008-08-04 12:42:08] rquadling@php.net At the windows command line ... php -r "echo ord('Å¡');" I get 154. If the file is encoded Windows 1252 I get 154. Latin small letter s with caron. If the file is encoded UTF-8, I get 197 as ord() is working on binary strings and Å¡ has a binary value of 0xC5 0xA1 an 0xC5 = 197. (This translates to U+0161 according to Charmap). In PHP 5, ord() currently works on binary strings. So, depending upon the encoding used, the binary string may start whatever is appropriate. There is a user note on ord() with regards to getting a value of a unicode "character". This returns a value of 353 which, as if by magic, has a value of 0x0161. ------------------------------------------------------------------------ [2008-08-01 14:39:41] mtesta at money-media dot com Description: ------------ The documentation for ord() is inaccurate. From my tests ord() returns the ANSI (Windows-1252) value of character. Bellow is a simple test to confirm this. "š" is not in the ASCII character set but the value of "š" in the ANSI character set is 154. Reproduce code: --------------- ord('š'); Actual result: -------------- 154 ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/?id=45675&edit=1

« previous php.doc.bugs (#1055) next »