So if you are reading this and the first thing you are thinking of is, “What’s the definition for the word string?” then you are in the wrong place and you should go now. Not because I think you are dumb, but more that you are going to fall asleep at what I am going to say so you might as well go.
Now the rest of you that are here then lets dive into this a little deeper. If you code in any manner pretty much you are going to understand what I mean by “string”, but not every coder truly understands how to use them in PHP. Now you can’t really go wrong in how you decide to use them, but you can make life easier. Trust me as a coder I see so many lines everyday that shaving off a few seconds here and there can make a huge difference.
First of all there are 4 ways to define a string.
- 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 what to?echo "The $types are"
?That will look for the variable?$types
. To get around this use?echo "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.
Don’t worry you are pretty much only going to most of the time use the top two ways. Single and Double Quotes. But here is where many coders do not know how they work. On easy way to tell them apart is remember that single quotes are ‘as is’ so what you see is what you get. Here is an example
‘This is a sentence with a single quote’
Now lets add a variable to the mix
echo 'This is a sentence with a single $quote';
would out put as
This is a sentence with a single $quote
Remember that single quotes are as is, so what you see is what you get. Variables placed in single quotes display as text. If you want to have a variable display in your string use double quotes.
So
$quotes = 'Winner'; echo "This is a sentence in double $quotes";
Would out put as
This is a sentence in double Winner
See 🙂