Sign In | Subscribe
Start learning today, and be successful in your academic & professional career. Start Today!
Loading video...
This is a quick preview of the lesson. For full access, please Log In or Sign up.
For more information, please see full course syllabus of Introduction to PHP
  • Discussion

  • Study Guides

  • Download Lecture Slides

  • Table of Contents

  • Transcription

  • Related Services

Bookmark and Share
Lecture Comments (8)

4 answers

Last reply by: Kitt Parker
Mon Dec 29, 2014 8:57 PM

Post by samuel rodriguez on January 28, 2013

can anybody please write an example using the /n and /$ i cant seem to make them work.

1 answer

Last reply by: Matthew M.
Tue Feb 7, 2012 2:24 AM

Post by Jesus Fernandez on December 16, 2011

Thank you. Love the php lessons.

String Data Type

  • The string data type is for representing a series, or 'string', of characters.
  • String literals can be specified in four ways:
    • single quotes
    • double quotes
    • heredoc syntax
    • nowdoc syntax
  • Escape sequences are specific character sequences used within string literals to represent special characters, such as tabs and newlines.
  • Single-quoted string literals do not allow any escape sequences except those for the single quote and the backslash.
  • Double-quoted string literals allow for the use of more escape sequences than single-quoted literals and also allow for variable interpolation.
  • Variable interpolation is the process where variable names in string literals are replaced by the variable's current value.
  • Additional Resources:

String Data Type

Lecture Slides are screen-captured images of important points in the lecture. Students can download and print out these lecture slide images to do practice problems as well as take notes while watching the lecture.

  • Intro 0:00
  • Lesson Overview 0:12
    • Lesson Overview
  • String Data Type 0:29
    • String Data Type
    • Specifying String Literals
  • Single-Quoted Strings 1:53
    • Single-Quoted Strings
  • Escape Sequences 2:31
    • Escape Sequences
    • Example
    • Escape Sequences for Commonly Used Special Characters
  • Double-Quoted Strings 6:04
    • Double-Quoted Strings
    • Variable Interpolation
  • Coding Conventions: Strings 7:54
    • Coding Conventions: Strings
  • Homework Challenge 8:54
    • Homework Challenge

Transcription: String Data Type

Hello, and welcome back to's Introduction to PHP course.0000

In today's lesson, we are going to be talking about another one of PHP's basic data types, known as string, or the string data type.0004

In today's lesson, we are going to talk about, as mentioned, specifically the string data type.0015

We are going to talk about single-quoted strings and what are known as double-quoted strings, which will make more sense as we progress through the lesson.0019

The string data type: what is the string data type used for?0031

Well, a string is essentially a series of characters--in other words, text.0034

A string would be a sentence, or a couple of words, a space...0040

This is a sequence or series of characters; that is what a string is.0057

Now, there are rules for specifying string literals, as we talked about float literals and integer literals in the last lesson.0065

And string literals--there are four ways in PHP in which you can do that.0076

One is known as the single quotes method and the double quotes method, both of which we are going to go over today.0080

There are two other methods, or syntaxes, known as the heredoc syntax and the nowdoc syntax.0087

These are lesser well-known syntaxes that are a little more complicated, and not typically used, so we are not going to cover them in the course.0093

But we do provide, in the Quick Notes, a link to a reference where you can find out information about each of those different types of syntaxes0101

if you find a need for it, or if you are interested in how they work.0109

Single-quoted strings: single-quoted string literals are strings, or series of characters, enclosed in single quotes.0115

You have the word string, and we enclose it in single quotes; and that is a string literal.0128

Now, using single-quoted strings...PHP only allows you to use a couple of what are known as escape sequences,0136

and those are the escape sequence for the single quote and the backslash.0144

We are going to talk about that in just a second--escape sequences.0149

So, what are escape sequences? Some of you may be familiar with these.0154

Basically, what they are is specific character sequences you place within your string that are used to represent special characters.0158

For example, with our string literal that is literally the value "string" that we had on the last page, 0167

what if we wanted to include a single quote within the actual string itself--maybe we wanted a piece of quoted text?0177

How would you do that?--because, if we put...let's have a sample message, and let's say we want to say 'Hello 'Matthew'' in single quotes...0186

How is that going to work? Well, single-quoted strings treat everything between two single quotes as a new string.0204

So, once it reaches the end of Hello and comes here, it is going to assume that is at the end of the string,0214

when in fact, you want the single quote to be part of the string.0221

In order to include that, we use escape sequences.0223

So, if we wanted to write out Hello with Matthew in single quotes, the escape sequence for single quotes is the backslash character,0227

followed by a single quote--and there is our string literal that will say Hello with 'Matthew' in quotation marks.0238

And specifically, the backslash is what is known as the escape character; and that is: when a PHP is processing a string or a string literal,0253

and it finds that backslash, it knows that whatever is coming after it is going to be an escape sequence, and it is going to represent a special character.0263

I have listed here escape sequences for commonly-used special characters in strings.0273

We went over the single quote, which is just backslash and then a single quote; there is one for the double quote, which is a backslash and a double quote.0281

The escape character for the backslash is just two backslashes; for a dollar sign, it's forward slash, dollar sign.0293

You could also...these are actual characters that you will see on the page, 0300

but you can also use escape characters to represent things such as white space.0304

For example, for a new line, we use backslash, n; and that represents a line feed to go back to the next line.0307

You can also represent a horizontal tab with /t.0315

Additionally, you can also use escape sequences for special characters that, for example, don't have a key on the keyboard, like the copyright character.0319

What it does is: if you use the escape sequence backslash x, and then follow it by two hexadecimal digits, what that is saying is:0329

the two hexadecimal digits are the hexadecimal ASCII code for a particular character--0340

in this case, this hexadecimal number A9 refers to the copyright character.0349

You might find that useful for different characters like that, and trademark characters, and so forth.0355

We know what single-quoted strings are; what are double-quoted strings, and why do they have them?0365

Well, double-quoted strings actually are used because they provide many more options to you in creating strings in your PHP code.0369

First of all, they allow for more escape sequences; all of the sequences that we saw on that last slide are allowed in double-quoted strings.0379

However, in single quotes, as mentioned, only the backslash and single quote escape characters can be used.0390

Double-quoted strings allow you to put things like new lines and tabs within a particular string of text.0399

Additionally, and one of the most useful things, is that double-quoted strings allow what is known as variable interpolation.0406

What that means is: variable interpolation is a process where variable names included within a string, or string literals,0419

are replaced by the variable's current value.0427

For example, let's say we declare a variable year, and set it equal to the integer 2011, the current year.0431

Then, we create an echo statement in which we want to output "The year is 2011."0439

Because it's in double quotes, we can include the variable name year, and when PHP goes ahead and outputs this text here,0445

it is going to replace the variable year with this value; so it is going to output this string.0454

This is a very useful function that we will be using all throughout the course.0466

Returning, again, to the coding conventions topic, which is something I am going to mention many times throughout the course:0476

I just want to mention coding conventions we are going to use in this course for string literals and strings.0483

Basically, that is that any strings that don't require any of the special escape characters (like new lines, dollar signs, and so forth)0488

that single quotes don't allow--you should use single quotes.0498

Additionally, any string that doesn't require variable interpolation--you should use single quotes, 0504

and that is because variable interpolation doesn't work with single quotes.0509

Basically, any regular string that maybe has a backslash in it, you would use single quotes for.0514

But anything that you need a variable interpolation for--you would use double quotes.0524

What is today's homework challenge?0535

Basically, I want you to create a script with several echo statements that output single-quoted literals and double-quoted strings.0538

I would like you to create a string literal that includes the apostrophe, or the single quote, within your string.0550

You will have to make use of the escape character; and also, make use of the backslash character, which you will have to escape, as well.0558

Additionally, I want you to create two double-quoted string echo statements that contain both a double quote within them and a dollar sign.0566

And again, you will have to use the escape sequences.0577

These two are going to give you practice in creating variables using escape sequences.0579

Additionally, I would like you to declare an integer and a float variable, and give them useful, meaningful names,0584

using CamelCase notation, as we had talked about in the last lesson.0590

And then, output them within a double-quoted string--so, as we had talked about before, something along the lines of this.0594

What that is going to do is give you practice with learning and using variable interpolation, 0604

which is something that you are going to be using a lot as you program in PHP.0610

And when you do this, be sure to view your script in the browser to make sure the output is what you are expecting,0614

because working with strings in PHP can be extremely tricky, and there are a lot of complications about it.0619

Really quickly, before we end, I just want to look at a sample file with some string examples in it.0627

These are single-quoted string examples, and as you can see, these string literals are quoted with single quotes.0635

Here, we just have a regular HTML text being echoed.0644

This string right here makes use of the single-quote escape character, so that we can have single quotes within our string.0649

And this string literal right here makes use of the backslash escape character (I missed one), and what that does is:0658

that allows us to do things--for example, in Windows, if you are referencing a directory that uses backslashes in your Windows file system.0667

In order to do that, you have to use the backslash escape character sequence.0675

Everything else between a pair of single quotes remains as is, and would be output as is; so no other escape sequences are expanded.0681

So, you couldn't include the copyright sign using the hexadecimal notation that we talked about before, and you can't do variable interpolation.0690

If we browse this file in our web browser, we can see our first line, which outputted 0699

a single-quoted string literal that actually has single quotes within the string itself, and we had to use the escape character for that.0711

Additionally, we have outputted a directory name in Windows that requires the backslash character, 0720

and we have to use the backslash escape sequence in order to include that.0727

And basically, as stated, all other characters within single quotes remain as they are coded.0732

Now, moving on to double-quoted strings: here is an example of a double-quoted string that has double quotes within it,0739

and they are escaped with the backslash, double quote sequence.0747

Additionally, let's say we want to include the price of something in the string.0751

Well, the dollar sign needs to be escaped appropriately, and so we use the backslash, dollar sign to output that.0755

Additionally, as mentioned, within double-quoted strings, there are additional escape characters that we can use, 0762

that you can't use within single quotes--one of them being the new line character.0770

This actually is going to put a new line in the output; and in order to do that, it requires a backslash and an n.0774

Let's take a look at this file again, paying attention to the double-quoted strings.0783

Here we can see how we were able to output double quotes and dollar signs; and it states that new lines require the backslash and escape sequence.0789

Now, we have included new lines in here, in our PHP code, but new lines don't necessarily translate to HTML.0799

So, if we actually look at the source code for this (blow it up a little bit), you can see that up here with single quotes,0808

because PHP just echoes to output what it is given between the double quotes or single quotes,0818

it doesn't create any new lines, or add any new lines on its own.0827

So, even though, for example, like up here, each of these echo statements were on a different line,0830

because no new lines were actually added as part of the string--no new line characters--PHP is just going to end them, one after the other.0836

When we look at our source code here, we can see that all of these strings up here that were echoed are simply output, one right after the other.0843

Now, because we have included breaks, they actually show up as separate lines in HTML, but that is just to show you0854

that new line characters are not added by PHP, and that they don't always translate to HTML.0859

This can cause problems: sometimes you like having your code formatted a certain way, as up here.0866

You probably wouldn't want your HTML to look like this.0872

And so, in order to fix these things, you have to go back and add, for example, new lines.0875

Well, actually, you can't even do that in the single quotes; so you have to change this to a double quote and add a new line.0880

And so, right now, these are on the same line, but you would want them to be separate for good HTML formatting.0894

So, if we reload the page, the output is going to look the same; but when we view the page source,0899

we can see that it added the new line character here; and so, our HTML is formatted properly.0906

Because of this--because of the way PHP formats the HTML--if you want your HTML to look formatted as you want it,0913

sometimes it is better to temporarily escape from PHP mode and back into HTML 0921

to output large chunks of HTML formatted how you like them--for example, this paragraph block.0926

If we actually look at the strings.php file, we can see that we had some echo statements.0932

We escaped from PHP mode; we outputted our chunk of HTML code that we want formatted as we do, which is:0937

we want a new line after the p tag; we want tabs so that the text is indented within the p tag; and so forth.0947

And when we are done doing that, we can escape back into PHP mode again, and you can pick up in PHP mode where you left off.0957

And so, as you can see here, this was the line before we escaped out of PHP mode.0966

This was the paragraph section and the p tags that we used in HTML mode, and this is a statement we echoed when we went back to PHP mode.0977

Also, we learned today that double quotes can make use of variable interpolation.0989

So here, we have created two variables: one, an integer variable for days in the week, 0997

and one a variable called bodyTemp, which is a float variable set to 98.6.1002

And so, let's say we want to output a sentence that makes use of these variables.1008

Well, with double quotes and variable interpolation, we can do that.1011

Here we have an echo statement that says, "There are ___ days in a week," and what it does is:1015

it replaces the days in the week variable with this value, which is 7.1021

We would expect this statement to say, "There are 7 days in a week."1026

Additionally, we have bodyTemp variable within this echo statement; it says, "Normal body temperature is ____ degrees."1030

It is going to replace bodyTemp with 98.6, so it should say, "Normal body temp is 98 degrees."1037

Let's go back and look at that file; and it looks like we had an extra block of code here--let's clean that up...1047

We can see that variable interpolation worked, and it output the value 7 for the variable daysOfWeek, and it output 98.6 degrees for bodyTemp.1062

And so, that is a useful way of using variable interpolation to make strings with variable quantities in them.1072

That ends today's lecture; thank you for watching, and we look forward to seeing you next time.1082