در حال بارگذاری ...

Creating Project Directories0:00

Okay, so our tools are now in place, which means the next baby step is a little one. Let's just see if we can get something in the browser. All right, let's get going. All right, so I hope you're working along. If so, open up the terminal that you set up in the last episode, and I'd like to create a directory for our first website. But real quick, and this will apply across the board for this series, the way you go about creating a new site will slightly depend upon how you installed your environment, how you installed php in your server. So for example, if you went with Homebrew, like I demonstrated in the last video, you

you installed php in your server. So for example, if you went with Homebrew, like I demonstrated in the last video, you can store your website directories anywhere you want. On the other hand, if you went with something like MAMP, have a look at the documentation. I think they will give you a specific directory where you should create your new website folders. So again, just look at the documentation for whatever installation path you chose, and then follow that. In my case, I used Homebrew, so I can put my websites anywhere I want. In that case, I will go to my home directory, and on the Mac, I can cd to that home directory by using ~/.

In that case, I will go to my home directory, and on the Mac, I can cd to that home directory by using ~/. For Windows, I believe it'll be something like cd C:, and then maybe users/yourname/desktop. I think that's right. Okay, let's make a directory for all of our websites. I can use this command, mkdir, make directory, and we'll call it websites, whatever you want. Some people call it code, it doesn't matter. Okay, now I can cd to that directory, change directory to websites. Okay, so now, yeah, we can name this first project anything we want.

Okay, now I can cd to that directory, change directory to websites. Okay, so now, yeah, we can name this first project anything we want. I will just call it demo. So once again, make directory called demo, and we'll cd into that new folder. Okay, I think we're ready to get started. Open up the editor that you chose in the last episode. And then in my case, I can choose Open Here, but for you, it might be File and Open Directory, something like that. Okay, and if I open my home directory, sure enough, I can see that websites folder at the top, and demo is the one I want to open.

Adding an HTML Page2:04

Okay, and if I open my home directory, sure enough, I can see that websites folder at the top, and demo is the one I want to open. Okay, I'm excited. So let me click up here on the top left, and I only want to see my project files, and we're all set. So let's right click and choose new file. And to begin, I'm going to call it index.html. Not php, this is a simple static HTML file. Now most editors will include any number of snippets for common tasks. In my case, I want some basic boilerplate for an HTML file, and this is what it gives

Running a Local Server3:15

So it may turn out that your editor has some kind of functionality for viewing the results in line, like you see here. But why don't we just load it directly in a browser so that we're all on the same page? So to do that, I will return to the terminal, and let's run php -h. This brings up the help for PHP. And have a look right here, "-s runs with a built-in web server." Sounds like this is what we want. But real quick, once again, a discrepancy. If you went with something like MAMP, they will include a server for you. You probably have Apache or Nginx, and they will have instructions for how to preview.

If you went with something like MAMP, they will include a server for you. You probably have Apache or Nginx, and they will have instructions for how to preview your website in the browser. Otherwise, you'll need to boot one up yourself. And that's what I need to do here. So I can say php -S, and let's go with localhost and a port. How about 8888? All right, we have a development server running. And in fact, I should be able to command-click on this URL to access it. Otherwise, just copy it and paste it into a browser.

And in fact, I should be able to command-click on this URL to access it. Otherwise, just copy it and paste it into a browser. All right, congratulations. You've now booted up a server, and we're displaying a static piece of HTML. Great. But I want you to notice that it automatically worked. And part of that is because I named the file index. So for example, if I were to rename this file, and on phpStorm, I can go to Refactor, Rename, or there's always a shortcut. And yeah, if I changed it to anything else, well, keep in mind, as soon as I return to

Switching to PHP4:33

or there's always a shortcut. And yeah, if I changed it to anything else, well, keep in mind, as soon as I return to the browser and refresh, it won't be able to find that. So yeah, you'll often find that index is an important file name. OK, but now I want this to be dynamic. I'm assuming you already have a little experience building a static website, at least the basics. To instead make it dynamic, I'm going to switch to php. So once again, I will rename this file to index.php. Now again, notice if I don't change anything at all, because I'm running PHP's built-in web server, it'll still work just like the previous example.

Now again, notice if I don't change anything at all, because I'm running php's built-in web server, it'll still work just like the previous example. But now we have the full power of PHP at our fingertips. So let's have a look here. Let's clear out this h1 tag. And let's open up our first PHP block. We do that by typing less than sign, question mark, PHP, and then a space, and then another question mark, and a greater than sign. And I'll warn you, it's really confusing the first time you do it. Maybe the first 10 times you do it, you'll have to look at the keyboard to make sure

And I'll warn you, it's really confusing the first time you do it. Maybe the first 10 times you do it, you'll have to look at the keyboard to make sure you're hitting the right keys. But I promise after a while, you won't even think about it. OK, so now we're basically designating that whatever occurs between or inside this php block should be treated as not HTML, but php. So for example, if we just did something like this, and I once again wrote, hello world, this is not going to work. And in fact, if you have a good editor, it'll let you know, hmm, there's something going on here.

Printing Output with Echo6:01

And in fact, if you have a good editor, it'll let you know, hmm, there's something going on here. Switch back to the browser, reload it, and we get a parse error. So notice, we're trying to interpret this php, but it has no clue what this is, because it's not HTML. We have to write using the php language. So I'll show you the easiest way to simply print a string. We can do that by saying echo, and then within quotes, your string, 'hello world', and then at the very end, add a semicolon. And in fact, I just used the word string, which can be a little confusing at first.

at the very end, add a semicolon. And in fact, I just used the word string, which can be a little confusing at first. Whenever you hear that word, all I mean is basic text, a string of characters that mean exactly what you read here. They are not special symbols or identifiers that the language will understand. It's literally text. Okay. All right. So think about what's going on here. When the page loads, we display a heading level one tag, and as its text content, we

So think about what's going on here. When the page loads, we display a h1 tag, and as its text content, we open up a php block, and we echo out the string, hello world. And yeah, I know echo can be a little confusing. Think of it just like print this text onto the page. And in fact, print is something that we could use here, and it's mostly the same as echo. There's some slight differences, but still stick with echo. Most people do. All right. So if we switch back, give it a refresh, we get the exact same thing as our static version.

Practice with PHP Blocks7:24

All right. So if we switch back, give it a refresh, we get the exact same thing as our static version. But again, I hope you're excited. By definition, we are now building our first dynamic website that uses php, and we'll keep taking this further and further. So to finish up today's learning, I want you to play around with echoing different strings. Hello universe, hello town, whatever you want. Practice inserting that text in other areas of the document. Maybe add a <p> tag where you echo out some gibberish string. The entire point is to become comfortable with writing this awkward sequence of characters.

PHP TagsStringsecho

دوست دارید گاهی خبرهای Laracasts را ایمیل کنیم؟