Start learning with our library of video tutorials taught by experts. Get started
Viewers: in countries Watching now:
PHP is a popular, reliable programming language at the foundation of many smart, data-driven websites. This comprehensive course from Kevin Skoglund helps developers learn the basics of PHP (including variables, logical expressions, loops, and functions), understand how to connect PHP to a MySQL database, and gain experience developing a complete web application with site navigation, form validation, and a password-protected admin area. Kevin also covers the basic CRUD routines for updating a database, debugging techniques, and usable user interfaces. Along the way, he provides practical advice, offers examples of best practices, and demonstrates refactoring techniques to improve existing code.
In this movie, were going to be learning about the data type called Strings. Strings are a very common feature of most programming languages. And we've already been using Strings without knowing it. A string is a set of characters. Those characters can be letters, numbers, symbols. And their going to be defined inside of quotation marks, either single quotes or double quotes. In non-programming terms, you can think of a string as being text. In the last movie, we're working with hello world. Hello world is an example of a string, so is the br tag. That is a string as well it is defined inside those quote. It is a string of text that we can then work with. let's take our variable.php page and do a Save As on it, so that we can create a new one for strings.
Strings.php saved in our sandbox, we'll call it strings and I'm going to take all of the PHP that's in here and let's just take it all out and we can start fresh with our strings. Let's try it with just a simple echo of a string, right? Hello World, we saw that before, nice and simple. We can also put HTML inside of it, right? That's perfectly valid, that's a string. You can output it, and it will output the text, hello world, followed by the HTML tag br. We can also do the same thing, but with single quotes. I'm just Copy and Paste it, and I'll replace the double quotes with single quotes.
In general, I'd like you to start getting used to using double quotes, though, instead of single quotes. And in just a minute, we're going to see why. Now, we also saw that we can assign strings to variables, so for example, if we have greeting equals hello, and let's have target equals and we'll have world. And then we can actually put those together phrase equals the greeting. And then we learn how to cancatnate with that dot. And I'll put another string that has a single blank space in it. That's still a string even though it only has a blank space, and then target.
And last of all lets echo that phrase. Alright, now that we've got a few of these, let's bring them up in the browser and try them out. Let's go to Firefox, and instead of variables, we're going to be going to strings. So there they are. They all came back just fine. Hello world, hello world, hello world. Now, we can also do variable replacement inside a string. Let me show you how to do that. Let's drop down here, I'll do a br tag. Let's open up a new set of PHP tags, and let's do that variable replacement down here.
So, let's say that I have echo and this time I still have phrase define, right? The fact that I defined it in a different block of PHP doesn't matter, this variable is still set, and inside double quotes I'm going to tell it to echo phrase, space again. Let's go ahead and put br tag at the end. So, I've now put the variable inside of the double quotes. Because it has the dollar sign in front of it, PHP is going to see it as being a variable, and it's going to pull up the value that was here.
Let's try that. See, hello world again. So, it dropped in that phrase followed by again. Now, this only works with double quotes, that's why I think it's a good habit for you to get in using double quotes instead of single quotes. Let's just change this real quick, to single quotes and you'll see what happens. It just comes back with the literal phrase again, it doesn't do the substitution. That only happens with the double quotes. Now, there's a problem with this, right? Let's imagine that we have our phrase here, hello again, but what if we didn't have a space? What if we were trying to use a variable? And then right after, wanted another bit of text.
Well, if we run this PHP is going to see that as being one variable. It's going to look for phrase again as a variable. That doesn't exist. Well, the best way to do these in line replacements, I'm going to select it all, and then I'll put curly braces on either side of it, then I'm going to clearly differentiate what is the variable from the non-variable. When we're inside the double quotes, they have the special meaning and they allow us to put variables inside of them, to do this in place substitution. You can put slashes in front of it if you wanted to have the literal curly quotes. Same thing if you wanted to have literal double quotes inside of your double quotes.
You could use a backslash double quote in front of it. Backslash escapes the value and let's us use it. So let's see what this looks like. Let's just save it switch back over, re-load it and see it works here. Alright, now I was able to still find it and still do in place substitution. So, I think if you are going to do in place substitution, this one is your best bet. This one is the one that allows you to make it really clear what you are doing, this is a variable that's being dropped into the string. So try and use those curly braces. Of course, it is still just as valid to do it by concatenating the two together instead. And you'll find yourself using both. Now, that we understand the basics of strings, in the next movie lets take a look at some string functions that are going to be really useful.
Find answers to the most frequently asked questions about PHP with MySQL Essential Training.
Here are the FAQs that matched your search "":
Sorry, there are no matches for your search ""—to search again, type in another word or phrase and click search.
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.