Table of Contents
String 字符串
字符串的表达方式
Home Backend Development PHP Problem What does a string in php consist of?

What does a string in php consist of?

Dec 15, 2022 pm 03:55 PM
php php string

在php中,字符串就是连续的字符序列,由一系列的字符(数字,字母和符号)组成;字符串的每个字符只占用一个字节。PHP定义字符串的方法:1、用单引号进行定义,例“'abc'”;2、用双引号进行定义,例“"abc"”;3、利用heredoc语法糖进行定义,它是一种在命令行shell和程序语言里定义字符串的方法;4、利用nowdoc语法糖进行定义。

What does a string in php consist of?

本教程操作环境:windows7系统、PHP8.1版、DELL G3电脑

String 字符串


字符串(string)就是连续的字符序列,由一系列的字符(数字,字母和符号)组成;其中每个字符都等同于一个字节。在php中只支持256字符集,不支持unicode。

php中的字符串的实现方式是由字节组成的数组再加上一个指明缓冲区长度的整型组成。该数据类型并不包含如何将字节转化为字符的信息,这些信息都是由程序员来控制的。字符串的组成没有什么限制,特别的是,其值为0即(null byte)的字节可以处于字符串的任何位置。还需要留意的是在相关的一些处理函数中,可能会把null字节之后的数据全部忽略掉,这点在使用的时候需要特别留意。

Note:String最大可以达到2GB

字符串的表达方式


1.单引号

定义一个字符串最简单的方式就是用单引号将字符串内容包含起来。在单引号定义的字符串中,变量和特殊字符的转义序列不会被替换。

$string_var = 'hello world';
        echo $string_var;//hello world
        echo "<br>";
        $string_var = &#39;hello \world&#39;;
        echo $string_var;//hello \world
        echo "<br>"; 
        $string_var2 = &#39;China $string_var&#39;;
        echo $string_var2;//China $string_var
        echo "<br>";
Copy after login

2.双引号

如果是以双引号将字符串内容包含起来的方式定义,那么需要注意:php会对一些特殊字符进行解析的,通常来说就是我们熟知的转义字符了。

序列    含义
\n    换行(ASCII 字符集中的 LF 或 0x0A (10))
\r    回车(ASCII 字符集中的 CR 或 0x0D (13))
\t    水平制表符(ASCII 字符集中的 HT 或 0x09 (9))
\v    垂直制表符(ASCII 字符集中的 VT 或 0x0B (11))(自 PHP 5.2.5 起)
\e    Escape(ASCII 字符集中的 ESC 或 0x1B (27))(自 PHP 5.4.0 起)
\f    换页(ASCII 字符集中的 FF 或 0x0C (12))(自 PHP 5.2.5 起)
\\    反斜线
\$    美元标记
\"    双引号
\[0-7]{1,3}    符合该正则表达式序列的是一个以八进制方式来表达的字符
\x[0-9A-Fa-f]{1,2}    符合该正则表达式序列的是一个以十六进制方式来表达的字符
Copy after login

和单引号定义方式比较最大的不同点是:双引号定义的字符串如果里面包含变量,那么该变量会被php解析。

$string_var = &#39;hello \world&#39;;
        echo $string_var;//hello \world
        echo "<br>"; 
        $string_var2 = &#39;China $string_var&#39;;
        echo $string_var2;//China $string_var
        echo "<br>";
        $string_var3 = "China $string_var";
        echo $string_var3;//China hello \world
        echo "<br>";
Copy after login

3.Heredoc结构

这种方式类似于xml定义格式,有一个开始标记和结束标记。heredoc的语法结构:

$var = <<< flag
……..here is your content
flag;
Copy after login

flag值可以根据自己的喜好来自定义,但必须要遵循php命名规范。另外需要注意的是在结束标识这行除了;外,不能包含其他字符。这就是说在标识符和分号前面不能有任何的缩进,字符。标识符和分号之间也是一样。如果没有遵循这条规则,php将认为该标识不是结束标识而将继续寻找,如果在文件结尾也没有找到的话,php将会产生解析错误。

$str = <<<ENDFLAG
            Example of string
ENDFLAG;
        echo $str;//Example of string
        echo "<br>";
Copy after login

4.Nowdoc结构

nowdoc结构和heredoc的结构很相似,但是nowdoc中不进行解析操作,也就是说nowdoc和单引号定义的字符串在一定意义来说是等价的。

nowdoc的结构和hereddoc的结构一样使用标记<<<,区别是在<<<之后的标识符要用单引号括起来(<<<’flag’)。

$str = <<<&#39;ENDFLAG&#39;
            Example of string
ENDFLAG;
        echo $str;//Example of string
        echo "<br>";
Copy after login

string可以当作字符数组来使用,其起始索引为0,比如有一个字符串$var=”hello world”,那么我们可以通过$var[0]的形式来获取或修改字符串。

$string_var4 = "hello world!";
        echo $string_var4[0];//h
        echo "<br>";
        $string_var4[0] = "f";
        echo $string_var4;//fello world!
        echo "<br>";
Copy after login

推荐学习:《PHP视频教程

The above is the detailed content of What does a string in php consist of?. For more information, please follow other related articles on the PHP Chinese website!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

CakePHP Quick Guide CakePHP Quick Guide Sep 10, 2024 pm 05:27 PM

CakePHP is an open source MVC framework. It makes developing, deploying and maintaining applications much easier. CakePHP has a number of libraries to reduce the overload of most common tasks.

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

See all articles