PHP’sdata type is divided into eight data types, PHP string is also one of them, today we will explain it in detail PHPString.
Introduction
The implementation of strings in PHP is an array of bytes plus an integer indicating the buffer length. There is no information on how to convert bytes into characters; this is left to the programmer. There are no restrictions on what values a string consists of; in particular, a byte with a value of 0 can be anywhere in the string.
Since PHP does not specifically specify the encoding of the string, how is the string encoded? For example, the string "á" is equal to "\xE1" (ISO-8859-1), "\xC3\xA1" (UTF-8, C form), "\x61\xCC\x81" (UTF-8, D form) or any other possible expression? The answer is that the string will be encoded in the same encoding as the script file.
Representation of strings
php strings can be represented in 4 ways, including:
single quotes /Double quotes/heredoc/nowdoc
These four methods all support writing strings into multiple lines. At the same time, newlines and spaces in the string will be retained in the characters as they are, that is, the format in the source code will be maintained. If you want to eliminate line breaks, you can write it in a single line and use the string concatenation operator.
Single quotes are simple to use. When using single quotes, there are only two escape characters, namely \' and \\
If the string contains double quotes, the use of single quotes will be very concise.
Double quotes support more escape characters, such as \n and \t. Additionally, escape characters support octal notation, hexadecimal notation, and UTF-8 notation:
\[0-7]{1,3}
\x[ 0-9A-Fa-f]{1,2}
\u{[0-9A-Fa-f]+}
"Hi, $str."
##heredoc syntax
Syntax format:
<< String content str The identifier specifying the end of the string must be written on another line and starting from the first column. This line must not contain any other characters except possibly a semicolon (;) after it. This means that identifiers cannot be indented, nor can there be any whitespace or tabs before or after a semicolon. More importantly, the end identifier must be preceded by a newline recognized by the local operating system, such as \n in UNIX and Mac OS X systems, and the end delimiter (which may be followed by a semicolon) must also be followed immediately. Followed by a newline. Note that when Heredocs include variable parsing, they cannot be used to initialize class attributes. Grammar format: <<<'str' String content Variable parsing in stringThe simplest way is to write the variable name directly, for example: This method must have a character at the end of the variable name that cannot be used for variable naming, such as "!" in the above example, unless of course the string has ended. One exception is array elements, which use ] to identify the end of the variable, so there can be other letters or numbers after ]. Note: 1.$ and { must be close together, such as: ${ or {$, otherwise the curly braces will be parsed as an ordinary character in the string. 2. There must be a } symbol, otherwise an error will occur. 4.解析数组元素时,只有通过花括号语法才能正确解析带引号的键名 只使用一层花括号时,无法处理函数或方法的返回值或者类常量以及类静态变量。正确的做法是使用两层花括号: 下面的例子演示了字符串中的可变变量: 以数组的形式访问字符串 一个字符串可以当成一个可读写的数组进行访问,这时,数组的键固定为0、1、2... 可以使用方括号或花括号来访问字符串中的元素: 注意,用超出字符串长度的下标写入将会拉长该字符串并以空格填充。非整数类型下标会被转换成整数。非法下标类型会产生一个 E_NOTICE 级别错误。用负数下标写入字符串时会产生一个 E_NOTICE 级别错误,用负数下标读取字符串时返回空字符串。写入时只用到了赋值字符串的第一个字符。用空字符串赋值则赋给的值是 NULL 字符。 运算符 字符串支持以下这些运算符: . .= 该运算符用于连接两个字符串: 字符串函数 strlen(str) 返回字符串的长度 以上就是本文的所有内容,希望可以给你带来对字符串的新认识哦~ 相关推荐: The above is the detailed content of string in php. For more information, please follow other related articles on the PHP Chinese website!
Note that the above str is a Custom identifier (you can add double quotes), used to help mark the beginning and end of the string. In addition, <<
heredoc syntax will parse variables in the string the same as double quote notation.
nowdoc syntax
str
nowdoc syntax is similar to heredoc syntax, except that identifiers must be surrounded by single quotes. And nowdoc syntax will not parse variables in strings. <?php
$name = "pish";
echo <<<'str1'
My name is $name.<br>
str1;
?>
$name = 'Ann';
$str = "hello $name!";
If the variable name is followed by a letter or number, the variable will not be parsed correctly because the parser will get $ as soon as it encounters the $ symbol. The string between the symbol and the next character that is not an English letter, Arabic numeral or underscore is treated as a variable name. If there is no such variable in the program, it will be automatically ignored.
If you want to better control the variable name or use a complex expression, you can use {} to assist identification.
3. If the $ symbol is next to the variable name, there must be no space between them. $str = "hello ${name}";
$str = "hello ${ name }";
$str = "hello {$name }";
$str = "hello {$ name }"; // 出错
{${getName()}} // 函数
{${$object->getName()}} // 方法
{${beers::softdrink}} // 类常量
{${beers::$ale}} // 类变量
$name = 'Ann';
$Ann = 'Jeck';
echo "hello {${$name}}"; // hello Jeck
echo "hello ${$name}"; // hello Jeck
class foo {
var $bar = 'I am bar.';
}
$foo = new foo();
$bar = 'bar';
$baz = array('foo', 'bar', 'baz', 'quux');
echo "{$foo->$bar}\n"; // I am bar.
echo "{$foo->{$baz[1]}}\n"; // I am bar.
$str = "abcdefg";
$str[0] = "2";
$str{1} = 3;
echo var_dump($str); // string(7) "23cdefg"
PHP 的字符串在内部是字节组成的数组。因此用花括号访问或修改字符串对多字节字符集很不安全。仅应对单字节编码例如 ISO-8859-1 的字符串进行此类操作。$str1 = "hello " . "world";
$str1 .= "!";
echo $str1; // hello world!
$str1 = "abcdefg";
$str2 = "大家好!";
echo strlen($str1); // 7
echo strlen($str2); // 10