Please support if you like my work by payment through upi: sinhamit@icici or payment by bank
account name: Amit Kumar Sinha,
account number: 2646728782
IFSC code: KKBK0005660
SWIFT: KKBKINBB
More About Strings in Category: PHP by amit
🕙 Posted on 2023-05-12 at 06:31:15 Read in Hindi ...
Using single quote and double quote inside a string
You have seen various features of string data-type. In this topic (page), we will learn more about functionality of string data-type, that is, how we can use single quotes and double quotes, inside a string, and also how we can use a variable or constant inside a string. In previous pages, you have seen that a string data-type literal can be created within a pair of single quotes, or a pair of double quotes. For example,
<?php $my_name = 'Amit'; // a string literal inside a pair of single quotes ?>
<?php $first_name = "Amit"; // another string literal inside a pair of double quotes ?>
Now, you can insert a single quote or a double quote, inside these string literals, with the help of escape sequences (also called as escaped characters). (These single quote or double quote is not in a pair, that is, each quote is individually treated inside the string literal.) A escape sequences is always prefixed with a \ (backslash key just above the Enter/
<?php $item1 = 'Amit\'s phone'; echo $item1; // or ?><br />
<?php $item2 = "Amit\"s phone"; echo $item2; // or ?><br />
<?php $item3 = 'Amit"s phone'; echo $item3; ?><br /><!-- using a double quote inside a pair of single quotes -->
<!-- (HTML comments outside PHP tags are not processed by PHP interpreter, but can be viewed in source-code by pressing Ctrl U keys) -->
<?php $item4 = "Amit's phone"; echo $item4; ?><br /><!-- using a single quote inside a pair of double quotes -->
<?php $item5 = 'Amit\'s phone number "can\'t" be displayed!'; echo $item5; ?><br /><!-- In this example, a pair of double quotes showing as quotation is enclosed within a pair of single quotes. Also, two single quotes (not in pair) are individually inserted with escape sequences. -->
\\ (use two backslashes to insert a backslash into the string literal), for example
<?php echo "You have deleted C:\\*.* all files!"; ?><br />
<?php echo 'You have deleted C:\\*.* all files!'; ?><br />
<?php echo 'You have deleted C:\*.* all files!'; // one backslash can also be output and displayed ?><br />
You have deleted C:\*.* all files!
You have deleted C:\*.* all files!
All other instances of backslash will be treated as a literal backslash: this means that the other escape sequences you might be used to, such as \r or \n, will be output literally as specified rather than having any special meaning.
In old days (that is, from 1995 to 2015), there were limited options to insert data into databases, and programmers had to struggle to script code accordingly. In JavaScript, developements were added since 2015 (ECMAScript 6 or ES6 or ES2015, and later updation). Similarly, various features and developements are added in PHP 5, PHP 7, and PHP 8. Interestingly, you will not find migration from older versions to PHP4 and PHP6 in official documentation. PHP4 and PHP6 had not been released by developers for general public.
Following pre-defined escape sequences can be used in a pair of double quotes, in PHP and will be interpreted as special character. The result can be viewed in source-code, by pressing the Ctrl U keys. The web-browser because of its design to display HTML structure, without line-break, tabs, blank spaces, etc. (except when the output is displayed in <pre> </pre> element).
<?php echo "You can also
insert line-break inside
a pair of single quote
or a pair of double quotes
with Enter
or Return
key in your keyboard. "; // line-break by Enter
key in a pair of double quotes ?>
<?php echo 'because of
web-browser\'s design to
display the result will
not be notice unless you
press Ctrl U
keys
simultaneously. '; // line-break by Enter
key in a pair of single quotes ?>
Caution
: Following escape sequences will not work properly in a pair of single quotes. As in single quoted strings, escaping any other character will result in the backslash being printed too.
Escape sequences | Description | Hexadecimal | ASCII |
---|---|---|---|
\n | linefeed (to insert a new line) | 0x0A | 10 |
\r | carriage return | 0x0D | 13 |
\t | horizontal tab (to insert a tab space) | 0x09 | 09 |
\v | vertical tab | 0x0B | 11 |
\e | escape | 0x1B | 27 |
\f | form feed | 0x0C | 12 |
\$ | dollar symbol | 0x24 | 36 |
\\ | backslash | 0x5C | 92 |
Fine difference between a pair of single quotes and a pair of double quotes
In above examples, you have seen various functionality of string literals. A string literal inside a pair of single quotes is static, that is, any variable or constant place inside it will not be expanded to its respective value. Whereas, a string literal inside a pair of double quotes is dynamic, that is, you can place escape sequences, variables or constants inside it, and these escape sequences, variables and constants can be expanded to their respective values. For example:
<?php
$my_name = 'Amit';
$greeting = "Hello $my_name";
echo $greeting; // outputs Hello Amit?>
This above example can also be written with the help of curly braces, when there may not be any space between these two words (variable name and string literal).
<?php $my_name = 'Amit'; ?>
<?php echo "Hello {$my_name}"; // Hello Amit ?><br />
<?php echo "Hello ${my_name}"; // Hello Amit ?><br />
<?php echo "Hello{$my_name}"; // HelloAmit ?><br />
<?php echo "Hello${my_name}"; // HelloAmit ?><br />
<?php echo 'Hello {$my_name}'; // Hello {$my_name} ?>
The last line of above examples does not expand the variable name $my_name to its value, because it is placed inside a pair of single quotes. No matter how you place the variable as explained above, inside the pair of single quote, it will not be expanded.
Leave a Comment:
Amit Sinha March 2nd, 2023 at 9:30 PM
😃 😄 😁 😆 😅 😂 😉 😊 😇 😍 😘 😚 😋 😜 😝 😶 😏 😒 😌 😔 😪 😷 😵 😎 😲 😳 😨 😰 😥 😢 😭 😱 😖 😣 😞 😓 😩 😫 😤
Ribhu March 3rd, 2023 at 9:30 PM
🐵 🐒 🐶 🐩 🐺 🐱 🐯 🐅 🐆 🐴 🐎 🐮 🐂 🐃 🐄 🐷 🐖 🐗 🐽 🐏 🐑 🐐 🐪 🐫 🐘 🐭 🐁 🐀 🐹 🐰 🐇