 Hey everybody, this is Brian. I welcome to the 9th PHP tutorial. I've already got a file all ready to go and this one is going to be fun with strings. What is a string? Well, I'm glad you asked. The string is just a variable. We've made a couple of them so far, but it's a special variable because it's inside of quotes. Now one thing you should know is that this can get a little bit tricky. That's why we're having a full tutorial dedicated just to strings. So we'll say my string. Let's actually Echo this out. See, there's my string. Now, how can this get a little tricky? You notice how a string starts with double quotes. Well, PHP is actually very forgiving. You can start it with double or single quotes. The problem comes in, whichever one you started in, you'll need to escape it later, and let me explain that. Let's say my quote. This is my quote. Whoops, can't spell. For the day. Notice how Komodo ID is basically barking at me saying, hey, syntax error. You screwed something up. Well, what it's complaining about is these quotes. We have a string within a string basically according to this. Our delimiter is a double quote. And we have a double quote here. So what it's thinking is that this is a string and that this is a string. And then we've got this quote thing in the middle that it doesn't understand. So what we need to do is escape it. By escape it, I mean you simply put a slash in front of it. That way it treats it as a character and not as a quote. PHP has many types of escape characters, which we'll cover in future tutorials. But for now, just understand that you'll have to treat this a little bit special. Let's actually echo this out. See, this is my quote with the quotes in there for the day. Now, I realize some of you geniuses are out there saying, well, you could have used the HTML code for it, which I think is hex 34. That's actually pretty scary that I know how to top my head. But anyways, yes, you could, but it's a lot easier just to escape the character rather than memorizing a bunch of numbers. Now, let's actually play with this a little bit and do my single quote. What we're going to do now is we're going to change the delimiter to a single quote. Now, let's actually delete the escape and notice how there's no error this time. This is because our delimiter is a single quote, and then we're using double quotes internally. That's one way of getting around that. The problem comes in. And let's actually say, let's call it a single example. You think you're smart and you're doing this and you say, OK. Bob's idea was dumb. Well, we shouldn't have to escape that because we're using single quotes, right? But look, we got to escape this. So either way, you're not going to get out of escaping. You're just going to have to learn to deal with it. Unfortunately, this is a holdover from a C style language. All C style languages use this. When I say C style, I mean PHP was derived pretty much from C++ and a mixture of Java, I believe. So C styling or just like C, C++, Java, C sharp, there's boatload of others have this same issue. That's why you need to know it. Now, strings are a special type because they have to be quoted and there are a ton of functions that involve strings. If you don't know what a function is, it's code that's already been written for you. Somebody else already wrote code, you just have to know how to call it. And we're going to cover functions in the next tutorial. But if you go out and look, there are a lot, a lot of functions just for strings. Wow, yeah, we're not going to cover all these, but we're going to cover some of the more popular ones. For example, we're going to get the length that I'm a spell that I did of a string. So we're going to say echo, could just do this. The length of my name is strlen, which is short for string length. And we have to notice how it's asking for a string and it even says gets the string length. You can just go ahead and type your name in there. Save it, let's run it. The length of my name is five. Well, b-r-y-a-n, sure enough, five characters. So now, let's cover, let's do a position of a string. Like I said, I'm just going to cover some of the more popular ones here. There's over 700 functions in PHP in case you're wondering. So there's a lot to know. The position of dog in this sentence is, and let's actually do str, pose for string position. And notice how it's wanting a string haystack and string needle, meaning you're looking for a needle in a haystack. So the first one is the haystack as it's called. And let's actually say, grab this whole thing. I know that's a bit long and we could very well turn this into a variable. And actually, let's just go ahead and do that. My source, or actually let's call this haystack. So we're going to say the position of the string is, the position of dog in this sentence is 16. Now let's explain this a little bit because I'm sure this might be a little bit confusing. We're looking for the position of a string. We have a variable called haystack. This is the string right here. This is the source string. Notice the variable haystack. Now, notice that we are saying string, but the variable is inside the string. That's one of the magic powers of PHP. You don't have to escape this string and concatenate it, et cetera, et cetera. You can just put the variable right in the string and it'll still spit it out. So it's saying the position of the dog in the sentence is, and then we're looking for string post. You guessed it. Here's the haystack and here's the needle. We're looking for the needle in the haystack. Pretty ingenious, huh? So the position of dog is in 16. So if you wanted to go and count all that out, it's actually at 16. Some of you are saying, ah, that's at 17. Actually, remember, strings are zero-based, meaning the very first position in a string is zero. One, two, three, four, five, six, seven, eight, nine, ten, eleven, twelve, thirteen, fourteen, fifteen, sixteen. There you go. Remember, strings are always zero-based. Zero-based is another holdover from the C-style languages. Now, the last function we're going to cover, because I'm getting a little crunched on time here, is how to get a substring. What is a substring? What's a string within a string? So what we're going to do is we're going to say, echo substring, and notice how it wants a string, an int start, and an int length. What is that? Well, the string is our source. The int is an integer or a number, meaning the position within the string to start at. And then notice the brackets here. That means that's optional. If you exclude the length, it'll just take from the start all the way to the end. But if you give it a length, it'll do up to the length. So let's actually say, haystack, and let's start it at the third position and grab four characters, just because we can. Let's actually do this. Oops. There we go. P-O-S. See? We're starting at the third position. One, two, three. And then we're grabbing four bytes. Now you don't see four bytes. You see three. Why? Because there's a space there. For example, if we grab three characters, P-O-S, there's only two. Why? Because it's that space, it's just the way HTML is rendered in some systems. Probably a better example is if we say zero to three, hit the word the. See? If you say zero to two, you guessed it, TH. Well, that's all for this tutorial. I hope you found this educational and entertaining, and thank you for watching.