Home > Article > Backend Development > Can’t Chinese characters be placed in the php array?
Chinese characters can be placed in the php array. Because PHP is a programming language with weak data types, arrays in PHP can store any number of data of any type, that is, there is no limit on the type of array elements, which can be numbers, Boolean values, arrays, strings, etc.; while strings It can be divided into pure English strings, pure Chinese strings and mixed Chinese and English strings.
The operating environment of this tutorial: windows7 system, PHP version 8.1, DELL G3 computer
Array is one of the most important data types in PHP First, it is widely used in PHP. Because PHP is a programming language with weak data types, array variables in PHP can store any number of data of any type, and can implement the functions of data structures such as heaps, stacks, and queues in other strong data types.
Simply put, There is no restriction on the type of PHP array elements, which can be numbers, strings, Boolean values, arrays, Object objects, etc.
A string is a continuous sequence of characters, consisting of a series of characters, where each character is equivalent to one byte.
Strings can be divided into pure English strings, pure Chinese strings and mixed Chinese and English strings. Therefore, Chinese characters can be placed in the php array.
Example 1: Put pure English strings in php array
<?php header("Content-type:text/html;charset=utf-8"); $arr= array(1,2,"hello",TRUE,[3,4]); var_dump($arr); ?>
Example 2: php array Put the pure Chinese string
<?php header("Content-type:text/html;charset=utf-8"); $arr= array("你好","汤姆","hello",TRUE,[3,4]); var_dump($arr); ?>
. It should be noted that in PHP, one Chinese character occupies three characters, so the length of the two Chinese strings in the above example For 6.
Example 3: Put Chinese and English mixed strings in the php array
<?php header("Content-type:text/html;charset=utf-8"); $arr= array("你好124","23汤姆","hello",TRUE,[3,4]); var_dump($arr); ?>
Extended knowledge: Chinese Two processing functions for strings
1. Chinese interception: mb_substr()
mb_substr( $str, $start, $length, $encoding )
$str, the string that needs to be truncated
$start, the starting point of truncation, the starting point is 0
<?php header("Content-type:text/html;charset=utf-8"); $str='php中文网://www.php.cn'; echo mb_substr($str,0,4,'utf-8');//截取头5个字,假定此代码所在php文件的编码为utf-8 ?>2. Get the Chinese length: mb_strlen()
mb_strlen( $str, $encoding )
<?php header("Content-type:text/html;charset=utf-8"); $str='php中文网://www.php.cn'; echo mb_strlen($str,'utf-8');//假定此代码所在php文件的编码为utf-8 ?>Recommended learning: "
PHP Video Tutorial"
The above is the detailed content of Can’t Chinese characters be placed in the php array?. For more information, please follow other related articles on the PHP Chinese website!