Differences between single quotes and double quotes in PHP _ PHP Tutorial

Source: Internet
Author: User
The difference between single quotes and double quotes in PHP. In programming languages, both single quotes and double quotes play an important role, as in PHP. Compared with ASP, PHP quotes are easier to use. in ASP, data should be replaced in programming languages, whether it is Single quotesOr Double quotation marksAnd has a very important role in PHPThe same is true for languages. PHP quotes are easier to use than ASP quotes. in ASP, to substitute data into a variable, you must use double quotation marks. In addition, you can only use single quotation marks when using the quotation marks, double quotation marks are not allowed. if double quotation marks are used, they are treated as the ending character of the previous quotation marks.

But in PHP, there is no such restriction. Replace the value with a variable. single quotation marks and double quotation marks can be used, but must be used in pairs.

In PHP, if it is only used for text data that does not contain variables, there is no difference between single quotation marks and double quotation marks. However, to use variables, single quotes and double quotation marks are different.

In PHP, variables can be directly substituted into double quotes without definition or other symbols,

For example:

 
 
  1. $ B = "cat"; $ a = "This is a $ B"; // Display This is a cat

It cannot be enclosed in single quotes. if

 
 
  1. $ A = 'This is a $ B '; // This is a $ B.

There is also a difference between single quotes and double quotes in terms of operation efficiency. Generally, the speed of single quotes is faster, and double quotes are slower. The reason is that the double quotation marks must first look for variables in the statement, you do not need to use single quotes. Therefore, use single quotes if the variables are not entered in the statement. This is a habit of writing programs, always thinking about improving program efficiency.

If you want to convert the definition operation in the statement, you must use double quotation marks.

For example, to define a single quotation mark, write it as follows:

 
 
  1. $a = ‘He’s name is Tom.’ ;

The program will display the He's name is Tom. intact, if written as follows:

 
 
  1. $a = “He’s name is Tom.”;

The program will display: He's name is Tom.

Single quotation marks are a stumbling block to SQL statements. text data in SQL statements must be included in single quotation marks. Therefore, if single quotation marks appear in the data, the database considers the data to end, the subsequent data will be considered as other components of the SQL statement, and of course an error will be reported during database query. Therefore, the text data written into the SQL statement must use addslashes () the function is converted to single quotes, and stripslashes () is used for data reading.


Single quotation marks and double quotation marks (") of" comment "play an important role. They are also used in PHP. Compared with ASP, PHP quotes are easier to use. in ASP, data should be replaced...

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.