Home  >  Article  >  Backend Development  >  How to convert string in php ascii

How to convert string in php ascii

藏色散人
藏色散人Original
2020-09-12 09:29:142239browse

You can use the chr function in PHP to convert the ascii code value into a string. The syntax of this function is "string chr (int ascii);" and its parameter "ascii" represents the ascii value that needs to be converted. .

How to convert string in php ascii

Recommended: "PHP Video Tutorial"

Use PHP's chr and ord functions to implement strings Convert to and from ASCII codes

chr and ord functions are used to convert strings to ASCII codes.

ASCII codes are the encoding of characters that can be displayed by the computer. The value range is 0-255, including punctuation, letters, numbers, Chinese characters, etc. During the programming process, specified characters are often converted into ASCII codes for comparison.

The following is the function provided by PHP to convert ASCII codes and characters.

1. chr() function

This function is used to convert ASCII code value into a string. Its function declaration is as follows:

string chr (int ascii);

2. ord() function

This function is used to convert a string into an ASCII code value. The function declaration is as follows:

int ord(string str);

Example:

Use the chr() function and ord() function to convert between strings and ASCII codes. The program code is as follows:

<?php 
$str1=chr(88); 
echo $str1; //返回值为X 
echo "\t"; 
$str2=ord(&#39;S&#39;); 
echo $str2; //返回值为83 
?>

Run result: X 83

The above is the detailed content of How to convert string in php ascii. 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