Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Strings3:43 with Alena Holligan
A string is a series of characters, typically specified within quotations. Explore the differences between single and double quoted strings and using other variables within a string.
Learn more about Strings
Single Quoted Strings are the simplest way to specify a string. Enclose the string in single quotes (the character ').
To specify a literal single quote, escape it with a backslash (\'). To specify a literal backslash, double it (\\). 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.
Note: variables and escape sequences for special characters will not be expanded when they occur in single quoted strings.
Double Quoted Strings are enclosed in double-quotes ("), PHP will interpret more escape sequences for special characters. As in single quoted strings, escaping any other character will result in the backslash being printed too.
The most important feature of double-quoted strings is the fact that variable names will be expanded.
Although quotes are the primary way to specify a string, there is another option: the heredoc syntax (<<<). After this operator, an identifier is provided, then a newline. The string itself follows, and then the same identifier again to close the quotation.
The closing identifier must begin in the first column of the line. Also, the identifier must follow the same naming rules as any other label in PHP: it must contain only alphanumeric characters and underscores, and must start with a non-digit character or underscore.
Heredoc syntax allows people to easily write large amounts of text from within PHP, but without the need to constantly escape things. Put simply, it allows you to define your own string limiter so that you can make it something other than a double or single quote. So, for example, we could use the string "EOT" (end of text) for our delimiter, meaning that we can use double quotes and single quotes freely within the body of the text - the string only ends when we type EOT.
$name = 'Alena'; $myString = <<<EOT Example of string spanning multiple lines using spaces and including a $name variable using heredoc syntax. EOT;
Example of string spanning multiple lines using spaces and including a Alena variable using heredoc syntax.
Nowdocs are to single-quoted strings what heredocs are to double-quoted strings. A nowdoc is specified similarly to a heredoc, but no parsing is done inside a nowdoc. The construct is ideal for embedding PHP code or other large blocks of text without the need for escaping.
A nowdoc is identified with the same <<< sequence used for heredocs, but the identifier which follows is enclosed in single quotes, e.g. <<<'EOT'. All the rules for heredoc identifiers also apply to nowdoc identifiers, especially those regarding the appearance of the closing identifier.
$name = 'Alena'; $str = <<<'EOT' Example of string spanning multiple lines using spaces and including a $name variable using nowdoc syntax. EOT;
Example of string spanning multiple lines using spaces and including a $name variable using nowdoc syntax.
In the last section we started exploring the two basic pieces of programming,
storage and retrieval of data, and the logic that tells what and
when to do something with that data.
You were introduced to variables and we use integers and float variables
along with arithmetic and assignment operators, to create a unit converter.
In this section we'll continue to expand up on these skills as we explore
more data types and logic to create a daily exercise program.
We'll store each exercise in a string variable,
then we'll use conditionals to control which exercise is displayed.
Let's start by looking at the string variable.
A string is a series of characters typically specified with quotations.
You can use either single or double quotes to create a string but
they work differently.
Single quoted strings, for the most part represent each individual character
exactly as it is stored in the string.
Double quoted strings on the other hand let you place variables inside the string.
PHP will add the value of the variable or expand the variable within the string.
This will make a lot more sense when you see it in action.
So let's take a look at an example of storing and retrieving string variables.
We're starting a new program, so let's create a new file and name it strings.php.
Again, we need to start this file using the opening and closing PHP tags.
Then we're ready to add our variables.
We'll name this variable, string_one.
Again, we'll use the assignment operator,
a single equals sign followed by the value that we want to store.
In this case, it will be a string surrounded by single quotes.
For string one, let's set the sequel to hello world.
Now that the variable string one contains the value hello world.
Let's retrieve that value using echo string one
and now we can run the script in the console.
Great, you've written your second hello world script.
As I mentioned before, the primary difference between single and
double quoted strings is whether variable names will be expanded.
Let's add a variable called name we'll set this equal to our name.
I'm gonna use my name, Alina.
You can use your name.
Then instead of saying hello world, let's say hello Alina using the name.
Save the file and run the script again.
Notice that the actual variable name is displayed instead of its value.
That's because variables are not expanded within single quotes.
Let's change these to double quotes and run the script again.
Now it works just like I want and shows hello Alena.
By using double quotes the PHP interpreter expands the variable
showing us the value of the name variable instead of the variable name itself.
If you want the value of a variable within your string
make sure that you use double quotes.
There are a few other differences in the way that PHP interprets characters in
a In the next video will explore the use of escape sequences.
You need to sign up for Treehouse in order to download course files.Sign up