Lesson #1: PHP Intro

A beginner's guide to writing your first PHP script. You'll set up a file, use echo to print "Hello World", and learn why semicolons matter and server-side code stays hidden from the page source.
Chapters
Welcome to Lesson 1, An Intro to PHP. In this tutorial, I'll be talking about the basics of PHP, how to get set up, and a couple of extra stuff. So first of all, you will need PHP, of course, installed on your server. For this tutorial and the next ones, I will be using PHP version 5.2. However, any other version should do just just fine. Okay, now in case you didn't know, PHP is one of the best languages to use on your website, and it's used by many major companies like Google and Facebook. But let's get started. First of all, you need a file editor. I'm using cPanel in this case that came with my hosting package. However, any other file manager that can create files, directories, and edit the codes will be acceptable. Now, first of all, we need to create a file. I set aside a directory just for our learning. Let's create it and we'll call it 1 for lesson1.php. Create new file. And we can also open up our code editor, and we can see that we have nothing in it so far because we didn't write anything. Okay, so now if we open up our directory, we can see that we have This one file, and if we open it, you'll see that we have nothing. Now, that's because we haven't produced anything in it. So to open up our tags or tell our engine that we are now using PHP, we do less than question mark PHP and then question mark greater than. Now there is a shorter form of this, which is just like that. Or no, just like that. But you need something called short tags enabled. You can find more on your phpinfo, but we're just going to go like this. Okay, so in order to write something out or print it, we do echo followed by the string that we want to print. Let's do hello world. We also end our lines with a semicolon, just like any other programming language. If we save our changes, go back to the page, and press F5, we will see that we now have Hello World. But if we view our source, you can see that it's just Hello World. This is because PHP is server-side. It doesn't— you will not see it in the source because You don't ever see the PHP. Whenever you call the page, your server will run the PHP, and PHP will create an HTML file based on the output. It's basic input and output, basics of programming. Another version of echo is print. However, studies have shown that print is indeed somewhat slower than echo, so echo is the main thing used. You may also see that it does not have a newline, but even though we have a newline here, this is because of course it prints out the HTML, so all it really sees is this And this. We can make a new line by doing— or putting in our HTML code for new line right here. And of course you can echo out p— or HTML. Just like that. That concludes our tutorial for today. I'll see you next time. I hope you find this useful, but that is all. Thanks for watching.










