{"id":838,"date":"2017-03-18T18:24:52","date_gmt":"2017-03-18T12:54:52","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=838"},"modified":"2017-03-29T13:17:08","modified_gmt":"2017-03-29T07:47:08","slug":"difference-single-quoted-double-quoted-strings-php","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/difference-single-quoted-double-quoted-strings-php\/","title":{"rendered":"PHP &#8211; What is the difference between single-quoted and double-quoted strings in PHP"},"content":{"rendered":"<h4 id=\"single-quoted-strings\"><span style=\"color: #ff6600;\"><strong>Single Quoted Strings:<\/strong><\/span><\/h4>\n<ul>\n<li>Single quoted strings are the easiest way to specify string. Single quote is used to specify string when we want the string to be exactly as it is written.<\/li>\n<li>To specify a literal single quote or backslash, escape it with a backslash (\\).<\/li>\n<li>All\u00a0other escape sequences\u00a0like\u00a0\\r\u00a0or\u00a0\\n, will be output literally as specified rather than having any special meaning.<\/li>\n<\/ul>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">Php Code<\/span> <\/div> <pre class=\"language-php code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-php code-embed-code\">echo &#039;This is \\&#039;test\\&#039; string&#039;;<br\/>\/\/Output: This is &#039;test&#039; string<\/code><\/pre> <\/div>\n<h4 id=\"example\"><span style=\"color: #808000;\"><strong>Example:<\/strong><\/span><\/h4>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">Php Code<\/span> <\/div> <pre class=\"language-php code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-php code-embed-code\">\/\/ Outputs: It is a simple string.<br\/>echo &#039;It is a simple string.&#039;;<br\/> <br\/>\/\/ Outputs: It&#039;ll be a simple string.<br\/>echo &#039;It\\&#039;ll be a simple string.&#039;;<br\/> <br\/>$string = &#039;string&#039;;<br\/>echo &#039;This is a simple $string.&#039;;<br\/>\/\/Output: This is a simple $string.<br\/> <br\/>\/\/ Outputs: This is a simple \\n string.<br\/>echo &#039;This is a simple \\n string.&#039;;<\/code><\/pre> <\/div>\n<ul>\n<li>When string is specified in single quotes , then PHP will not evaluate it or interpret escape characters except single quote with backslash (\u2018) and backslash(\\) which has to be escaped.<\/li>\n<li>If you want a variable to be used with a single quoted string, the you need to use the\u00a0period to separate code in string because PHP will not evaluate the variable if used directly within the single quoted string.<\/li>\n<\/ul>\n[ad type=&#8221;banner&#8221;]\n<h4 id=\"double-quoted-strings\"><span style=\"color: #800080;\"><strong>Double Quoted Strings:<\/strong><\/span><\/h4>\n<p><strong>If we use double quoted string, then following things can be done:<\/strong><\/p>\n<ul>\n<li>Double quotes force PHP to evaluate the string.<\/li>\n<li>Escape sequences are interpreted.<\/li>\n<li>Any variable will be replaced by their value.<\/li>\n<\/ul>\n<p><strong>Example:<\/strong><\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">Php Code<\/span> <\/div> <pre class=\"language-php code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-php code-embed-code\">\/\/ Outputs: It is a simple string.<br\/>echo &quot;It is a simple string.&quot;;<br\/> <br\/>\/\/ Outputs: It&#039;ll be a simple string.<br\/>echo &quot;It&#039;ll be a simple string.&quot;;<br\/> <br\/>$string = &#039;string&#039;;<br\/>echo &quot;This is a simple string.&quot;;<br\/>\/\/Output: This is a simple $string.<br\/> <br\/>\/\/ Outputs: This is a simple string.<br\/>echo &quot;This is a simple \\n string.&quot;;<\/code><\/pre> <\/div>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">Php Code<\/span> <\/div> <pre class=\"language-php code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-php code-embed-code\">$count = 1;<br\/>echo &quot;The count is $count&quot;;<br\/>\/\/Output: The count is 1<\/code><\/pre> <\/div>\n<ul>\n<li>In Double quote strings will be printed after interpretation.<\/li>\n<li>So it is better to use single quotes unless you need to interpret the variable value.<\/li>\n<\/ul>\n<h4 id=\"advantage\"><span style=\"color: #808000;\"><strong>Advantage:<\/strong><\/span><\/h4>\n<p>Using double quotes is you don\u2019t need to use concatenation(.) operator for strings (Like\u00a0\u2018My blog is\u2019.$blog).<\/p>\n<p><span style=\"color: #ff6600;\"><strong>Difference between single-quoted and double-quoted strings:<\/strong><\/span><\/p>\n<table width=\"853\">\n<tbody>\n<tr>\n<td width=\"427\"><strong>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Single Quoted Strings<\/strong><\/td>\n<td width=\"427\"><strong>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Double Quoted Strings<\/strong><\/td>\n<\/tr>\n<tr>\n<td width=\"427\">A single-quoted string does not have variables within it interpreted.<\/td>\n<td width=\"427\">A double-quoted string does.<\/td>\n<\/tr>\n<tr>\n<td width=\"427\">A single-quoted string can contain unescaped quotation marks.<\/td>\n<td width=\"427\">A double-quoted string can contain apostrophes without backslashes<\/td>\n<\/tr>\n<tr>\n<td width=\"427\">Single-quoted strings are faster at runtime because they do not need to be parsed.<\/td>\n<td width=\"427\">double-quoted string are than single-quoted strings.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n[ad type=&#8221;banner&#8221;]\n<h4 id=\"which-option-should-be-used\"><span style=\"color: #800080;\"><strong>Which option should be used?<\/strong><\/span><\/h4>\n<p>Since,\u00a0double quotes force PHP to evaluate the string\u00a0 (even though it might not be needed) while single quotes not. So, using single quotes is faster than using double quotes.<\/p>\n<p>So, use single quotes (\u2018 \u2018) for string unless we need the double quotes (\u201d \u201c). \u00a0Also, parsing variables between strings takes more memory than concatenation.<\/p>\n<h4 id=\"variable-parsing-in-double-quoted-strings\"><span style=\"color: #ff6600;\"><strong>Variable parsing in double quoted strings:<\/strong><\/span><\/h4>\n<ul>\n<li>When a string is specified in double quotes, variables are parsed within it.<\/li>\n<li>There are two types of syntax of using variables in double quoted strings:<\/li>\n<li>simple one (directly as $string)<br \/>\ncomplex one (with curly braces as {$string})<\/li>\n<li>It provides a way to embed a variable, an array value, or an object property in a string with a minimum of effort.<\/li>\n<\/ul>\n<h4 id=\"simple-syntax\"><span style=\"color: #808000;\"><strong>Simple syntax:<\/strong><\/span><\/h4>\n<p>If a dollar sign ($) is encountered, the parser will greedily take as many tokens as possible to form a valid variable name.<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">Php Code<\/span> <\/div> <pre class=\"language-php code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-php code-embed-code\">$car = &quot;car&quot;;<br\/> <br\/>echo &quot;I have a $car.&quot;;<br\/> <br\/>\/\/ Invalid. &quot;s&quot; is a valid character for a variable name, but the variable is $car.<br\/>echo &quot;I have many $cars.&quot;;<br\/> <br\/>$colors = [&#039;red&#039;,&#039;black&#039;.&#039;white&#039;];<br\/>echo &quot;I have a $colors[0] car.&quot;;<br\/> <br\/>class Car {<br\/>    public $name = &quot;car&quot;;<br\/>}<br\/>$car = new Car();<br\/>echo &quot;I have a $car-&gt;name.&quot;;<\/code><\/pre> <\/div>\n<h4 id=\"complex-syntax\"><span style=\"color: #800080;\"><strong>Complex syntax:<\/strong><\/span><\/h4>\n<p>The complex syntax can be recognized by the curly braces surrounding the variable name. Enclose the variable name in curly braces to explicitly specify the end of the name.<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">Php Code<\/span> <\/div> <pre class=\"language-php code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-php code-embed-code\">$car = &quot;car&quot;;<br\/> <br\/>echo &quot;I have a {$car}.&quot;;<br\/> <br\/>\/\/ Invalid. &quot;s&quot; is a valid character for a variable name, but the variable is $car.<br\/>echo &quot;I have many $cars.&quot;;<br\/> <br\/>$colors = [&#039;red&#039;,&#039;black&#039;.&#039;white&#039;];<br\/>echo &quot;I have a {$colors[0]} car.&quot;;<br\/> <br\/>class Car {<br\/>    public $name = &quot;car&quot;;<br\/>}<br\/>$car = new Car();<br\/>echo &quot;I have a {$car-&gt;name}.&quot;;<\/code><\/pre> <\/div>\n[ad type=&#8221;banner&#8221;]\n","protected":false},"excerpt":{"rendered":"<p>Single Quoted Strings: Single quoted strings are the easiest way to specify string. Single quote is used to specify string when we want the string to be exactly as it is written. To specify a literal single quote or backslash, escape it with a backslash (\\). All\u00a0other escape sequences\u00a0like\u00a0\\r\u00a0or\u00a0\\n, will be output literally as specified [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[25],"tags":[1580,1585,1583,1586,1579,1584,1582,1581],"class_list":["post-838","post","type-post","status-publish","format-standard","hentry","category-php","tag-difference-between-single-inverted-comma-and-double-inverted-commas","tag-php-double-quotes-inside-double-quotes","tag-php-escape-double-quotes","tag-php-heredoc","tag-php-quotes-escape","tag-php-single-quote-in-string","tag-php-single-quotes-inside-double-quotes","tag-php-single-vs-double-quotes-performance"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/838","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=838"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/838\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=838"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=838"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=838"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}