Home  >  Article  >  Backend Development  >  PHP ord()

PHP ord()

WBOY
WBOYOriginal
2024-08-29 12:56:21652browse

The ord() function in PHP is an inbuild function which is a very important function to get the ASCII value as a returned value. This is a function which considers a character string as a parameter and then as mentioned return value comes out to be the ASCII value of the first character of this string which is defined. Php ord function basically takes a single parameter from the string and is a mandatory parameter to get the ASCII value as a return value. Association of the string function with ord function also has a significance in it.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

Syntax:

ord(String)

The syntax flow is as follows the ord function is a part of String References in PHP which is used to get the first character of the string is passed as a parameter. Also, String is the most important parameter of the ord function both are somehow dependent on each other. The string is the most required parameter and is used to get an ASCII value associated with the string and especially the first character of the string.

How ord() Function Works in PHP?

PHP ord is the function that is part of the PHP versions with PHP 4, PHP 5, and PHP 7. It converts the first byte of a string to a value lying between 0 and 255.

A binary value first gets interpreted and then the first byte of the string given as an input of string works as an unsigned integer between 0 and 255.

Only if in case the string is a single byte encoded string then it only it will return a value equivalent to return the position of a character in the entire mapping of the set as value present in the set’s mapping table. But a condition applies that the function is not aware of the string encoding and particularly it will not identify a Unicode code as a point in a multi-byte encoded values.

chr is also a complimentary of ord() function it also takes string as an input that too with a single parameter containing some characters in the string by returning a one-character string again with a single byte encoding by interpreting byte value as an unsigned integer. Also, this can be used to create a one-character string in a single byte encoding to pass some code points or values which can also generate a string with multibyte encoding. Parameters belonging to the function are the same as integer values between 0 and 255. Contrary to this the return type becomes a single character string containing the specified byte. These mentioned traits show that it is totally complimentary to the ord() function in PHP.

ASCII values are the best companions for the ord() function of PHP as it helps in referring and pointing to ASCII values and ASCII table values pointing to it. It is also one of the important parameters of PHP ord() function.

mb_ord() is a subset function of the ord() function which works in a way that helps in getting the code point of character. If the condition gets satisfies then returns the value otherwise it returns a value as false as a return type. String and single encoding type string are the parameters being passed to the md_ord() function which is a subset of the ord() function.

Note: the md_ord function being stated is not fully documented and available as part of documentation for the PHP versions. It is just the parameters and the return type being defined and described but actual dependencies and follow ups is not yet present in the official documentation or website for PHP particularly for md_ord function.

Examples to Implement PHP ord() Function

Below are the examples of the PHP ord() function:

Example #1

This program illustrates the ord() function returning the value of We from the input of string as welcome in the given program with ord() function.

Code:

<?php
echo ord("we")."\n";
echo ord("welcome")."\n";
?>

Output:

PHP ord()

Example #2

This program illustrates the use of the ord function to represent the string being passed as a parameter to the ord function for its return type to return the integer value and ascii value of the string which is e is returned with a value of 101.

Code :

<?php
echo ord("educba");
?>

Output:

PHP ord()

Example #3

This program illustrates the use of the ord function to represent the string being passed as a parameter to the ord function for its return type to return the integer value and ascii value of the string which is g is returned with a value of 103

Code :

<?php
echo ord("grammer");
?>

Output:

PHP ord()

Example #4

A program to represent that the first character of the string being passed is a line feed using ord() function.

Code :

<?php
$str = "\n";
if (ord($str) == 10) {
echo "First character of \$str in the ord function is a line feed.\n";
}
?>

Output:

PHP ord()

Example #5

A program to represent the ord function if the scope of the declaration is not global instead if it is local then the output will be represented as an output.

Code :

<?php
declare(encoding ='UTF-8');
$str = " ";
for ( $pos=0; $pos < strlen($str); $pos ++ ) {
$byte = substr($str, $pos);
echo 'Byte ' . $pos . ' of $str has value ' . ord($byte) . PHP_EOL;
}
?>

Output:

PHP ord()

Example #6

A program to represent the ord function if the scope of the declaration is not declared and an empty string is passed whose output stands as illustrated below.

Code :

<?php
$str = " ";
for ( $pos=0; $pos < strlen($str); $pos ++ ) {
$byte = substr($str, $pos);
echo 'Byte ' . $pos . ' of $str has value ' . ord($byte) . PHP_EOL;
}
?>

Output:

PHP ord()

Conclusion

PHP ord() function only supports for PHP 4, PHP5 and PHP 7 versions. It is a pre-defined function that blends with the string references of the PHP language very nicely and helps in providing end-users a better functionality and feature for string and Unicode-defined strings.

Recommended Article

This is a guide to the PHP ord(). Here we discuss the Introduction to PHP ord() Function and how it works along with examples as well as Code Implementation. You can also go through our other suggested articles to learn more-

  1. Overview of Abstract Class in Python
  2. What is Abstract Class in PHP?
  3. Socket Programming in PHP with Methods
  4. Introduction to PHP Frameworks

The above is the detailed content of PHP ord(). For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:PHP number_format()Next article:PHP number_format()