How to Convert a String to a Decimal in PHP
The PHP programming language is typically embedded into an HTML page to provide the user with interactive elements. PHP supports multiple character encoding schemes, allowing it to be used internationally in multilingual websites. A character-encoding scheme maps a number with a character. You can convert a string into the decimal number representation of its component characters using PHP. This is useful if you are converting from one character-encoding scheme to another.
Instructions
-
-
1
Decide how you will run your PHP code. If you have a PHP server, you can execute code using PHP files. Otherwise, use an online PHP interpreter. Enter the code in this tutorial into either a PHP file or the interpreter.
-
2
Begin your PHP program with the following statement:
<?php
-
-
3
Declare a character string by writing the following line of code in a new line below the previous statement:
$character = 'a';
-
4
Convert the character string into its ASCII decimal value using the ord function. This function returns a new value that must be stored in a new variable. To store the returned value, you can create a new variable named $newCharacter and call the function like this:
$newCharacter = ord($character);
-
5
Print the ASCII decimal value using the print function, like this:
print($newCharacter)
-
6
Conclude your PHP program with the following statement. Your program is now ready to be tested on your server or interpreter.
?>
-
7
Run the program. The output is the ASCII decimal value for the letter a, and it looks like this:
226
-
1