How to convert string in php ascii

藏色散人
Release: 2023-03-05 21:02:01
Original
2248 people have browsed it

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);
Copy after login

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);
Copy after login

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 
?>
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template