PHP programming: exploring the mysteries of strings

Source: Internet
Author: User
I. define a string using quotation marks in PHP. a string is usually defined in a pair of quotation marks, for example, IamastringinsinglequotesIamastringindoublequotesPHP syntax analyzer judges a string using pair of quotation marks. Therefore, all strings must use the same single or double quotation marks to define the start and end. Example 1: define a string using quotation marks

In PHP, a string is usually defined in a pair of quotation marks, such:

'I am a string in single quotes'
"I am a string in double quotes"

The PHP syntax analyzer uses pair quotation marks to determine a string. Therefore, all strings must use the same single or double quotation marks to define the start and end. For example, the following string definition is illegal:

"I am not a valid string since I have unmatching quote marks'
'Me neither! "

When defining a string, only one type of quotation mark is considered as a definition character, that is, single quotation marks or double quotation marks. Therefore, if a string starts with double quotation marks, only the double quotation marks are parsed by the analyzer. In this way, you can include any other character in the double quotation marks, or even single quotation marks. The following quotation marks are valid:

$ S = "I am a 'single quote string 'inside a double quote string ";
$ S = 'I am a "double quote string" inside a single quote string ';

When PHP encounters a quotation mark corresponding to the start of the string, it is considered to have reached the end of the string, so:

"Why doesn't" this "work? "

In fact, PHP syntax analyzer is divided into three parts:

"Why doesn' t" -- contains a single double quotation mark string
This -- Redundant characters, which cannot be processed by the analyzer
"Work? "-- Common string

The above example attempts to include double quotation marks in a double quotation mark string, and the analyzer considers the string to end when it encounters the second double quotation mark. To enclose quotation marks, the analyzer must ignore the original meaning of the regular quotation marks in a string. we add a backslash before the quotation marks to tell PHP: this quotation mark is a part of the string. the correct representation is as follows:

"Why doesn't \" that \ "work? "

A common problem in English strings is the use of the marker, because it is a single quotation mark, which is very common in English strings (all spaces in English ). You must handle these characters with caution:

You \ 'd better escape your apostrophes'

We can see that the backslash has its special meaning in the string. when we need to include the backslash itself in the string, we need to add a backslash before the symbol. For example:

$ File = "c: \ windows \ system. ini ";
Echo $ file; // The printed result is: c: windowssystem. ini.
$ File = "c :\\ windows \ system. ini ";
Echo $ file; // The printed result is: c: \ windows \ system. ini.

Another string definition method can eliminate the troubles of special characters and facilitate reference of long texts. This string defines the method as < <符号紧跟一个自定义字符串开头,最后一行以该自定义字符串结束,并且必须顶格。


2. string connection

A string can be connected using a string connector (.), for example:

$ First_name = 'Charlie ';
$ Last_name = 'brown ';
$ Full_name = $ first_name. ''. $ last_name;

The common purpose is to create a large HTML string code. The value pair (=) connector (.) can be abbreviated to the (. =) symbol, for example:

$ Html ='





';$ Html. =' ';For ($ I = 0; $ I <10; $ I ++ ){$ Square = $ I * $ I;$ Html. =' ';}$ Html. ='
Number Square
'. $ I .' '. $ Square .'
';


3. use variables in strings

This feature allows you to stick a large number of simple strings without using the concatenation symbol. PHP allows us to directly include string variables in double quotes. we can find that the processing results of the following two strings are the same.

$ Full_name = $ first_name. ''. $ last_name;
$ Full_name = "$ first_name $ last_name ";

The processing of single and double quotation marks is different in PHP. The content in the double quotation mark string can be interpreted and replaced, while the content in the single quotation mark string is always considered as a common character. For example:

$ Foo = 2;
Echo "foo is $ foo"; // print the result: foo is 2
Echo 'foo is $ Foo'; // print the result: foo is $ foo
Echo "foo is $ foo \ n"; // print the result: foo is 2 (line feed at the same time)
Echo 'foo is $ foo \ n'; // print the result: foo is $ foo \ n

As you can see, even the backslash in a single quotation mark loses its extended meaning (except for inserting the backslash \ and inserting the single quotation mark \'). Therefore, when you want to replace variables in strings and escape sequences including \ n (linefeed), you should use double quotation marks. Single quotation mark strings can be used anywhere else. the processing speed of single quotation mark strings in the script is faster, because the PHP syntax analyzer treats single quotation mark strings in a simple way, the processing of double quotation marks is more complicated because the strings also need to be parsed, so the processing speed is slightly slower.

When you reference a complex variable combination in a string, some problems may occur. the following code works properly:

Echo "value = $ foo ";
Echo "value = $ a [$ I]";

However, the following code cannot get the expected results:

Echo "value = $ a [$ I] [$ j]"; // we want to print an element of the two-dimensional array $.

To avoid potential problems in the use of these strings, we usually separate complex variables from the strings, like this:

Echo 'value = '. $ a [$ I] [$ j];

Another way is to enclose complex variables in curly brackets so that the syntax analyzer can correctly identify them:

Echo "value = {$ a [$ I] [$ j]}" // print an element of a two-dimensional array $

In this way, new problems have emerged. When we want to reference the curly braces in a string, remember to use the escape character:

$ Var = 3;
Echo "value = {$ var}"; // print the result "value = 3"
Echo "value =\{ $ var}"; // print the result "value = {3 }"


III. slashes and SQL statements

Generating HTML code or SQL query statements is a common and interesting issue when writing PHP programs. Why do you say this? because this involves generating another type of code, you must carefully consider and follow the syntax and rules required by the code.

Let's look at this example. if you want to query a database named "O 'Keefe", the SQL statement format is usually as follows:

Select * from users where last_name = 'O \ 'Keefe'

Note that the SQL statement must be escaped by a backslash. PHP provides some special functions to deal with such a situation. the Function AddSlashes ($ str) is used to automatically insert a backslash escape character into the string:

$ Last_name = "O 'Keefe ";
$ SQL = "select * from users where last_name = '". addslashes ($ last_name )."'";

In this example, you also need to enclose the single quotation marks (SQL syntax requirements) outside the last_name string. because the double quotation marks are used here, you do not need to use escape for the single quotation marks. The following statement uses the equivalent form of a single quotation mark string:

$ SQL = 'select * from users where last_name = \ '. addslashes ($ last_name ).'\';

At any time you want to write strings in the database, you must ensure that the quotation marks correctly use escape characters. this is a common mistake for PHP beginners.


IV. double quotation marks and HTML

Unlike SQL statements, double quotation marks ("double quotation marks") are often used to represent strings in standard HTML languages (many browsers now have strong fault-tolerant features that allow single quotation marks or even no quotation marks to represent strings in HTML ), for example:

$ Html = ''. $ link .'';
$ Html = "$ link ";

The HTML language does not support backslash escape. this is an experience when we use the form hidden inputs to transmit data. The best way to set the value of hidden inputs is to use the htmlspecialchars () function for encoding. The following statement transfers data that may contain double quotation marks:

  

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.