Usage of chr (ascii) and ord (string) functions in php

Source: Internet
Author: User
The functions of these two functions are the opposite. The chr function returns characters from the specified ASCII value, while the ord () function returns the ASCII value of the first character of the string. You can use this letter to understand this.

The functions of these two functions are the opposite. The chr function returns characters from the specified ASCII value, while the ord () function returns the ASCII value of the first character of the string. You can use this function to understand this.

First look at the chr function

The chr () function returns characters from the specified ASCII value.

Chr (ascii)

Ascii parameters can be decimal, octal, or hexadecimal. You can specify the octal ratio by using the frontend 0 and the hexadecimal value by using the frontend 0x.

Example,The code is as follows:

  1. Echo chr (52 );
  2. Echo chr (052 );
  3. Echo chr (0x52 );
  4. ?>
  5. // Output:
  6. 4
  7. *
  8. R

Isn't it amazing? in fact, it's not surprising that I often use chr to operate some codes that do not see the encoding. the code is as follows:

  1. Echo chr (13 );
  2. Echo chr (32 );
  3. ?>

Let's think about what this will output. The result is a carriage return and a space. let's look at the ord function. the ord () function returns the ASCII value of the first character of the string, from the above, it is exactly the opposite of chr.

Syntax:Ord (string)

Example

  1. Echo ord ("h ");
  2. Echo ord ("hello ");
  3. ?>

Output result:104 104

Now let's look at a comprehensive example. the code is as follows:

  1. $ Str1 = chr (88 );
  2. Echo $ str1; // The return value is X.
  3. $ Str2 = chr (ord (X) + 1 );//
  4. Echo $ str2; // The return value is Y.
  5. Echo "t ";
  6. $ Str3 = ord ('s ');
  7. Echo $ str3; // return value: 83
  8. ?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.