Deep understanding of PHP string functions

Source: Internet
Author: User

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

 
 
  1. $str = "i love u";  
  2. $str = 'i love u'; 

The two have some differences. The latter treats all single quotes as characters; the former is not. For example

 
 
  1. $Test="Iwind";
  2. $Str="I love $ test";
  3. $Str1='I love $ Test';
  4. Echo $ str; // I love iwind
  5. Echo $ str1; // I love $ test

The two examples of the same behavior are different:

 
 
  1. Echo "I love test"; // you will get the I love est, which has been treated as escape
  2. 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:

 
 
  1. $Return=Print"Test ";
  2. Echo $ return; // output 1

For this reason, print can be applied to compound statements, but echo cannot:

 
 
  1. Isset ($ str) or print "str variable undefined"; // output "str variable undefined"
  2. Isset ($ str) or echo "str variable undefined"; // an analysis error is prompted.
  3. Echo can output multiple strings at a time, but print cannot:
  4. Echo "I", "love", "iwind"; // output "I love iwind"
  5. Print "I", "love", "iwind"; // an error is returned.
  6. Echo and print can also output a string called "Document Syntax". The syntax is as follows:
  7. Echo<<<Tag Name

The tag name of the PHP string function content. For example:

 
 
  1. echo <<< test 
  2. i love iwind  
  3. 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.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.