国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

Table of Contents
單引號(hào)
Heredoc結(jié)構(gòu)
Nowdoc結(jié)構(gòu)
變量解析
簡(jiǎn)單句法規(guī)則
復(fù)雜句法規(guī)則
存取和修改字符串中的字符
有用的函數(shù)和操作符
轉(zhuǎn)換成字符串
字符串轉(zhuǎn)變成數(shù)字
Home php教程 php手冊(cè) PHP 第二節(jié) 數(shù)據(jù)類型之字符串類型

PHP 第二節(jié) 數(shù)據(jù)類型之字符串類型

Jun 13, 2016 pm 12:01 PM
php Down apostrophe Can string definition data Way type pass

PHP中一個(gè)字符串可以通過下面的4種方式來(lái)定義:

  • 單引號(hào)
  • 雙引號(hào)
  • heredoc 語(yǔ)法結(jié)構(gòu)
  • nowdoc 語(yǔ)法結(jié)構(gòu) (自PHP 5.3.0以后)

單引號(hào)

定義一個(gè)字符串 的最簡(jiǎn)單的方法是用單引號(hào)把它包圍起來(lái) (標(biāo)點(diǎn)符號(hào) ')。

如果想要輸出一個(gè)單引號(hào),需在它的前面加個(gè)反斜線 (\)。在單引號(hào)前或在字符串的結(jié)尾處 想要輸出反斜線,輸入兩條 (\\)。注意,如果在任何其它的字符前加了反斜線,反斜線將會(huì)被直接輸出。

Note: 不像雙引號(hào)heredoc語(yǔ)法結(jié)構(gòu), 在單引號(hào)字符串中的變量 和特殊含義的字符將不會(huì)被替換。

如果字符串是包圍在雙引號(hào)(")中, PHP將對(duì)一些轉(zhuǎn)義字符進(jìn)行解析,和單引號(hào)字符串一樣, 如果輸出轉(zhuǎn)義字符之外的字符,反斜線會(huì)被打印出來(lái)。 PHP5.1.1以前,\{$var} 中的反斜線還不會(huì)被顯示出來(lái)。

用雙引號(hào)定義的 字符串最重要的特征是變量會(huì)被執(zhí)行。

Heredoc結(jié)構(gòu)

第三種定義字符串的方法是用heredoc句法結(jié)構(gòu):。在該提示 符后面,要定義個(gè)標(biāo)識(shí)符,然后是一個(gè)新行。接下來(lái)是字符串本身,最后要用前面定義的標(biāo)識(shí)符作為結(jié)束標(biāo)志。

結(jié)束時(shí)所引用的標(biāo)識(shí)符必須在一行的開始位置, 而且,標(biāo)識(shí)符的命名也要像其它標(biāo)簽一樣遵守PHP的規(guī)則:只能包含 字母、數(shù)字和下劃線,并且不能用數(shù)字和下劃線作為開頭。

Warning要注意的是結(jié)束標(biāo)識(shí)符這行除了 可能有一個(gè)分號(hào)(;)外,絕對(duì)不能包括 其它字符。這意味著標(biāo)識(shí)符不能縮進(jìn),分號(hào)的前后也不能有任何空白或tabs。更重要的是結(jié)束標(biāo)識(shí)符的前面必須是個(gè)被本地 操作系統(tǒng)認(rèn)可的新行標(biāo)簽,比如在UNIX和Mac OS X系統(tǒng)中是\n ,而結(jié)束標(biāo)識(shí)符(可能有個(gè)分號(hào))的后面也必須跟個(gè) 新行標(biāo)簽。

如果不遵守該規(guī)則導(dǎo)致結(jié)束標(biāo)簽不“干凈”,PHP將認(rèn)為它不是結(jié)束標(biāo)識(shí)符而繼續(xù)尋找。如果在文件結(jié)束前也沒有找到一個(gè)正確的結(jié)束標(biāo)識(shí)符,PHP將會(huì)在最后一 行產(chǎn)生一個(gè)句法錯(cuò)誤。

Heredoc結(jié)構(gòu)就象是沒有使用雙引號(hào)的雙引號(hào)字符串, 這就是說(shuō)在heredoc結(jié)構(gòu)中引號(hào)不用被替換,但是上文中列出的字符 (\n等)也可使用。 變量將被替換,但在heredoc結(jié)構(gòu)中字符串表達(dá)復(fù)雜變量時(shí),要格外小心。

Nowdoc結(jié)構(gòu)

就象heredoc結(jié)構(gòu)類似于雙引號(hào)字符串,Nowdoc結(jié)構(gòu)是類似于單引號(hào)字符串的。Nowdoc結(jié)構(gòu)很象heredoc結(jié)構(gòu),但是 nowdoc不進(jìn)行解析操作 。 這種結(jié)構(gòu)很適合用在不需要進(jìn)行轉(zhuǎn)義的PHP代碼和其它大段文本。與SGML的 結(jié)構(gòu)是用來(lái)聲明大段的不用解析的文本類似,nowdoc結(jié)構(gòu)也有相同的特征。

一個(gè)nowdoc結(jié)構(gòu)也用和heredocs結(jié)構(gòu)一樣的標(biāo)記 , 但是跟在后面的標(biāo)志符要用 單引號(hào)括起來(lái),就像這樣。heredocs結(jié)構(gòu)的所有規(guī)則也同樣適用于nowdoc結(jié) 構(gòu),尤其是結(jié)束標(biāo)志符的規(guī)則。

變量解析

當(dāng)字符串用雙引號(hào)或heredoc結(jié)構(gòu)定義時(shí),其中的變 量將會(huì)被解析。

這里共有兩種語(yǔ)法規(guī)則:一種簡(jiǎn)單 規(guī)則,一種復(fù)雜規(guī) 則。簡(jiǎn)單的句法規(guī)則是最常用和最方便的, 它可以用最少的代碼在一個(gè)字符串中加入變量, 數(shù)組 值,或 對(duì)象屬性。

復(fù)雜的句法規(guī)則是在PHP4以后加入的,被花括號(hào)包圍的表達(dá)式是其明顯標(biāo)記。

簡(jiǎn)單句法規(guī)則

當(dāng)PHP解析器遇到一個(gè)美元符號(hào) ($) , 它會(huì)和其它很多解析器一樣,去盡量形成一個(gè)合法的變量名??梢杂没ɡ?號(hào)來(lái)明確變量名的界線。

?

如果想要表達(dá)更復(fù)雜的結(jié)構(gòu),請(qǐng)用復(fù)雜句法規(guī)則。

?

復(fù)雜句法規(guī)則

?

復(fù)雜句法規(guī)則不是結(jié)構(gòu)復(fù)雜而命名,而是因?yàn)樗梢允褂脧?fù)雜的表達(dá)式。

任何想用在字符串中標(biāo)量變量,數(shù)組變量或?qū)ο髮傩远伎墒褂眠@種方法。 只需簡(jiǎn)單地像在字符串以外的地方那樣寫出表達(dá)式, 然后用花括號(hào){}把它括起來(lái)。 由于 { 無(wú)法被轉(zhuǎn)義,只有 $ 要緊挨著 {才會(huì)被認(rèn)出來(lái),可以用 {\$ 來(lái)表達(dá) {$

?

存取和修改字符串中的字符

?

字符串中的字符可以通過一個(gè)以0為開始的,用類似數(shù)組結(jié)構(gòu)中的方括號(hào)包含對(duì)應(yīng)的數(shù)字來(lái)查找和修改,比如 $str[42], 可以把 字符串想像數(shù)組 。 函數(shù) substr()substr_replace()可以用來(lái)實(shí)現(xiàn)多于一個(gè)字符 的情況。

?

Warning方括號(hào)中的數(shù)字超出范圍將會(huì)產(chǎn)生空白。非整數(shù)類型被轉(zhuǎn)換成整數(shù),非整數(shù)類型轉(zhuǎn)變成整數(shù),非法類型會(huì)產(chǎn)生一個(gè) E_NOTICE級(jí)別錯(cuò)誤,負(fù)數(shù)在寫入時(shí)會(huì)產(chǎn)生一個(gè)E_NOTICE,但讀 取的是空字符串。被指定的字符串只有第一個(gè)字符可用,空字符串用指定為空字節(jié)。

?

有用的函數(shù)和操作符

字符串可以用'.' (點(diǎn)) 操作符連接起來(lái), 注意 '+' (加號(hào)) 操作符沒有 這個(gè)功能。?

對(duì)于字符串的操作有很多有用的函數(shù)。

可以參考 字符串函數(shù) 了解大部分函數(shù), 高級(jí)的查找&替換功能可以參考 正則表達(dá)式函數(shù)Perl類型的正則 表達(dá)式函數(shù)。

另外還有URL字符串的函數(shù), 也有加密/解密字符串的函數(shù)。 (mcryptmhash).

最后,可以參考 字符類型函數(shù)

?

轉(zhuǎn)換成字符串

?

一個(gè)值可以通過在其前面加上(string)或用strval()函數(shù)來(lái)轉(zhuǎn)變成 字符串。 在一個(gè)需要字符串的表達(dá)式中,字符串會(huì)自動(dòng)轉(zhuǎn)變,比如在使用函數(shù) echoprint 時(shí), 或在一個(gè)變量和一個(gè) 字符串 進(jìn)行比較時(shí),就會(huì)發(fā)生這種轉(zhuǎn)變 類型類型轉(zhuǎn)換 可以更好的解釋下面的事情,也可參考函 數(shù) settype()

?

一個(gè)boolean TRUE 值被轉(zhuǎn)換成 字符串"1"。 Boolean FALSE 被轉(zhuǎn)換成"" (空的字符串)。 這種轉(zhuǎn)變可以在 boolean字符串 之間往返進(jìn)行。

?

一個(gè) 整數(shù)浮點(diǎn)數(shù) 被轉(zhuǎn)變?yōu)閿?shù)字的字面樣式的字符串 (包括 浮點(diǎn)數(shù)中的指數(shù)部分),使用指數(shù)計(jì)數(shù)法的浮點(diǎn)數(shù) (4.1E+6)也可轉(zhuǎn)變。

數(shù)組轉(zhuǎn)換成 字符串 "Array",因此, echoprint c無(wú)法顯示出該數(shù)組的值。如果顯示一個(gè)數(shù)組值,可以用 echo $arr['foo']這種結(jié)構(gòu),更多內(nèi)容見下文。

在PHP 4中對(duì)象被轉(zhuǎn)換成 字符串 "Object", 為了調(diào)試原因需要打印出對(duì)象的值,方法見正文。為了得到對(duì)象的類的名稱,可以用 get_class() 函數(shù)。 在PHP5中, 可以用 __toString 。

資源總會(huì)被轉(zhuǎn)變成"Resource id #1"這種結(jié)構(gòu)的字符串 , 其中的 1 是PHP分配給該資源的獨(dú)特?cái)?shù)字。不用過多關(guān)注這種結(jié)構(gòu),它馬上要轉(zhuǎn)變了。為了得到一個(gè) resource類型,可以用函數(shù) get_resource_type()。

NULL 總是被轉(zhuǎn)變成空的字符串。

如上面所說(shuō)的,直接把數(shù)組, 對(duì)象資源 轉(zhuǎn)換成 字符串 不會(huì)得到超出其自身的更多信息??梢允褂煤瘮?shù) print_r()var_dump() 列出這些類型的內(nèi)容。

大部分的PHP值可以轉(zhuǎn)變成 字符串s 來(lái)長(zhǎng)期儲(chǔ)存,這被稱作串行化,可以用函數(shù) serialize() 來(lái)實(shí)現(xiàn)。 如果PHP引擎設(shè)定支持 WDDX , PHP值也可儲(chǔ)存成XML格式。

字符串轉(zhuǎn)變成數(shù)字

當(dāng)一個(gè)字符串被用在了一個(gè)數(shù)字的環(huán)境中,結(jié)果和類型如下:

如果字符串沒有包含 '.','e'或'E' 并且數(shù)字值符合整數(shù)類型的限定 ( PHP_INT_MAX定義的), 這個(gè) 字符串 可被認(rèn)定是一個(gè) integer, 在其它情況下被認(rèn)定為一個(gè)float。

字符串的開始部分給定了它的值,如果 字符串 以合法的數(shù)字開始,這個(gè)數(shù)字可直接使用。 否則,值就是 0 (零)。 合法數(shù)值由符號(hào),后面跟著一個(gè)或多個(gè)數(shù)字(可能有個(gè)小數(shù)點(diǎn)),再跟著可選的指數(shù)符號(hào)如'e' 或 'E',后面跟著一個(gè)或多個(gè)數(shù)字。

不要想像在C語(yǔ)言中的那樣,通過一個(gè)整數(shù)轉(zhuǎn)換得到相應(yīng)字符,使用函數(shù) ord()chr() 實(shí)現(xiàn)ASCII碼和字符間的轉(zhuǎn)換。

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

Undress AI Tool

Undress AI Tool

Undress images for free

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.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

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)

Why We Comment: A PHP Guide Why We Comment: A PHP Guide Jul 15, 2025 am 02:48 AM

PHPhasthreecommentstyles://,#forsingle-lineand/.../formulti-line.Usecommentstoexplainwhycodeexists,notwhatitdoes.MarkTODO/FIXMEitemsanddisablecodetemporarilyduringdebugging.Avoidover-commentingsimplelogic.Writeconcise,grammaticallycorrectcommentsandu

How Do You Handle File Operations (Reading/Writing) in PHP? How Do You Handle File Operations (Reading/Writing) in PHP? Jul 16, 2025 am 03:48 AM

TohandlefileoperationsinPHP,useappropriatefunctionsandmodes.1.Toreadafile,usefile_get_contents()forsmallfilesorfgets()inaloopforline-by-lineprocessing.2.Towritetoafile,usefile_put_contents()forsimplewritesorappendingwiththeFILE_APPENDflag,orfwrite()w

How to Install PHP on Windows How to Install PHP on Windows Jul 15, 2025 am 02:46 AM

The key steps to install PHP on Windows include: 1. Download the appropriate PHP version and decompress it. It is recommended to use ThreadSafe version with Apache or NonThreadSafe version with Nginx; 2. Configure the php.ini file and rename php.ini-development or php.ini-production to php.ini; 3. Add the PHP path to the system environment variable Path for command line use; 4. Test whether PHP is installed successfully, execute php-v through the command line and run the built-in server to test the parsing capabilities; 5. If you use Apache, you need to configure P in httpd.conf

Your First PHP Script: A Practical Introduction Your First PHP Script: A Practical Introduction Jul 16, 2025 am 03:42 AM

How to start writing your first PHP script? First, set up the local development environment, install XAMPP/MAMP/LAMP, and use a text editor to understand the server's running principle. Secondly, create a file called hello.php, enter the basic code and run the test. Third, learn to use PHP and HTML to achieve dynamic content output. Finally, pay attention to common errors such as missing semicolons, citation issues, and file extension errors, and enable error reports for debugging.

What is PHP and What is it Used For? What is PHP and What is it Used For? Jul 16, 2025 am 03:45 AM

PHPisaserver-sidescriptinglanguageusedforwebdevelopment,especiallyfordynamicwebsitesandCMSplatformslikeWordPress.Itrunsontheserver,processesdata,interactswithdatabases,andsendsHTMLtobrowsers.Commonusesincludeuserauthentication,e-commerceplatforms,for

PHP Syntax: The Basics PHP Syntax: The Basics Jul 15, 2025 am 02:46 AM

The basic syntax of PHP includes four key points: 1. The PHP tag must be ended, and the use of complete tags is recommended; 2. Echo and print are commonly used for output content, among which echo supports multiple parameters and is more efficient; 3. The annotation methods include //, # and //, to improve code readability; 4. Each statement must end with a semicolon, and spaces and line breaks do not affect execution but affect readability. Mastering these basic rules can help write clear and stable PHP code.

PHP 8 Installation Guide PHP 8 Installation Guide Jul 16, 2025 am 03:41 AM

The steps to install PHP8 on Ubuntu are: 1. Update the software package list; 2. Install PHP8 and basic components; 3. Check the version to confirm that the installation is successful; 4. Install additional modules as needed. Windows users can download and decompress the ZIP package, then modify the configuration file, enable extensions, and add the path to environment variables. macOS users recommend using Homebrew to install, and perform steps such as adding tap, installing PHP8, setting the default version and verifying the version. Although the installation methods are different under different systems, the process is clear, so you can choose the right method according to the purpose.

python if else example python if else example Jul 15, 2025 am 02:55 AM

The key to writing Python's ifelse statements is to understand the logical structure and details. 1. The infrastructure is to execute a piece of code if conditions are established, otherwise the else part is executed, else is optional; 2. Multi-condition judgment is implemented with elif, and it is executed sequentially and stopped once it is met; 3. Nested if is used for further subdivision judgment, it is recommended not to exceed two layers; 4. A ternary expression can be used to replace simple ifelse in a simple scenario. Only by paying attention to indentation, conditional order and logical integrity can we write clear and stable judgment codes.

See all articles