String type data is collection of any number of characters or bytes (In computer memory, different combination of eight bits, 0 or 1, makes a byte and express characters) enclosed with string notation. These characters include letters, numbers, symbols, spaces, and variables.
Creating strings
There are four ways to create strings in PHP-
- Single quote
- Double quote
- Heredoc syntax
- Nowdoc syntax
Single quote
- In this method, characters are enclosed with single quotation mark (‘).
- Contents inside the single quotes are displayed as it is.
- Variables inside the single quotes are not interpreted.
- Special characters insides don’t express the meanings.
Example
<pre> <?php $month_quantity=12; echo ‘There are $month_quantity months. \n January is the first one.'; ?> </pre>
Output:
There are $month_quantity months. \n January is the first one.
In the output above, the value of the variable $ today is not printed and the special character new line \n has no effect on output.
Double quote
- It is the most commonly used quote to express string.
- Here characters are enclosed with double quotation marks (” “).
- PHP interpreter interprets variables and special characters inside double quotes. In the following example, the above example is re-written using double quotes.
Example
<pre> <?php $month_quantity =12; echo “There are $month_quantity months. \nJanuary is the first one.” ?> </pre>
Output:
There are 12 months.
January is the first one.
- An empty quote (single/double) is considered as a string. In the following example variable $var contains an empty string.
<?php $var = “”; ?>
- If a number is enclosed by quote, it becomes a string. For example-
<?php $days = "365" // Here "365" is a string ?>
Don’t mix single quote and double quote in a string pair-
If a string starts with a single quote, then it must end with a single quote. Similarly, if a string starts with a double quote, it must end with a double quote. If single is mixed with double quote in a string, PHP will produce a syntax error.
<?php echo ‘This string is surrounded by single quotes only.’; //It is correct; echo “This string is surrounded by double quotes only.”; //It is correct; echo ‘This string is surrounded by a single and a double quotes.”; //It will display a syntax error; ?>
How to display single quote and double quote in a string?
Sometimes you’ll need to use single quote in your string (ex. John’s blog). Similarly, you may need to display double quote in a string too. There are 2 ways to do these-
- To display a single quote (apostrophe), the string could be within double quotation makes. Like the following example-
<?php echo “It is John’s blog.”; // Output: It is John’s blog. ?>
Similarly, to display double quote, the string could be surrounded by single quotes, Check the following example-
<?php echo ‘John said “This is my car”.’; // Output: John said " This is my car ". ?>
- If you want to display a double quote within a double quoted string, precede the middle double with a backslash. Check the following example-
<?php echo “John said \“This is my car\”.”; // Output: John said "This is my car". ?>
You can apply the same rule in a single quoted string too-
<?php echo ‘John said \’This is my car\’.’; // Output: John said "This is my car". ?>
Heredoc syntax
- The third way of creating string is to use here-document or in short heredoc syntax.
- When large amount of texts need to be displayed, then heredoc is the choice.
- It works similarly to double quotes except it doesn’t require escaping quotes, but the escaping codes in the table above can be used.
Rules of using heredoc syntax
- Heredoc syntax begins with three less than signs (<<<) followed by a user defined name. The name can be any combination of letters, numbers, underscores but the first character must be a letter or an underscore.
- The string begins in the next line and goes as long as it requires
- After the string, the same exact name that is defined after the (<<<) signs in the first line should be placed in the next line. Nothing can be added in this last line except a semicolon after the name and it is optional.
Example:
<pre> <?php echo <<<EOD This is first line. \nThis is second line.; EOD; ?> <pre>
Output:
This is first line.
This is second line.
- It’s better to code the name of heredoc all caps.
- The most common used heredoc names are EOD, EOT
Nowdoc syntax
- If heredoc syntax works similar to double quote, then nowdoc syntax works similar to single quote.
- Like single quote, no variable inside the nowdoc is interpreted.
Rules of using nowdoc syntax
Nowdoc supports all the syntax rules of heredoc except the starting name; it must be enclosed by single quotes.
Example:
<pre> <?php echo <<<’EOD’ This is first line. \nThis is second line.; EOD; ?> <pre>
Output:
This is first line. \nThis is second line.
What is escaping character?
You’ve seen to display a quote how backslash is used. This is known as escape the character to the string. This tells PHP interpreter to print the character as it is. When you’re escaping a quotation mark, PHP interpreter doesn’t consider it as the beginning/ending of the statement.
Which characters can be escaped?
- In a single quoted string, PHP only escapes single quote (‘) and backslash (\).
- In a double quoted string, PHP escapes double quote (“) and backslash (\).
The following few sequences support different strings format-Sequence Meaning Support \’ Single quote Only single quote \” Double quote Only double quote \\ Backslash All strings but nowdoc
- Escaping other characters in double quoted string and heredoc string express special meaning.
- All the following escape sequences have special meaning and can be used in only double quoted and heredoc strings –
Sequence Meaning \n New line \r Carriage return \t Horizontal tab \v Vertical tab \e Escape \f Form feed \[0-7]{1,3} Octal value \x[0-9A-Fa-f]{1,2} Hexadecimal value Example
Check the example-<pre> <?php echo "This is first line \nThis is second line."; ?> <pre>
Output-
This is first line
This is second line.[To show the output of the escape sequences, HTML tag <pre> is used in the previous examples.]
Variable parsing
When variable is included inside double quoted string or heredoc string, the variable is parsed or interpreted. There are 2 ways to include variables inside double quoted string or heredoc string.
- Simple syntax and
- Complex syntax
Simple syntax
- In this way, a variable is simply put inside the string.
- When php parser finds the $ sign, it treats it as the starting of a variable.
- Variables that represent scalar type, array, or object type data are parsed.
<?php // Parsing scalar type data $today = “Tuesday”; echo “This is $today today.”; // output: It is Tuesday today. // Parsing array type data $days=[“Monday”, “Tuesday”, “Wednesday”, “Thursday”, “Friday”, “Saturday”, “Sunday”]; echo “This is $day[1] today.”; // This is Tuesday today. // Parsing object type data class days{ public $today = “Tuesday”; } $days = new days(); echo “This is $days->today today.”; // This is Tuesday today. ?>
Complex Syntax
- Here variable is wrapped with { and }.
- When php parser finds the {$ sign, it treats it as the starting of a variable. Please make sure that { immediately follows $ in the syntax.
- Variables that represent scalar type, array, or object type data are parsed.
<?php // Parsing scalar type data $today = “Tuesday”; echo “This is {$today} today.”; // output: It is Tuesday today. // Parsing array type data $days=[“Monday”, “Tuesday”, “Wednesday”, “Thursday”, “Friday”, “Saturday”, “Sunday”]; echo “This is {$day[1]} today.”; // This is Tuesday today. // Parsing object type data class days{ public $today = “Tuesday”; } $days = new days(); echo “This is {$days->today} today.”; // This is Tuesday today. ?>
Curly braces makes it very clear that which one is variable and which parts are static in string.
Next Lesson: PHP Arrays ›› |