8.2 Your First Line of PHP Code
In this lesson, you'll write your first line of PHP code. I'll show you how to mix HTML and PHP in a
.php file. I'll also teach you about HTTP server default documents.
1.Introduction2 lessons, 05:59
2.How the Web Works2 lessons, 16:03
3.Creating Documents3 lessons, 27:13
4.Styling Documents3 lessons, 34:07
5.Scripting Documents3 lessons, 22:36
6.The Document Object Model6 lessons, 42:45
8.Introduction to Server-Side Development5 lessons, 43:24
9.Getting Started With Databases3 lessons, 31:01
10.Using PHP to Interact With MySQL4 lessons, 51:59
11.Conclusion1 lesson, 01:47
8.2 Your First Line of PHP Code
In the previous lesson, we installed a utility called MAMP. Which makes it easier to get started with PHP development on Windows and MacOS. And now that we have MAMP installed, we can start writing PHP code. Now, the first thing we want to do is ensure that MAMP is running, and you also want to make sure that the servers are running. Now, we don't care about MySQL Server. But Apache server is vital because that is how we are gonna access our files through HTTP. So ensure that that is running there is an indicator there to show you that it is running. You should also go to preferences. And then go to the web server tab and make note of this path. This is the document route. This is where we put all of our documents so that we can access them through HTTP. Now you don't wanna close this, just minimize it, and then let's go to that path and we will see what's there. So mine is C, MAMP and then htdocs and there's nothing there. So we are going to add the first file. So you want to fire up your code editor, I'm going to use Visual Studio Code, and I've already pointed it to that folder. So I'm going to add a new file. And I'm going to call it index php. Now there is something called a default document. Every web server has it and that's basically the document that it is going to serve. If you don't specify a document. For example, let me go ahead and Delete this file. And let's open up the browser, and we are going to go to just local host, that is going to access our websites so localhost there is no port there. And we're going to see index of and then just a slash. This means that we are at the root of our website. And right now, we don't have a default document, which would be index. So we would see all of the files within this directory. Now we don't have any files there. So let's add one, but let's not call it index. Let's call it food.php. And if we go back to the browser and refresh we are going to see food dot PHP, listed here. So we would see everything in this directory until we added the default document. Now we could go here, and we could say local host slash food dot PHP, and then that would take us to that food page. But the default document is what would be used if you don't specify any document in the request. So let's get rid of food. And we are going to add index, index PHP, and this is our file, so we can type anything in here. This is the index, and this is exactly what we are going to see in the browser, now, this is not PHP code. Even though the file extension is PHP, this is just text. So we can come in here, and we can actually have our HTML markup so we can have HTML. Then let's have body And then we could have a p element that says hello Apache web server. And then let's close that. And this is going to be an HTML document that we see in the browser. Of course it looks pretty normal. That's because we don't have anything fancy, CSS or anything like that, but this is HTML. But for right now, we don't care about HTML. We care about PHP. So whenever we want to switch into PHP code, we have to have; Some delimiters something that says that okay, this block of text is special, so we want to treat it as PHP. We do that with an opening angle bracket followed by a question mark. And really that's good enough, but the best practice is to say PHP. So we have an open angle bracket, question mark, PHP. And then this says that anything after this delimiter is going to be PHP. So if we just type something here, and we can see that Visual Studio doesn't like this. But if we go back to the browser and refresh then Now here we can see that we have a response code of 500. That means that something on the server went wrong. Whenever you have PHP, Ruby Python, a speedy nets and you have 500 that means that something is wrong with your code. And that's because that this is not valid PHP code. We're now within the PHP code block. We need to write PHP. Now we could end our PHP code block with a question mark followed by a closing angle bracket. And then this is after PHP and then our PHP code would be inside here. So we can go back to the browser and refresh, and we're not going to get that error 500 anymore. Instead, we see that this is after PHP, but we don't see anything before it because we didn't have any PHP code written here. So let's do that. Our first line of PHP code is going to use Something called Echo. This is a function that we can execute. That writes whatever it is that we want to write to the browser. Actually, that's not technically correct. We are actually outputting data to the response stream, but practically it's being written to the browser. So here we say echo. This is a function that we are calling, and then we want to echo some text. So we begin with a string. Now strings and PHP can start an end with a pair of double quotes, but the standard practice is to use. Single quotes. So we will have echo and then a single quote. This is inside PHP that we have the closing single quotes, and then we end the line with a semi-colon. So let's save that. Let's go back to the browser and now we will see. Two pieces of text, although it looks like it's still one line. It says that this is inside php and then this is after php. And if we look at the source, we can see that they are indeed on two different lines but of course the browser, as it renders this text, is going to do so All in one line. Well, let's add some visual difference between these two phrases. And whenever we echo something we could include HTML. So let's put this is inside PHP inside an opening and closing p tag. And then let's go back to the browser. Let's refresh, and we will see that HTML is rendered. And so now you've written your first line of PHP code, it was very simple, but we all have to start somewhere.