string in php

韦小宝
Release: 2023-03-17 17:24:01
Original
3190 people have browsed it

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]+}


##Similarly, In a string represented by double quotes, single quotes do not need to be escaped.


The string represented by double quotes has another important feature, which is that it will parse the variables in the string:

"Hi, $str."
Copy after login

Once the syntax analyzer encounters the $ symbol, It will get the string between the $ symbol and the next character that is not an English letter, Arabic numeral or underscore, and treat it as a variable name. If there is no such variable in the program, it will be automatically ignored.

If you want to output the $ symbol, you must use the escape character \$



##heredoc syntax

Syntax format:

<<

String content

str


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, <<

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.


heredoc syntax will parse variables in the string the same as double quote notation.

Note that when Heredocs include variable parsing, they cannot be used to initialize class attributes.



nowdoc syntax

Grammar format:

<<<'str'

String content


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 <<<&#39;str1&#39;  
My name is $name.<br>  
str1;  
?>
Copy after login

Variable parsing in stringThe simplest way is to write the variable name directly, for example:

$name = &#39;Ann&#39;;  
$str = "hello $name!";
Copy after login

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.


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.

One exception is array elements, which use ] to identify the end of the variable, so there can be other letters or numbers after ].



If you want to better control the variable name or use a complex expression, you can use {} to assist identification.

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.


3. If the $ symbol is next to the variable name, there must be no space between them.

4.解析数组元素时,只有通过花括号语法才能正确解析带引号的键名

$str = "hello ${name}";  
$str = "hello ${  name  }";  
$str = "hello {$name  }";  
$str = "hello {$  name  }";  // 出错
Copy after login

只使用一层花括号时,无法处理函数或方法的返回值或者类常量以及类静态变量。正确的做法是使用两层花括号:

{${getName()}}            // 函数  
{${$object->getName()}}   // 方法  
{${beers::softdrink}}     // 类常量  
{${beers::$ale}}          // 类变量
Copy after login

下面的例子演示了字符串中的可变变量

$name = &#39;Ann&#39;;  
$Ann = &#39;Jeck&#39;;  
echo "hello {${$name}}"; // hello Jeck  
echo "hello ${$name}";   // hello Jeck  
  
class foo {  
  var $bar = &#39;I am bar.&#39;;  
}  
$foo = new foo();  
$bar = &#39;bar&#39;;  
$baz = array(&#39;foo&#39;, &#39;bar&#39;, &#39;baz&#39;, &#39;quux&#39;);  
echo "{$foo->$bar}\n";          // I am bar.  
echo "{$foo->{$baz[1]}}\n";     // I am bar.
Copy after login

以数组的形式访问字符串

一个字符串可以当成一个可读写的数组进行访问,这时,数组的键固定为0、1、2...

可以使用方括号或花括号来访问字符串中的元素:

$str = "abcdefg";  
$str[0] = "2";  
$str{1} = 3;  
echo var_dump($str);  // string(7) "23cdefg"
Copy after login

注意,用超出字符串长度的下标写入将会拉长该字符串并以空格填充。非整数类型下标会被转换成整数。非法下标类型会产生一个 E_NOTICE 级别错误。用负数下标写入字符串时会产生一个 E_NOTICE 级别错误,用负数下标读取字符串时返回空字符串。写入时只用到了赋值字符串的第一个字符。用空字符串赋值则赋给的值是 NULL 字符。
PHP 的字符串在内部是字节组成的数组。因此用花括号访问或修改字符串对多字节字符集很不安全。仅应对单字节编码例如 ISO-8859-1 的字符串进行此类操作。

运算符

字符串支持以下这些运算符:

. .=

该运算符用于连接两个字符串:

$str1 = "hello " . "world";  
$str1 .= "!";  
echo $str1;     // hello world!
Copy after login

字符串函数

strlen(str)

返回字符串的长度

$str1 = "abcdefg";  
$str2 = "大家好!";  
echo strlen($str1); // 7  
echo strlen($str2); // 10
Copy after login

以上就是本文的所有内容,希望可以给你带来对字符串的新认识哦~

相关推荐:

最全的php字符串处理函数

php字符串转换为小写的函数strtolower()

几个被人遗忘的PHP字符串处理函数

The above is the detailed content of string in php. 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