PHP ord() Function
The ord() function in PHP converts the first byte of a string to a value between 0 and 255. This function interprets the binary value of the first byte of string as an unsigned integer.
Syntax
1 2 3 |
ord ( string $string ) |
Parameter
string(required): This parameter represents a character.
Return
This function returns an integer between 0 and 255.
Example 1
1 2 3 4 5 6 7 |
<?php // converting the first byte of string to a value between 0 and 255 echo "The unsigned integer value for 'h': ".ord("h")."\n"; // will convert h echo "The unsigned integer value for 'hello': ".ord("hello")."\n";// will convert only h ?> |
Output
1 2 3 4 |
The unsigned integer value for 'h': 104 The unsigned integer value for 'hello': 104 |
Example 2
1 2 3 4 5 6 7 |
<?php // converting the first byte of string to a value between 0 and 255 echo "The unsigned integer value for 'a': ".ord("a")."\n"; // will convert a echo "The unsigned integer value for 'A': ".ord("A")."\n";// will convert A ?> |
Output
1 2 3 4 |
The unsigned integer value for 'a': 97 The unsigned integer value for 'A': 65 |
Example 3
1 2 3 4 5 6 7 8 9 |
<?php echo "The converted unsigned integer VALUE:"."\n"; for($i=0;$i<=6;$i++){ // returning an integer between 0 and 255. echo ord("$i")."\n"; } ?> |
Output
1 2 3 4 5 6 7 8 9 10 |
The converted unsigned integer VALUE 48 49 50 51 52 53 54 |