Home>Article>Backend Development> Does php string have subscripts?

Does php string have subscripts?

青灯夜游
青灯夜游 Original
2022-04-24 11:49:06 3060browse

php strings have subscripts. In PHP, subscripts can be applied not only to arrays and objects, but also to strings. You can use the subscripts and square brackets "[]" of the string to access the character at the specified index position and read and write the character. Syntax "string name [subscript value]"; the subscript value (index value) of the string can only be an integer type, and the starting value is 0.

Does php string have subscripts?

The operating environment of this tutorial: Windows 7 system, PHP version 7.1, DELL G3 computer

php strings have subscripts.

In PHP, subscripts can be applied not only to arrays and objects, but also to strings.

In PHP, you can use the subscript of the string and the square brackets "[]" to access the character at the specified index position and read and write the character. The left side of the brackets is the string name, and the brackets are the string subscript:

字符串名[下标值]

The subscript (index value) can only be an integer type, and the starting value is 0 (string subscript Counting starts from 0).

Example 1: Output the characters at the specified position

"; echo $str[0]."
"; echo $str[1]."
"; echo $str[2]."
"; ?>

Does php string have subscripts?

Example 2: Modify the characters at the specified position

"; $str[0]="H"; echo "修改第一个字符后:".$str."
"; ?>

Does php string have subscripts?

Note: This method is suitable for English characters. Chinese characters occupy multiple character lengths and cannot be accurately positioned using subscripts.

Does php string have subscripts?

Because Hanzi occupies 3 characters in length under UTF-8 encoding, when one character is replaced, the subsequent display will be garbled.

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of Does php string have subscripts?. 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