PHP programming: Discourse of exploration strings
[2004-05-21 14:32:48]
[Show Light Source Code]
In many web programming, strings are always generated and processed in large quantities. Use and process strings correctly, for the PHP
The sequence is also more and more important. This article has always guided you to high-level string processing skills from the simplest string definition.
Help everyone.
First, quotation mark definition strings
In PHP, usually a string is defined in a pair of quotes, such as:
'I am a string in single quotes'
"I am a string in double quotes"
The PHP syntax analyzer is to determine a string with a pair of quotes. Therefore, all strings must use the same order or double
Quotation marks to define start and end. For example, the string definition below is illegal:
"I am Not a valid string Since i Have unmatching quote Marks'
'Me neither! "
When defining a string, only one quotation is considered a definition, namely a single quotes or a double quotes. So, if a string is brought
Start, then only double quotation is analyzed. This way, you can include any other characters in a dual quotation string, even single
number. The following quotation strings are legal:
$ 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 quotes corresponding to the beginning of the string, it is considered that the tail of the string is that the tail of the character is reached.
"Why doesn't" this "work?"
In fact, it is divided into three parts by the PHP syntax analyzer:
"Why doesn't" - contains a double quotes of single quotes
This - Excess characters, analyzers cannot be processed
"WORK?" - normal string
The above example attempts to include double quotes in the double quotation string, and the analyzer thinks the string of string when encountering the second double quotes.
Bunch. To reach the purpose of including quotation, the analyzer must be analyzed to ignore its original intention, we are in quotes.
In the front, a backslash tells PHP: This quotation marks are part of the string, the correct representation is true:
"Why doesn't /" That / "WORK?"
A common problem in English string is the use of the somarcies', because it is a single quotes, and it is very common in the English string.
(English all grid). You have to take care of these characters carefully:
'You /' D Better Escape your Apostrophes'
It can be seen that the backslash has his special meaning in the string. When we need to include the backslash in the string itself, it needs to be
A backslash is added in front of the symbol. E.g:
$ file = "c: /windows/system.ini";
Echo $ file; // Print results are: C: WindowsSystem.ini
$ file = "c: //windows//system.ini";
Echo $ file; // Print results are: c: /windows/system.ini
Another string definition method can eliminate the troubles of special characters, and it is easy to reference longer text. The string definition method begins with the <<< symbol with a custom string, and the last row ends with this custom string and must be topped.
Second, the connection of strings
Strings can be connected using a string connection (.), Such as:
$ first_name = 'charlie';
$ last_name = 'brown';
$ ful__name = $ first_name. ''. $ last_name;
Common uses is to establish a large block of HTML string code, assign the value (=) connection (.) Can be simplified as (. =).
No., such as:
$ html = '
Number td> | Square td> tr>';
For ($ I = 0; $ i <10; $ i ) { $ Square = $ i * $ i; $ html. = ' |
'. $ I. ' td> | '. $ square. ' td> tr>';
} $ html. = ' table>'; Third, use variables in the strings This feature allows you to use a connection symbol to stick a lot of simple strings. PHP allows us to include the word in double quotation string String variables, we can find that the results of the following two strings are the same. $ ful__name = $ first_name. ''. $ last_name; $ full_name = "$ first_name $ last_name"; The processing of single quantity strings and duplex strings in PHP is different. The content in the double-quotes can be interpreted and replaced, and single The content in the string is always considered to be ordinary characters. E.g: $ foo = 2; Echo "foo is $ foo"; // Print Results: foo is 2 Echo 'foo is $ foo'; // Print Results: foo is $ foo Echo "Foo IS $ FOO / N"; // Print Result: Foo IS 2 (At the same time) Echo 'Foo IS $ FOO / N'; // Print Results: Foo IS $ FOO / N As you can see, even the backslash in the single quarter is lost (except inserting a backslash // and insertion quotation marks/'). So, when you want to change the variable to the string and contain / n (newline), you should use double quotation number. Single number strings can be used anywhere, and the single quotular string processing speed will be faster in the script because the PHP syntax analyzer is The processing method of single-leading skeers is relatively simple, and the processing of the double quotes also needs to be parsed due to the inside of the string, so more complex, so the processing speed Slightly slow. When references complex variable combinations in a string, some problems may be generated, and the following code will work properly: echo "value = $ foo"; echo "value = $ a [$ i]"; The following code cannot get the results of our hopes: echo "value = $ a [$ i] [$ j]"; // We want to print an element of the 2D array $ A. To avoid potential problems in these strings, we usually leave complex variables from the strings, just like this: Echo 'Value ='. $ A [$ I] [$ J]; Another way is to enclose complex variables to parentheses, grammatical analyzers can identify: echo "value = {$ a [$ I] [$ j]}" // Print a two-dimensional array $ A In this way, a new problem occurs. When we want to reference the quadratic characters in the string, remember to use the escape character: $ VAR = 3; echo "value = {$ var}"; // Print results "value = 3" echo "value = / {$ var}"; // Print results "value = {3}" Third, slash and SQL statement Generating an HTML code or SQL query statement is often encountered when writing a PHP program and is a fun thing. Why do you say this? Because this involves generating another type of code, you must carefully consider and follow the written grammar and regulations required by this code. then. Let's see such an example, if you want to query the name of the name "O'Keefe" in the database, usually the form of the SQL statement Is such that: Select * from users where last_name = 'o /' keefe ' Note that the SQL statement is in English, and a backslash transfusion is required. PHP provides some functions to handle this The situation, the use of the function addslashes ($ STR) is automatically inserted in the string to insert a backslash escape: $ last_name = "o'keefe"; $ SQL = "Select * from users where last_name = '". Addslashes ($ last_name). "'"; In this example, you have to enclose a single quotation number (SQL grammar requirements) outside the Last_name string, because the double is used here Quotation marks, so there is no need to use this pair of single quotes. The following statement is the equivalence form using a single quotem string: $ SQL = 'SELECT * from users where last_name = /' '. Addslashes ($ last_name).' / ''; At any time you have to write a string in the database, you must make sure that the quotes inside correctly use the escape symbol, this is a lot of PHP The mistakes of beginners are often committed. Fourth, double quotes and HTML Unlike the SQL statement, dual quotes are often used in standard HTML languages (now many browsers have strong fault tolerance Yes, allowing single quotes in HTML or no quarters without quotation marks), for example: $ html = ' '; $ html = " $ link "; The HTML language does not support backslash righteousness, which will have some time when we use the Hidden Inputs of the form to transfer data. Experience. The best way to set the value of HIDDEN INPUTS is to encode the HTMLSpecialChars () function. The following statement can transmit data that may contain double quotes: 转载请注明原文地址:https://www.9cbs.com/read-104459.html New Post(0)
|