First, quotation marks definition string 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" PHP syntax analyzer is used Quotation marks to determine a string. Therefore, all strings must use the same order or dual 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!" Defines the string, only one quotation is considered a definition, namely single quotes or Double quotes. So, if a string starts with a double quotation, only double quotation is analyzed. This way, you can include any other characters, or even single quotes in a dual quotation string. The following quotation strings are legal: $ s = "i am a 'single quote string"; $ s =' i am a "double quote string" inside a single quote string "; when PHP encounter When the quotation corresponding to the beginning of the string, it is considered that the tail of the string is that: "why doesn't" "Work?" Is actually divided into three parts by the PHP syntax analyzer: "why doesn't" - Contains a single quotes of double quotes string this - excess characters, the analyzer cannot handle "Work?" - This example of the normal string is attempted to include double quotes in the dual quotation string, and the analyzer is encountered When the second double quotes, the string is considered to end. To achieve the purpose of including quotation marks, you must analyze the original intention when you encounter ordinary quotes in the serial number, we add a backslash in front of quotation marks to tell PHP: This quotation is part of the string, the correct representation The method is this: "why doesn't /" That / "Work?" A common problem in English string is the use of the nosen ', because it is a single quotes, and it is very common in English strings (English grid). You have to take care of these characters: 'You /' D better escape your apostrophes' can see that the backslash has his special meaning in the string, when we need to include the backslash in the string, you need to Add a backslash in front of the symbol. For example: $ file = "c: /windows/system.ini"; echo $ file; // print results are: c: Windowssystem.ini $ file = "c: //windows/system.ini"; Echo $ file // The print result is: C: /Windows/system.ini another string definition method, can eliminate the trouble of special characters, and it is easy to reference longer text. The string definition method begins with the <<< symbol follows a custom string, and the last line ends with this custom string and must be topped.
Second, the string connection string can be connected using a string connection (.), Such as $ first_name = 'charlie'; $ last_name = 'brown'; $ full_name = $ first_name. ''. $ Last_name; common The use is to establish a large block of HTML string code, assign the value (=) connection (.) Can be simplified as (. =) Symbol, such as: $ html = '
Number TD> | Square TD> TD> '; for ($ I = 0; $ I <10; $ I ) {$ Square = $ I * $ I; $ HTML . = ' |
'. $ I. ' td> | '. $ Square. ' td> tr>';} $ html. = ' table>'; Third, using variables in the strings allow you to use the connection symbol to stick a lot of simple strings. PHP allows us to directly contain a string variable in a double quotation string, we can find that the processing result of the following two strings is the same. $ full_name = $ first_name. ''. $ last_name; $ full_name = "$ first_NAME $ last_name"; the processing of single-quaternary strings and duplex skeers in PHP is different. The contents in the double-lead number can be interpreted and replaced, while the contents in the single summit string are always considered to be a normal character. For example: $ 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 Results: Foo IS 2 Echo 'Foo IS $ FOO / N'; // Print Results: Foo IS $ FOO / N As you can see, in single quantum string, even a backslash Lost his extended meaning (except inserted backslash // and insert single quotes / '). So, when you want to change the variable to the string and contain / n (newline), you should use the double quotes. Single-quatroms can be used anywhere, and the single quotular string processing speed will be faster in the script, because the PHP syntax analyzer is simple to process the single quotes, and the double quotes have also needed to resolve due to the inside of the string. Therefore, more complex, so the processing speed is slightly slow. When a complex variable combination is referenced in a string, some problems may be generated. The following code will work properly: echo "value = $ foo"; echo "value = $ A [$ I]"; and the code below cannot Get our hope: echo "value = $ a [$ I] [$ j]"; // We want to print a certain element of the 2D array $ A.
To avoid potential problems in these strings, we usually leave complex variables from strings, just like this: echo 'value ='. $ A [$ I] [$ j]; there is a way It is a complicated variable to enclose the complicated variables. The grammatical analyzer can correctly identify: echo "value = {$ A [$ I] [$ j]}" // Print a 2D array $ A This is the element, There is a new problem again. When we want to reference the quadratic character in the string, you will remember to use the escape character: $ VAR = 3; echo "value = {$ var}"; // Print Results "Value = 3" Echo "Value = / {$ VAR} "; // Print Results" Value = {3} "three, slashes and SQL statements Generate HTML code or SQL query statements often encounter and interesting things when writing PHP programs. Why do you say this, because this involves generating another type of code, you must carefully consider and follow the written grammar and rules required by this code. Let's look at such an example, if you want to query the name "O'Keefe" in the database, usually the form of the SQL statement is: select * from users where last_name = 'o /' keefe 'Please pay attention to the SQL statement English is required to use a backslash transfusion. PHP provides some functions to handle such situations, the use of function addslashes ($ STR) is automatically inserted into the quotation marks in the string into the backlaps: $ last_name = "o'keefe"; $ sql = " Select * from users where last_name = '"."' ";" In this example, you have to enclose single quotes (SQL grammar requirements) outside the last_name string, because it is double quotes here. Skewers, so there is no need to use this for single quotes. The following statement is an equivalent form using a single quotem string: $ sql = 'select * from users where last_name = /' '. Addslashes ($ last_name).' / ''; You have to write in the database at any time Strings, you must make sure that the quotes inside correctly use the escape symbol, which is a mistake of many PHP beginners. Fourth, double quotes and HTMLs are different from SQL statements. Double quotes are often used in standard HTML language (now many browsers have strong fault tolerance), allowing single quotes in HTML or even quotes to represent strings ), For example: $ html = ' $ LINK "; HTML language does not support backslash escape, this will experience it when we use the Hidden INPUTS of the form to transmit data. The best way to set the value of HIDDEN INPUTS is to encode the HTMLSpecialChars () function.
转载请注明原文地址:https://www.9cbs.com/read-55551.html New Post(0)
|