In PHP, there are differences between single quotes and double quotes in string representation: variable interpolation: Single quotes do not perform variable interpolation, while double quotes do. Special escape characters: Single quotes are not supported, double quotes are supported. String concatenation: Single quotes only concatenate when adjacent, double quotes always concatenate.
The difference between single quotes and double quotes in PHP
In PHP, strings can be represented by single quotes or enclosed in double quotes. There are some key differences in syntax between these two quote types:
1. Variable interpolation
For example:
<code class="php">$name = "John Doe"; echo 'My name is $name.'; // 输出:My name is $name. echo "My name is $name."; // 输出:My name is John Doe.</code>
2. Special escape characters
For example:
<code class="php">echo 'This is a new line: \n'; // 输出:This is a new line: \n echo "This is a new line: \n"; // 输出:This is a new line: <换行></code>
3. String concatenation
For example:
<code class="php">$firstName = 'John'; $lastName = 'Doe'; echo $firstName' '$lastName; // 输出:John $lastName echo $firstName." ".$lastName; // 输出:John Doe</code>
Summary
In general, single quotes are used for characters that need to be output as is string or does not support special escape characters. Double quotes are used when variable interpolation is required or when special escape characters are used.
The above is the detailed content of In php, what is the difference between strings surrounded by single quotes and double quotes?. For more information, please follow other related articles on the PHP Chinese website!