Example Lesson Videos - PHP Bootcamp for Drupal 8: Creating Your First PHP Program (Part 2 of 2)

  • Lesson Notes
  • Playlist
  • Transcript
Lesson Notes
These are two example lesson videos pulled from our PHP Bootcamp for Drupal 8.

See Full Course Syllabus and Schedule

Example Code For This Course

All the example code you see me using in this course is available as a git repository. This allows you "checkout" different versions of the code to move to any point in the course. Your primary assignment each week is to follow along with creating the modules you see me create in the lesson videos. You have two ways you can do this. Personally, I prefer to copy and type out the programs I see on screen, because I find it helps me remember things better. However, this can be fairly time-consuming, so there is a second option. Since I have provided many different versions of the code in the example git repository, you can watch the videos, and when you get to a checkout point (there will be an on-screen prompt), checkout that version of the code, read through it, and run it to confirm you understand what you've just watched. That will take less time, but I will leave it to you to decide how you learn best. Be sure you perform the clone operation in the correct folder, depending on whether or not you want to copy the code yourself or just use the example code to follow along. I discuss the appropriate locations in the code repository video.

Frequently Asked Questions

Do the lesson videos play on iPads and iPhones?

Yes, they work on both desktop/laptop computers and iOS devices.

How can I ask questions or get help?

You can ask questions about the lesson or get help with your assignment by posting to the class discussion forums and attending the weekly live Q&A sessions with the instructor. (online via webinar software)

Do I need to have my own website or hosting account to work on?

No, we will provide you with a development environment you can install on your computer. Installation and configuration of the local development is covered step-by-step in course lesson videos.

In this video we are going to create our first PHP program. Make sure that your development stack is up and running and then we can create our first project in NetBeans. So we'll do that in NetBeans by choosing file new project. Choose PHP and it'll be a PHP application. Click next. You'll need to browse to your PHP development environment, the BitWisdom development stack and then inside the sites folder and we'll call this project... I'll just call it php. Now NetBeans is a little annoying here. We actually want our sources folder to be php inside of sites. So make sure you get that path correct or the folder correct. Choose... if have version... NetBeans has PHP 7 available please choose that. Otherwise, choose PHP 5.6 and then choose next. We need to add the port number so colon 8888 to this project URL and click next. We're not going to use any frameworks and we're not going to use any composer packages so you can finish. Now by default, it creates this index.php. I'm going to get rid of that. So I'm going to choose, I'm going to right-click it and choose delete, because I want to start from scratch. And now we can create our first PHP file. So if I right-click onto source files, choose new PHP file, I'm going to call this one hello_1.php and then I'm going to type the simplest PHP that I can which is a simple print statement. So it's the word 'print space, "Hello, world";' and then very important a semicolon at the end. A semicolon should go at the end of every line of PHP that you write unless it happens to be a block of more than one line in which case you might have some curly brackets instead of a semicolon. But most... almost all lines of PHP you're going to end with a semicolon. And I will save that. I'll go over here to my development stack, refresh. I should see my folder that I just created here under your sites. I'll click through that and I will click the hello_1.php file and there's the statement I told it to print, "Hello world" so it works. All right. Let's talk a little bit about why it works, how it works. So this PHP code. PHP is designed to work in tandem with HTML. Now you'll notice as it works in web server, you'll notice that it did execute. Like we don't have the word 'print' doesn't show up here. So it did execute the statement. And the reason it did that is because this statement shows up after a PHP tag. So a PHP opening tag looks like what you see on the on this line. The 'less than' sign, I'll just say open pointy bracket, question mark php. So when you see that symbol that means anything after it is PHP and you'll notice that there's no closing tag here. There actually is sort of if you want to close your PHP so that let's say you want to go into HTML there is a closing PHP tag that is just a question mark followed by closing pointy bracket. Most of the time if you just have a PHP file and there's no other HTML or anything at the end, this closing tag is optional. So that's why even when we didn't have it worked fine. However, if we put it in there and save, it should work the same as before because this is a sort of a more complete version of the same file. All right and so that's how we designate that a certain part of a PHP file should be executed versus being just rendered as HTML. In this file by default you'll notice that NetBeans put this commentary in here and let me pull it over a little bit. But this didn't get executed and this didn't show up on the website or when we executed the file here. So this is a code comment. Sometimes, you want to leave notes in a code that are, in any of our code that are designed for humans for to read not for the machine to read. And that human that you may be leaving a note for could be yourself. Always remember that so especially if you're writing some complicated code, it's really handy. If you go away for six months, nine months, come back and then you've written yourself a note about why you did something that's super helpful and so PHP provides a way that you can put notes in the code that won't be executed. They just... during the compile step they get thrown away, but they're always in the text file there when you go back and read it. And so what you're seeing here is one... there are actually two types of comments that you can make. You're seeing one style here. This is a multi-line comment and a multi-line comment starts with the forward slash and an asterisk, a star and then every line that is part of the comment has the star at the beginning of it. And then when you want to finish the multi-line comment you have the star followed by another slash. All right if you want to make another multi-line comment down here, we could do it ourselves by doing slash, star and you'll see actually NetBeans helps us out a little bit. When I hit enter it automatically sort of filled in the rest of the closing bracket sort of if you want to call it that, the closing of the tag down here and the star. So I could put another multi-line comment and if I hit enter, you'll notice again, NetBeans, because NetBeans understands PHP, it automatically added the star for me. But it's critical. If I take that star out this will cause problems if I try to execute. So if for some reason, don't take you know, make sure not to delete it or anything. Or if you're using another editor that doesn't do this automatic formatting for you, then make sure that you always have a star at the beginning of the line. So you can type as many lines as you want in a multi-line comment like this. The other type of comment is a single line comment. Sometimes you just want to make a short note right before some line of code, usually. And that you can do with two slashes, two forward slashes. So... So if I write a note here, that's a single line. You'll notice if I go to a second line and start typing, it once again, NetBeans is going to start thinking I want to type code. Okay. It doesn't automatically assume I need multi-lines. The two slashes is typically for a single line. Now there's nothing that prevents you from you know, typing more lines like this, with a bunch of two slashes. I don't recommend that. That's considered sort of bad practice. If you need multi-lines of commentary use this other style. One thing that you can do is sometimes you've a really short line of code and you want to make a note beside it. And so you can do this with the slashes, the two slashes. You can add a comment to the end of a line. So I have this one line of code here, print hello world. I could add two slashes and then say, this is sort of an end of line comment and that's acceptable, as well. Then you could see that NetBeans is telling me it's acceptable so that it makes it gray because it knows it's a comment. So if I save this, this should have no effect. A comment should have no effect on the execution. Yeah. See it all looks the same here. Now obviously if you're doing end of line comments they need to be rather short because you don't want to have lines that go on and on and on. So these end of line comments are usually useful if you, you know, maybe want to maybe there's like a machine name and a label or some human readable name for something and you're using... the code will have to use the machine version of something like short version or maybe some kind of integer and you want to make a note about what that represents, that's a good use for an end of line comment because it'll just be short. Okay. Next, let's take a look at an example where we mix HTML and PHP together more because this file was pure PHP basically. So I'm going to make a second file. So again, I'll right click on source files, new php file and I'll call this one hello_2.php. All right. In here, I want to start to type in some HTML. If I try to do that... let's just make like sort of the most basic HTML possible. First of all, you'll notice that NetBeans is complaining here. It's giving me some red. If I save this and then try to execute it, you'll see that I get a PHP error when I do that. And that's because this HTML is not PHP code and I did not close the PHP part of this file. Obviously, this comment is just sort of boil a plate and so we could get rid of it by deleting the entire PHP section. If we want to leave it in, then we need to make sure to close the PHP bracket. You can do it either way. You can also obviously make the comment reasonable like PHP with HTML example. Okay? And then I'll make sure to close the PHP and that's fine. So now if I come over here and refresh this, yeah. Now that I've saved and I should point out I did save but I did it with my keyboard so you might not have seen it saved and refresh. And now you see it's blank but it's not giving me the PHP error. And you can also see in NetBeans that it's showing me this HTML in blue so it's indicating that it understands it as HTML now. So with PHP, we can mix HTML and PHP together and we sort of flip back and forth between them. So here, you're saying I start with PHP. I just do a comment and then I go out of PHP with the closing bracket. I go into HTML and so let's write something really simplistic. So we'll do a body tag. Notice that again, NetBeans is helping us out. It would do some auto-completion. And I'll do maybe a header 1 tag and then we could type some static text here, hello world and then save. And now if I show that, you'll see that I have that hello world. But what if we want to do it with PHP? Now this is kind of stupid example because it's just plain text, but if wanted to print hello world using PHP like we did in this example, we could integrate PHP here. Let me just delete this out. By opening the PHP tag with a pointy bracket, question mark php, and then we could type our statement, print "Hello, world" semicolon and then our closing PHP. Now notice I did that all in one line. I could break it into multiple lines. It's no different, but I've got an opening PHP tag, a PHP statement, and then a closing PHP tag. So if I save that, it should look the same over here. Well, it's with the comma, but otherwise, the same. And we can do this as many times as we want doing it in this file. So for example if I wanted to add a title to this HTML page, I could add a head section and the title section and then again, I could print "Hello, world!" in PHP tags opening and closing and maybe I'll show it there. It can be on multiple lines if you want. It doesn't matter. And I'll save. Okay. And now I have "Hello world!" up here. You can see it in the browser tab because that's what the title tag does. All right. So you can see I can go sort of back and forth and obviously, these are very simplistic examples. We're going to start to make them more complex in the next videos, but this is how PHP works and how you can combine it with HTML.