What is the difference between single-quoted and double-quoted strings in PHP?
PHP strings can be specified not just in two ways, but in four ways.
- Single quoted strings will display things almost completely "as is." Variables and most escape sequences will not be interpreted. The exception is that to display a literal single quote, you can escape it with a back slash
\'
, and to display a back slash, you can escape it with another backslash\\
(So yes, even single quoted strings are parsed). - Double quote strings will display a host of escaped characters (including some regexes), and variables in the strings will be evaluated. An important point here is that you can use curly braces to isolate the name of the variable you want evaluated. For example let's say you have the variable
$type
and you want toecho "The $types are"
. That will look for the variable$types
. To get around this useecho "The {$type}s are"
You can put the left brace before or after the dollar sign. Take a look at string parsing to see how to use array variables and such. - Heredoc string syntax works like double quoted strings. It starts with
<<<
. After this operator, an identifier is provided, then a newline. The string itself follows, and then the same identifier again to close the quotation. You don't need to escape quotes in this syntax. - Nowdoc (since PHP 5.3.0) string syntax works essentially like single quoted strings. The difference is that not even single quotes or backslashes have to be escaped. A nowdoc is identified with the same
<<<
sequence used for heredocs, but the identifier which follows is enclosed in single quotes, e.g.<<<'EOT'
. No parsing is done in nowdoc.
Notes:
Single quotes inside of single quotes and double quotes inside of double quotes must be escaped:
$string = 'He said "What\'s up?"';
$string = "He said \"What's up?\"";
Speed:
I would not put too much weight on single quotes being faster than double quotes. They probably are faster in certain situations. Here's an article explaining one manner in which single and double quotes are essentially equally fast since PHP 4.3 (Useless Optimizations
toward the bottom, section C
). Also, this benchmarks page has a single vs double quote comparison. Most of the comparisons are the same. There is one comparison where double quotes are slower than single quotes.
When should you use single or double quotes in PHP?
The only difference is that double quoted strings interpret embedded variables and a number of escape sequences, while single quoted strings do not. E.g.:
'This is $a \n string'
This is literally the string "This is $a \n string".
"This is $a \n string"
This is a string containing the value of variable $a
and a line break.
Use both as appropriate. If neither escape sequences nor variables are of interest to you, I'd default to single quoted strings; with one exception: if you need single quotes in the string, then double quotes are "cleaner". Compare:
'I don\'t care.'
"I don't care."
What are difference between single quoted and double quoted strings in PHP?
Double quoted strings get parsed by the PHP parser for variables and escaped characters like \n
$x = 10;
echo "$x dollars"; // Outputs: 10 dollars
Single quoted strings do not get parsed so:
$x = 10;
echo '$x dollars'; // Outputs: $x dollars
and to output 10 dollars
you would need to do string concatenation:
$x = 10;
echo $x.' dollars';
Parsing variables within strings uses more memory and takes more time than string concatenation. So single quotes are almost always the better way to go. Even though the difference is small.
PHP single quotes vs double quotes
One useful additional fact I may mention is speed - in case of double quotes ""
PHP will search every string for variables. This really is not an issue in general day to day programming, but if you have a specific large script where performance really matters, optimizing it may pay off a bit.
What is the difference between double quotes and single quotes in mysql query
I suspect that your plot string contains a single quote. To avoid the problem you should be escaping your string values using mysql_real_escape_string
, or (better) use parameterized queries.
Your "solution" of changing the single quote to a double quote may appear to work in this case, but it will fail if the plot string contains a double quote.
single quotes and double quotes in php?
The use of '
and "
for strings only changes when they are the outer container of the entire string - a different quote inside that is just treated as a plain character with no special meaning, therefore "'$var'"
uses the rules of "
(parsing variables), whereas '"$var"'
would literally output "$var"
as it uses the rules of '
(no parsing of variables).
Summary:
When you do "''"
or "\"\""
the quotes inside the string are not parsed by PHP and treated as literal characters, the contents of such quotes will have the same behaviour in or out of those quotes.
When should I use double or single quotes in JavaScript?
The most likely reason for use of single vs. double in different libraries is programmer preference and/or API consistency. Other than being consistent, use whichever best suits the string.
Using the other type of quote as a literal:
alert('Say "Hello"');
alert("Say 'Hello'");
This can get complicated:
alert("It's \"game\" time.");
alert('It\'s "game" time.');
Another option, new in ECMAScript 6, is template literals which use the backtick character:
alert(`Use "double" and 'single' quotes in the same string`);
alert(`Escape the \` back-tick character and the \${ dollar-brace sequence in a string`);
Template literals offer a clean syntax for: variable interpolation, multi-line strings, and more.
Note that JSON is formally specified to use double quotes, which may be worth considering depending on system requirements.
Difference Between Single and Double Quoted Strings in ActionScript
You can use either as delimiter for a string. They are however not interchangeable, i.e. you can't start a string with an apostrophe and end it with a quotation mark.
The only difference is which characters you need to escape. Inside a string delimited by quotation marks you need to escape quotation marks but not apostrophes, and vice versa.
To put the text He said "It's all right" and laughed.
in a string you can use:
"He said \"It's all right\" and laughed."
or:
'He said "It\'s all right" and laughed.'
php: string is double-quoted, not single-quoted. How to fix?
It works just fine. Its your testing that is causing the issue.
Do this instead
$_POST['source'] = '$abc$$$';
$str = $_POST['source'];
preg_match('/^([^\w]*)(\w+.*\w+)?([^\w]*)$/iu', $str, $matches);
$parts = array_slice($matches, 1);
print_r($parts);
Result
Array
(
[0] => $
[1] => abc
[2] => $$$
)
Additional Example
Look at this simple test. I load a variable using a single quoted string and var_dump shows it to me as a double quoted string.
$test = '$var';
var_dump($test);
Result
string(4) "$var"
Its just how the dump functions decide to show a string, the string has not been changed
Its only if within PHP itself you use a $
when creating a string variable using a double quoted string literal that this variable expansion takes place. This is something PHP does and nothing intrinsic about double quoted literals.
So this
$test = "$var";
Will generate an error if $var
does not exists, or expand it if it does. But only because it being done in PHP code.
Related Topics
Php Regex Find Text Between Custom Added HTML Tags
Disable Xampp Redirect Http to Https
Patch and Put Request Does Not Working With Form-Data
Convert Image to Base 64 String With Laravel
How to Update Multiple Select Box Values in PHP MySQL
Assign Jquery Value to PHP Variable Inside Ajax Sussess
How to Sends Two Values in Select Dropdown Using PHP
Fatal Error: Call to Undefined Function Mysqli_Connect()
Laravel 6: Call to Undefined Method App\\User::Createtoken()
Nginx Error Recv() Failed (104: Connection Reset by Peer)
Group Array Data on One Column and Sum Data from Another Column
Laravel App Storage Images Failed to Load and Redirect to 404
Unset Post Variables After Form Submission
Laravel Drop Down Multiple Values from Db in Drop Down
Php Adding 15 Minutes to Time Value
Detect a Quotation in a String
How to Get Woocommerce Order Details
How to Prevent a User from Directly Accessing My HTML Page by Writing Url