Does php string have subscripts?

青灯夜游
Release: 2023-03-15 19:32:01
Original
3143 people have browsed it

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:

字符串名[下标值]
Copy after login

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

<?php
header(&#39;content-type:text/html;charset=utf-8&#39;);   
$str = "hello";
echo $str."<br>";
echo $str[0]."<br>";
echo $str[1]."<br>";
echo $str[2]."<br>";
?>
Copy after login

Does php string have subscripts?

Example 2: Modify the characters at the specified position

<?php
header(&#39;content-type:text/html;charset=utf-8&#39;);   
$str = "hello";
echo "原字符串:".$str."<br>";
$str[0]="H";
echo "修改第一个字符后:".$str."<br>";
?>
Copy after login

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.

<?php
header(&#39;content-type:text/html;charset=utf-8&#39;);   
$a = "我是123";
$a[1] = "5";
echo $a;     //会出现乱码
?>
Copy after login

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!

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!