Home Backend Development PHP Tutorial The difference between single and double quotes in php_PHP tutorial

The difference between single and double quotes in php_PHP tutorial

Jul 13, 2016 am 10:27 AM
html php code the difference pair Field quotation marks translater explain output

” ” Fields enclosed in double quotes will be interpreted by the compiler and then output as HTML code.

  ‘ ‘ The words in the single quotes are not interpreted and are output directly.

It can be seen from the literal meaning that single quotes are faster than double quotes.

For example:

$abc=’my name is tome’;

echo $abc //The result is: my name is tom

echo ‘$abc’ //The result is

abc

echo “$abc” //The result is: my name is tom

Especially when using MYSQL statements, the usage of double quotes and single quotes can be confusing to novices. Here, I will give an example to illustrate.

Assume that constants are used in the query conditions, for example:

Select * from abc_table where user_name=’abc’;

The SQL statement can be written as:

 SQLstr = “select * from abc_table where user _name= ‘abc’”;

Assume that variables are used in the query conditions, for example:

 $user_name = $_REQUEST['user_name']; //String variable

or

 $user=array ("name"=> $_REQUEST['user_name‘,"age"=>$_REQUEST['age'];//Array variable

The SQL statement can be written as:

 SQLstr = “select * from abc_table where user_name = ‘ . $user_name . ” ‘ “;

 SQLstr = “select * from abc_table where user_name = ‘ ” . $user["name"] . ” ‘ “;

Compare:

 SQLstr=”select * from abc_table where user_name = ‘ abc ‘ ” ;

 SQLstr=”select * from abc_table where user_name =’ ” . $user _name . ” ‘ “;

 SQLstr=”select * from abc_table where user_name =’ ” . $user["name"] . ” ‘ “;

SQLstr can be decomposed into the following 3 parts:

 1: “select * from table where user_name = ‘ ” //Fixed SQL statement

 2: $user //Variable

 3:” ‘ ”

Use "." to connect the strings 1, 2, and 3

====

1. First think about how many types of words (actually they should be called symbols) in PHP.

1. Keywords and functions of PHP and mysql. For example, echo, print, mysql_connect, etc. These must be without quotation marks.

2. Constants. Novices may not use them much. The advantage of constants is that they are global and penetrate functions. They are also faster, but novices can ignore constants for the time being.

3. Variables. Those with "$" in front are variables. You can set a "value" for the variable, such as a string of characters, a number, a logical (true/false) value, etc. It can also represent a set of values ​​( Array, object, etc.)

4. Value. Usually you need to set values ​​for constants and variables. In the assignment statement $a='abc', the 'abc' on the right is the value.

5. Function parameters (in brackets). They can be constants, variables, and values.

The relationship between variables (constants) and values ​​is as follows.

"Color" and "red",

"Length" and 100,

"Date" and October 25, 2007 "

2. When to use PHP quotes

In fact, only the fourth item "value" needs to use quotation marks, and only the value in the function needs to use quotation marks. And only the string (date value can be regarded as a string) content needs to use quotation marks. Numbers (available or not) , true or false (cannot be used) exception.

Example

3. The difference between single quotes and double quotes

Under normal circumstances, the two are common. However, double quotes internal variables will be parsed, but single quotes will not be parsed.

Example

So if there is only a pure string inside, use single quotes (faster). When there are other things inside (such as variables), it is better to use double quotes.

IV. What to do if PHP quotes appear inside the string - about escaping.

For example, we want to output: I "am a genius

At this time, escaping must be used. Escape is to convert the originally useful symbols into meaningless characters.

This is normal, because the number converts any characters following it into meaningless symbols. In this case, the PHP parser does not treat the quotation marks after the number as quotation marks at all.

Similarly, you can also escape special symbols such as semicolons and $ symbols.

5. String concatenation.

This is a troublesome problem. Generally speaking, variable values ​​can be directly included in double quotes. In addition, the "." character is used to superimpose strings.

In complex situations, you can use braces to include it. PHP will know that this is a complete thing, and the quotation marks inside will not affect the relationship between the quotation marks outside.

Mixing with HTML is also very simple. It is best to develop the habit of using double quotes in HTML and single quotes in PHP. This makes it easier to copy large sections of HTML code, just add single quotes at the beginning and end. A correct string. Even with hundreds of lines of HTML code, you don’t have to worry about confusing PHP quotes.

Summarize the principles of using quotes in PHP

1. Use quotation marks for string values ​​

2. Use single quotes as much as possible in PHP, and use double quotes in all HTML codes

3. When including variables, use double quotes to simplify the operation

4. In complex cases, use braces

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/815012.htmlTechArticleFields in double quotes will be interpreted by the compiler and then output as HTML code. The words inside the single quotes are not interpreted and are output directly. As you can see from the literal meaning, single quotes...
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP's Purpose: Building Dynamic Websites PHP's Purpose: Building Dynamic Websites Apr 15, 2025 am 12:18 AM

PHP is used to build dynamic websites, and its core functions include: 1. Generate dynamic content and generate web pages in real time by connecting with the database; 2. Process user interaction and form submissions, verify inputs and respond to operations; 3. Manage sessions and user authentication to provide a personalized experience; 4. Optimize performance and follow best practices to improve website efficiency and security.

PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python: Code Examples and Comparison PHP and Python: Code Examples and Comparison Apr 15, 2025 am 12:07 AM

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

Choosing Between PHP and Python: A Guide Choosing Between PHP and Python: A Guide Apr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

HTML: The Structure, CSS: The Style, JavaScript: The Behavior HTML: The Structure, CSS: The Style, JavaScript: The Behavior Apr 18, 2025 am 12:09 AM

The roles of HTML, CSS and JavaScript in web development are: 1. HTML defines the web page structure, 2. CSS controls the web page style, and 3. JavaScript adds dynamic behavior. Together, they build the framework, aesthetics and interactivity of modern websites.

Why Use PHP? Advantages and Benefits Explained Why Use PHP? Advantages and Benefits Explained Apr 16, 2025 am 12:16 AM

The core benefits of PHP include ease of learning, strong web development support, rich libraries and frameworks, high performance and scalability, cross-platform compatibility, and cost-effectiveness. 1) Easy to learn and use, suitable for beginners; 2) Good integration with web servers and supports multiple databases; 3) Have powerful frameworks such as Laravel; 4) High performance can be achieved through optimization; 5) Support multiple operating systems; 6) Open source to reduce development costs.

See all articles