We all know that string operations, regardless of the language, are an important foundation, often simple and important. Just as people speak, they generally have shapes (graphical interfaces) and languages (print strings ?), Obviously, strings can explain more things. PHP provides a large number of string operation functions, which are powerful and easy to use. The following describes the functions and features of PHP string functions.
Weak PHP string functions
PHP is a weak type language, so other types of data can be directly applied to string operation functions, and automatically converted to string type for processing, such as echo substr ("1234567 ", 1, 3); and echo substr (123456,1, 3); is the same definition. Generally, a string is identified by double quotation marks or single quotation marks. For example
- $str = "i love u";
- $str = 'i love u';
The two have some differences. The latter treats all single quotes as characters; the former is not. For example
- $Test="Iwind";
- $Str="I love $ test";
- $Str1='I love $ Test';
- Echo $ str; // I love iwind
- Echo $ str1; // I love $ test
The two examples of the same behavior are different:
- Echo "I love test"; // you will get the I love est, which has been treated as escape
- Echo 'I love test'; // you will get the I love test
Therefore, we can simply think that the content in double quotation marks is "interpreted", and the single quotation marks are "What you see is what you get" (in particular, ''will be considered as ''). Obviously, double quotation marks are more flexible. Of course, single quotation marks are applicable to some special occasions and will not be described here. The most common output in PHP is echo and print. both are not real functions, but language structures. Therefore, you do not need to use double brackets (such as echo ("test"); print ("test") for calls ")). values can be assigned to both outputs: echo $ str = "test"; on the one hand, test is output, and test is assigned to the string variable $ strprint $ str = "test "; the two have different names. Print has a returned value and returns 1 for a while echo does not. Therefore, echo is faster than print:
- $Return=Print"Test ";
- Echo $ return; // output 1
For this reason, print can be applied to compound statements, but echo cannot:
- Isset ($ str) or print "str variable undefined"; // output "str variable undefined"
- Isset ($ str) or echo "str variable undefined"; // an analysis error is prompted.
- Echo can output multiple strings at a time, but print cannot:
- Echo "I", "love", "iwind"; // output "I love iwind"
- Print "I", "love", "iwind"; // an error is returned.
- Echo and print can also output a string called "Document Syntax". The syntax is as follows:
- Echo<<<Tag Name
The tag name of the PHP string function content. For example:
- echo <<< test
- i love iwind
- test;
Note that the two label names of the statement start and end are the same, and there cannot be blank before the last label name, that is, the top label should be written. The content of the syntactic output of the document recognizes variable names and common symbols, which are roughly the same as double quotation marks. In addition to echo and print output, PHP also provides some functions for formatting strings, such as printf, sprintf, vprintf, and vsprintf.