How PHP Works

Hey guys, welcome to this new video we will be looking at the basics of PHP in this video. And on my screen I am going to start from scratch and then we’ll go step by step as to how we can go about getting our PHP development started. Now the first thing that we want to do is ensure that we have all our tools set up and ready, namely Visual Studio code and our XAMPP server. If you don’t have those, then you can check my previous videos where I would have walked you through installing them and getting your environment ready. Now if you have already done so, or by the time we have done so then you can pick up from here. And the first thing that we want to do is start up some so we can just go to our start menu and look for our XAMPP server that’s a MPP. Next, we want to make sure that Apache and MySQL at the very least is running. So we can just click Start. And you can change configurations that way. made them start automatically. But for now we’ll just leave them as is. So Apache and MySQL one started will appear green. One note with Apache is that if you have something else using the Port 80, then you mean get this in orange as in it started, but it’s unable to be used. So you probably want to just check out if you have maybe Skype or another web server on your machine using Port 80. At which point you will have to start do have to modify your configurations. To change that configuration, you can simply click on the button here that says config, and then open the Apache httpd. conf. And then this will launch a text file which allows you to look for the listen port. So you can change that to 8080 or another port number if you need to. And that would solve your problem. It would kind of introduce another tiny problem. But if you have that if you don’t have that constraint Then you’re fine, and you’re good to go. And then we can proceed. The next thing you want to do is to create a folder inside of your Zamp for your XAMPP web server folder, so what I’ll do is go to my computer. And then I will browse to my installation folder, for example, which if you didn’t change it by default, we see some, and then we look inside of ht docs, and then all of our web applications and PHP code and all those nice things will go in here. Now my recommendation is that once you’re about to create a new site, you create a new folder. So since this lesson is about learning the basics of PHP, I’m going to create a new folder, and I’m going to call it PHP underscore primer. And then every file that is associated with this lesson, and by extension in the real world scenario, any website that you will be creating you would put in its own folder, and then every file associated with that website would go inside that folder. Now that I have initiated a new website in my web server, I’m just going to open up Visual Studio code and then we can get the party started. Now based on your last activity in Visual Studio code, it may look a bit different. Maybe you had a website open before and it’s still open. Either way, what we’re about to do we can do together so we can go to File and then open folder. And then that will allow us to browse to the folder. So I’m going to browse to HD Doc’s avatar in the Favorites already but then remember, you can just go to your C drive and find your installation folder for them. And then you will find the HD Doc’s inside of that folder. And then inside this folder, we have every other folder so you want to select the particular website that you’ll be working on at that time. and in this situation, we were working on PHP underscore primer, so we select folder And well, I started some configuration. So I’d say don’t save, you probably wouldn’t get that prompt. But any work that you were doing before, make sure you save it before you proceed. And then having done that, you see that the Explorer is now showing the empty folder to the left, because well, we created a folder but no files. So we can go ahead and create the first file. And we will call this file and always call this file index. The first file for any PHP application has to be index, even if it is going to be the home page. Don’t name it home dot php, you name it, index dot php. And by default, I wrote HTML it really should be PHP. So all files that will be PHP files will have the extension dot php. So dot HTML is for regular HTML, you know, you’re not doing anything fancy, nothing server side, just straight client side then that’s HTML, but For PHP, we have the file extension dot php. And once we create that, we see that we just get a blank file. And then we can fill it with whatever content we need right. Now the great thing about PHP is that everything that you do in HTML still works in a PHP file. But when we start injecting PHP code, the file extension will kind of negates the use of the code or enable it. So in this case, because we have dot php as a file extension, we have enabled the use of PHP code. But first, we’ll start off with an HTML page. So in Visual Studio code, we can simply type HTML colon five, and that will spin up an html5 template code for us. So this is the skeleton of an HTML page. And your HTML pages should always have them at the very minimum, the doctype, the head, and the body and the HTML tags. So that’s doctype head, body, and each one is closed, open and closed. At the very minimum, every HTML page should have that. So once again, if you’re using Visual Studio code, it’s easy to spin that up by typing HTML colon five, and then you will get that beer skeleton very quickly. Now I’m going to name this page, PHP primer index. Alright, so I’m just going to name it’s the name of the website, dash the page. So this is the Index page. And this is the first page that we will be using to, you know, build on top of for PHP application. The next thing that we have here is an HTML tag. So if you’re not really familiar with HTML, or even if you are, I’m just proving to you that HTML works just the same inside of PHP. So in this header tags that spleen is HTML, I’m just going to write Hello, world. And I’ve enabled in autosave already. So once I save this file, then I should be able to navigate to it. Now there are two ways to navigate to this one would be, you know, the regular HTML static way, and the other one will be you through using the server. So if I go to the folder where this file is saved, then I can just right click it and say reveal in Explorer, and that brings up the Explorer, then I see I can just double click it. And well, it’s a PHP file. So it will just open up in the browser so I can have to open with and well, you can see that this kind of difficult getting the PHP file to open with the browser as it would be with a nation of so what we will do is feed right into how the server expects us to behave which is by browsing to the server, and since the server is on our machine, we can Bros. The local host Now what I was saying earlier about changing the port number if you had changed it from 80, then you would have to say localhost, colon, whatever other ports you put switch, if you put 8080, then you have to browse to localhost colon 8080. But it is default, so we don’t have to specify 80. So we can see localhost slash. And you can see that all the folders that we probably had in our localhost will be accessible. But we will be looking for PHP underscore primer. And then once we do that, by default, it furnishes us with the content in the Index page. No, once again, your first page should always be called index not home, not landing, but index. So regardless of the content you want on this page, the first page should always be indexed, because by default, your browser your server is going to serve up the contents of the Index page. So just by browsing To the folder in new okay though there is an index page. Okay, let me just furnish the contents. Alright, so if I change this, this text, PHP primer, and then I save I then I refresh this page, then we see that we’re actually seeing the contents of this page. Now once again, we’re not browsing to the file path, we’re not seeing file colon slash slash c slash blog slash blah, we’re actually just browsing to our web server or XAMPP server, which is found by typing in localhost in the browser. And then we’re going to the specific website folder. So if you name it something else, if you’re working on a specific website, you just put it in a folder and you browse logo host slash that folder name, and then your index page will always be the very first thing that is displayed to you. Now once again, every tag that we know works just the same, it’s still a php page. It’s still an HTML page, just with a few additions. And the addition will come through the PHP tag. So now that you know that every HTML tag still works, we can go on and learn a new tag that you would not have learned in HTML. Now this tag is a PHP tag and to get a PHP tag, we ride our alpha bracket does the same, then a question mark, then the letters PHP. And then we we put in all the code that we want to end this one, we put a question mark, and close. All right, so this injects some amount of PHP code into our web HTML construct. But once again, this code will not work. If we’re not using a dot php page if we are using an HTML page, and I’ll just quickly rename this just to demonstrate what happens. If I rename this to index dot HTML, the browser or the server will still find this as the first page. But then it will literally print this because HTML doesn’t know how to process that. So if I go back to my page and refresh, then well, it’s actually ignoring it because it thinks it’s a tag. So if I put anything in there, and then I refresh, there you go. So it’s actually trying to make sense of all of this. It’s not making any sense. So our file extension needs to always be php file extension. And then we get code hinting, saying that this is not syntactically correct. So if you’ve done any other programming languages, you know that there are keywords that are required for every language. If not, then that’s fine. That’s why we’re here. So the PHP coding language has specific keywords that when they are typed in inside of these specialized PHP tags, they actually carry out specific actions Now what if we wanted to echo Hello, PHP. So we said hello world up here. And I’ll just say hello HTML. So we know that this was written with HTML. But what if I wanted to say hello PHP inside the PHP tag so you can always break the line don’t feel that it needs to be in one line. And then inside of the PHP tags, the open and close I’m going to write the word echo. So echo is the PHP way to print to the screen. So if you want to print a sentence, you want to bring some block of text, you can just say echo, open and close quotation marks and that’s only because I like to open and close, same time and a semicolon, every line ends with a semicolon. So once again, that’s pregnant or Aiko, sorry, sorry, which is used to print and then we have our quotation marks and then a semicolon. So in between our quotation marks, we type our text that we would like to print which is Hello, PHP. And then we We go back to our browser, and we refresh, then we see that our HTML has changed because we change the h1 to Hello HTML. And then we see that we’re printing Hello, PHP. And then that is how you start pushing content to your page through your PHP tags. Now, I’m going to add some comments so that I don’t lose you along the way. And when you get this so caught holding down the Ctrl key and pressing the letters key and see one after the other, then you’ll see that it will generate the comment tag for you. So this is basic HTML. And then in PHP, our comments will look more like a slash slash or a slash, asterisk, asterisk, slash right. And then that would generate a lot of multiple lines of comments as we go along. So you can use either one so you can have more blocks have PHP tags. And inside of one tag, you can have multiple lines of code. So what if I wanted a break point of the soul in HTML, we have a tag that is br, right. And that kind of gives you like a space almost like hitting the Enter key instead of work inside of a Word document, it brings it to the next line. So you have that, what if I wanted to print a construct like this inside of my PHP, so I can actually echo HTML content directly through the PHP, and all I have to do is type that HTML tag. So I can say echo b, r, just like it would look in HTML. And what this will do is generate that tag inside of the overall HTML, code construct or code setup. And then it will actually render that to the screen. So I’m just gonna To print another line here, or better yet, I’m going to print some block of text here. And I’m going to see a second line. So when I print second line or echo that to the HTML documents, then we will see that second line comes all in the same line. So what if I need a second line to actually be a second line and not in the same line. So in between these two, I could echo the HTML. Alright. And of course, those have to be in quotation marks. So echo open quotation and the HTML tag that you wish to have rendered, and then you close and then let’s just try that again. And there we go. So the br tag actually creates a space once again, it’s like hitting the Enter key in Microsoft Word, and it brings whatever comes afterwards the next line, and that’s what happened there. We have our code to print hello. PHP, then we break the line, the br means break. And then we have our second line and then that’s how we actually get that. So that being said inside of the PHP tags, no regular HTML is allowed. If you want HTML, then you have to echo it directly. Now I’m going to go ahead and create another PHP tag. So that’s angle bracket question mark, the word PHP or the letters PHP, and then question mark and close angle bracket. So as I open this, I close it, so don’t forget to close it at a later date. And then what I’m going to do in this scenario is creates what we call a variable. Now in a variable situation, what happens is that we declare some form of storage era. You know, many people call it different things. You can call it a bucket, you can call it a storage area here, just declaring something that is going to store a value and then once that value is stored. If you need this value a third, fourth 10th time, you can just call on this very instead of having to type it all the time. So like, for instance, my name is Trevor Williams, someone went to declare a variable to store my name. And then I’m going to print my name across multiple situations without having to type it each time. So to get a variable in PHP, we just use the dollar sign, and then we give it a name. so in this situation, I’m storing my name, so I’ll just call it name. And then we say is equal to whatever value we want. So the value I want my name variable to be equal to is my name, which is for vor. Williams know, once your code renders, once your page renders, then name is automatically generated somewhere somehow you really don’t have to worry about that. Maybe it’s on the server, but is generated and it’s stored somewhere. So anytime you need it while you’re on that particular appears. So that means that name here is unique to the Index page, then you can access name as long as it has been generated. Alright. So if I wanted to echo my name, then I could say echo, and then I just see name. And be sure to make sure you represent your variable names properly. So if I go back to my page, and I refresh, then I see Trevor Williams has been echoed. So the screen so I declared the variable name equals drummer Williams and then I echoed it, and it’s there. But you notice it’s still kind of attached to the second line and that’s because I need another break in between second to stay this and I can make or break after a second line or I can make or break before this echo. The fact is, the echo will always puts it in one line unless you try to break it. And this is an excellent way to just get that now. Next line effect. So I’m just going to echo PHP break, Echo, second line break, and then echo my name. And then I will just refresh. And there we go. So in case you’re wondering how I refresh, I’m pressing f5. So you can refresh using this, or you can just press f5 on your keyboard. Now what if I wanted to print this all as a sentence and even more so in an h1 tag for argument’s se, so we already established that we can print our HTML tags directly to the screen so I can say echo and then see, h1, open the h1 and I’m just going to close it same time because I don’t forget to close it later on. And then inside of the h1, I can see my name is colon, and then this will actually generates or you know, right? render an h1 tag to the screen with the text, my name is there we go. So we’re echoing an h1 tag with the sentence My name is, so what I want after this is actually my name. So then we can do what we call concatenation, which is kind of very structured. So I want you to be key and and very close attention to what I’m about to do. And what is required for concatenation is the end the block of text somewhere, I touched the variable and then and then reattach maybe if needs be another block of text. So it’s really just about chopping up a string, because you have many different sources for the content for this string. So one source for contents of the string is is this tag, but in between this tag we need to put in our variable which contains the name for Williams. Alright. So the advantage to this is that If, for argument’s sake, I give you this and you wanted to print your name instead of my name, you can just change it one time here for that name variable. And anywhere that you’re echoing name, it will echo whatever value you set this to. so in this situation, we want to change this block of text from seeing my name is to sing. My name is Trevor Williams and maintaining all of that inside of an h1 tag. So I’ll start off with my name is I open the string sorry, and I start off with the text, I want to echo. And then when I get to a point where I want to inject the text that I want to add on to what is being echoed, I’m going to end it there. All right, then you see the red lines appearing because they’re syntax errors. No. All right, it doesn’t know what to do about this. But that’s no fear because then I use a full stop. And then I attach my variable and my variables name is name. Alright, so that’s my variable. And then I Another full stop. And then I put on my quotation mark. So let’s look at that, again, I have one block of text, and that’s characterized by an open and close quotation mark. Alright. And sometimes you get, you can use single quotes, if you want, I use the sorry, you can use double quotes If you want, I use single quotes most times, but you can almost use them interchangeably in most situations in PHP. And then so I open quotation mark, I print my tag or the initial part of my tag. Alright, I then I inject the content that I want by using the full stop. So the full stop says attach this to this to the what’s on the left, and then I put it there. And then the next full SOP says attach this to what is on the right, and what’s on the right is the closing tag for my HTML because even though I’m writing PHP, I want to make sure that when it renders to the page, the HTM are listed as syntactically correct, because the HTML for an h1 requires you to open and close and the content. Well, that’s up to you. All right, so I’m just opening, adding some content, some static content, but then I’m adding dynamic content because this variable the word variable means subject to change. So the value that is presented by the variable here name is subject to change because I can change this, like I said, from for Williams to any other block of text. And once that is attached to what’s on the left, I’m just going to attach it to the closing tag for the HTML. And then I just end all of this with a semicolon. And then let’s see if my theory works. So what is expected is that when I refreshed it should say my name is with a little space because I put a space here inside the string. Alright, and then the name and then we’ll end of text So when I refresh, there we go, my name is with a little space. And then for vor Williams. Alright, so let’s try that exercise one more time. So we just printed out our name, I’m going to print my age or well an age. So I’m just demonstrating that these variables, we can always declare them when we want. And my suggestion is that you try to declare your variables one place, there are times when you won’t be able to do that, but try and get them all in one area. So that’s kind of organized instead of having variable declarations all over the place. So I’m just the query name and then I’m going to declare one called age and I’m going to tell you that my age is 21. All right, and then what I want to do is echo that my age is 21. Now what I’m going to do is use a keyboard shortcuts that’s Ctrl C Ctrl V, without highlighting with its copy and paste, but without highlighting what I want to Copy, I can just select the line and then Ctrl C Ctrl V and duplicate that line without having me highlight what I want to copy. And what I’m going to do is just retain this code, and then just change this to my age is, and I’m going to change this variable to age. All right, so I’m printing out, my name is whatever values in the name variable, and by ages whatever value is in the age variable. So if I wanted to change this to 30 to 40, each time that this page renders whatever the value is, that’s what will be displayed. Alright, so other state results, or I can leave that and then I will refresh this page. And we should have two h1 tags sprinting, for war Williams and my age is 21. All right, and just to drive home the point that I can change this to anything I want at any time, if I refresh, I just change it to 31 and I’m going to refresh And it should print 31 So, once again whatever the value is off the variable that is what will be displayed in your page. Now with all that being said if you have been exposed to other programming languages, you probably know that you will have different data types and your various their their rather strict on data types. If not, then that’s fine. But the reality is that PHP and variables, they are not what we call a strongly typed meaning that it can be a text No, it can be a number later it can be a char, it can be a Boolean, it can be almost any data type without you having to specify that it is this type of data type because right here, then nothing has changed in the way I declared the variables but the values are distinctly different because this is a word and that’s a number. In programming in other programming languages, such violations are almost not allowed. So the final thing I want to show you is how the single quotations And the double quotations kind of differ when it comes to echoing. So with a single quotation, we have to use our concatenation method to stick in any other bits of information coming from a variable. However, if I wanted to echo this very statement and say echo, and I use double quotation marks, then I could still say h1, and my name is and then instead of having to end the string and then put in the variable and then rejoin the string, I could just write in my variable name. And just continue typing like it’s one big sentence. So I opened my h1 tag, I put in the content, I stuck in the variable and you notice by the text highlighting it kind of shows you that it is not falling in the same category as are the strings and if I do that up here, if I said name, he You see that it turns orange. Now oranges whole Visual Studio Code shows me that something is, is part of the string. So just by the fact that it is kind of that light blue, it shows me that that variable is going to be represented in this echo statement without my need for the full stuffs in the concatenation. So that’s up to you. That’s I’m not saying one way is right, and one way is wrong. It’s just a matter of whole quickly you can work and if I refresh this, you see we get the same results, top and bottom. So whichever one works for you, that’s fine, whichever one you find easier. So I will make this index file available to you and you can read through the comments and try and appreciate it and if anything sticks out to you that you don’t understand, feel free to drop me a line and I will be sure to respond to your query.