back

Intro to PHP

Getting started with PHP3 H 57 M

Episodes
Episodes
  • PHP Overview and Getting Started
    • PHP Overview and Getting Started
    • Numbers, Strings and Operators
    • Conditionals
    • Arrays
    • Arrays Part 2
    • Loops
    • Functions
    • Classes and Objects
    • Dependency Management with Composer
    • Basic Error Handling

PHP Overview and Getting Started

26 M

itprotv course thumbnailitprotv course thumbnailitprotv course thumbnail
  • Episode Description
  • Transcript

In this episode, we look at a brief history of PHP and what it is used for. We then get our environment set up to begin working on our first PHP file.

[MUSIC] Greetings everyone, and welcome back to another exciting episode of DevPro TV. I'm your host Dustin Dennison and well we have a treat in store for you today. We're getting started with Intro to PHP and luckily we have someone very special in the studio who knows all about the PHP lan. Mr. Frank Corso. How are you doing today, Frank? >> I'm doing well. How are you doing today? >> I am doing fantastic Frank. And actually I'm pretty excited, I've always heard some interesting things about PHP. But before we really dive in to see if those are lies or truths, can you tell us a little bit about PHP, maybe some background there? Yeah, sure, Justin. So PHP is one of those scripting languages that a lot people hear about but aren't quite sure exactly what it's used for. Mostly PHP is used to create websites. So in the grand scheme of things, about 80% or four out of five websites you visit are built in PHP. So PHP's one of those languages that are very easy to learn and quick to pick up. So a lot of websites are using it to build, and create all kinds of useful things, everything from Facebook, to a lot of the other like Sony, and Walmart, and a lot of those sites are built using PHP technology. >> All right, well, Frank, that makes me think, well, maybe this is worth learning, but what we need to do to get [SOUND] what do we need to do to get started? I almost had a mess up there. >> Well, when it comes to PHP, the best part is that you need almost nothing to get started. So all we need is PHP itself and something to create the files in, which we can go over. So, to get started, we're gonna hop in. We're gonna jump to the laptop screen for just a moment and show off what PHP is and where to go get it. So luckily there's this website called PHP.net that has everything to do with PHP. And we're gonna revisit this as we go forward quite a few times but this is the all inclusive spot to learn everything in PHP, what to do with it, where to go, all those fun things. But to get started depending on which operating system you're using, there is a few different things you might wanna look at. So for example, today we're gonna be using XAMPP which is XAMPP@Apachefriends.org. And this is a useful tool that has everything in the LAMP stack. So what is the LAMP stack? The LAMP stack is a stack of technologies used to power most PHP websites. So for example, we're gonna look at Apache, and Apache is the server that powers the PHP framework. There is a few other servers that we're gonna be looking at but the main, we're gonna be using is Apache. There is also a database to store the data. Usually this is MySQL. This particular one, we're using MariaDB. But again it could be very, others. And then we're also gonna be using PHP. And so the LAMP, L for Linux. Most of these servers are using Linux, and today we're gonna be using Linux. A for Apache, M for usually MySQL, but today we're using MariaDB, and then P for PHP. And these installers, so if you're using Windows, you'd be looking at XAMPP, or if you're using Linux, XAMPP. If you're on Apple, you can use MAMP. It works very similarly to what we'll be going over today. So you can use either one depending on which operating system you are after. And once you have it installed, it looks something like this. And all you have to do to get things started, is to start the Apache web server. Now I've already studied both of these, the Apache web server and the database. But usually if we were to stop this, when you first get to it, you'll see a page with a whole bunch of red dots. And all you have to do is click on each one and click Start to start your development. And MAMP works very similarly the same way, there's a few extra steps involved there, but they work almost identically. Now if you want to, you can do all this from the command line or the terminal I don't recommend it. It's not the usual way for PHP. But you can do all that and not use these if you just wanna install Apache and a database and PHP on its own. Now we're gonna go ahead and turn these back on. Once you have PHP on your laptop or your computer, using something like XAMPP. Then we need a way to edit the files. So PHP in the simplest sense is just a collection of files that are used to power whatever you're building. So in these files is the language and different context, which we'll be going over shortly. So what we need is something to actually write this code, or these words, these language. There's two that you might wanna look at. These can be used on any operating system which is why I'd like to recommend these two and both are also free. And I like free. So the first one is ATOM. This one is by Behind the People, from behind GitHub. If you're not familiar with GitHub, we're gonna touch on Git a few times here and there. But it's not gonna be the focus of this. You're interested in Git, there's a great course in the video library. Intro to Git by Justin himself, that I definitely recommend you checking out. But we'll touch on that, a few points here and there throughout the lesson. But if not, I definitely recommend the course. But Adam's a great IDE. If you're not familiar with IDE, I said before we're gonna use these to create the files. So IDEs are pretty much a place where you can create the files, but a bit more than that. So you can create these files in simple Notepad, or Word technically and a few other things like that. But, you can also use these things that are even more powerful called IDEs. And what these do, is have a lot more context, they highlight different contexts which we'll go over in just a moment. And they can even show you errors that might be in your code or they can allow you to browse your different files that you're working with at the same time. And refer to those different files and a lot more. So you'll see how these are useful very shortly. But Atom is the one that you can use. This one is free, you just click the download button and get started. The one we'll be using today is Visual Code Studio. This one is by Microsoft and both of these are equally good, I would say. Visual Code Studio is a little bit faster, so that's why we're gonna be using it today to make sure you're not having to wait around for us to load Atom. But all in all, these are both great ones. I use both of these interchangeably. So definitely look at ones and see which one that you like better. So once you get one installed, if after you download and install Visual Code Studio, you might end up with something that looks a little bit like this. Without my file open. >> Quick question, Frank, but before we get too far ahead. Now, they actually already saw a glimpse here, you made a comment, I can use Notepad, or maybe you're using some sort of Text Edit, or G Edit if you're on Linux. Though you can, I wouldn't recommend it. Because we see some of the utility in these tools like Atom, and Visual Studio code or even things like Web Storm which is a little more heavy weight. I tend to stay away from those heavy weight options that are going to keep you from making mistakes as you get started. So, I would probably recommend, though you can, doesn't mean you should. Just wanted to make sure >> It's definitely a good distinction there, I probably should say, while you can do that, a lot of different tools you could use. I would definitely recommend staying with Atom or Visual Code Studio, or Visual Studio Code, just so it can check your code with you and make sure you're doing things the right way, that works. So I definitely think that's a great point. Thank you, Justin. Now, once we get our IDE open, and Atom looks very similar to this, if you were to choose Atom over this, or any of the other popular IDEs. Most of then have some sort of area for your files. So for example, on the left here, we have an intro-PHP folder open. And then it lists the files inside that folder here on the left. If we had other folders, you might see those folders there, as well. And we're going to go ahead and open one of these files just so we can take a peek at what PHP kinda goes with before we jump ahead. So when we open, here is where you would type all the PHP-relevant things. We'll be going over this in just a moment. And, inside here is where all the cool magic happens. You can code, make all your fun stuff that you wanna do. And then once you save it, you can open it using a regular web page. So we're going to come over here. So before when I talk about servers, I mentioned Apache and there's a few other servers and what they do is they take anything you would type in the php files. And I convert it in a way that get shown in a user's browser. So, if, for example, I typed in a popular site such as facebook.com, it runs to the server, and it looks at all these php code and loads it all in to the beautiful language of HTML. If you're not familiar with HTML, there is another great HTML course on the library. I definitely recommend that, not required for the PHP lessons but it's definitely something to look at. But, so PHP gets converted into this HTML and then that get sent to the user's browser, so in this case, we went to our file and all we're seeing is test because that's all I put in there so far. And that's kind of how that workflow works. So now, that we've gone over just the kind of basics of how that works, let's take a look, a little bit more what this php file is. >> Real quick, clarifying question. So we have a folder open inside of Visual Studio code and that was a folder that you made somewhere, inside of wherever we set up XAMPP, right. So we put that folder in there. We open that in Visual Studio Code, but is that folder required to be named that, or in a particular place, or something of that nature? >> That's a great question. So when you install XAMPP, it creates a folder, depending on which operating system you're on it might be named a little different, but on Linux, it was named lamp. On Windows, I wanna say it's called XAMPP and then on Mac OS if you're using MAC, it'll just be called Mamp. And inside, went to far. Inside there'll be a folder called HTDOCS and inside here, these are the folders that would get rendered out to that you can add PHP into so for example, when you first installed XAMPP, there's this dashboard folder. And if we open this, this is the info page to the XAMPP that we installed. So here's just some information about XAMPP after you've installed it. And then it'll have some various things going on that you can look into which we'll go over shortly. So if we wanted to add a new project and some new files, we can create a new folder here. So for example, I created an intro-php folder here. And any files we create today will be going inside this folder. So to get to load this file up, we went to localhost/ then the folder. So the dashboard which I just showed you, it's at local host slash dashboard. And then the file that we just looked at it's at local host slash intro dash php. And those two folders correlate to these two folders right here, dashboard and intro dash php. And inside dash intro dash php there's a file called index.php, and this will translate to index dot HTML when it got sent to the browser. And index.HTML does not need to be named here, which is why we can just do the backslash and it will load that file. If we had named this something such as test.php, then we would have to type here, test.php to load that page. Does that answer the question, Justin? >> Yeah, that makes a little more sense now. Now, I understand how we can organize our projects respectively, then navigate using the XAMPP tool, so thank you for that. >> Thank you, and again, excellent question. So as we jump in to, so now that we know how we get to that file, and how that work flow works of getting it to the browser, let's take a look at actually what that file is. So PHP, there's a few different ways you can structure all of this. But to get started the main aspect of getting into PHP are these opening and closing tags. So anything that goes on in here, the end goal is to create an HTML file that can get sent to the browser. So for example, in here we have no HTML right now. So right now it's just sending this word, test. And I'll go over this part in just a moment. And at the browser, we see a test. So theoretically, we could have just done an HTML folder here. Or an HTML, we're just gonna If I can type today. Now, normally, your ID would have a cool autocomplete for all of this to set up. I don't have that set up right this second so I'm just gonna go ahead and type this out but, Now, normally, if you're familiar with HTML, this might already make sense to you. But if not, this is a basic outline of HTML. We're not gonna go over too much HTML today, we're gonna hardly use it. But, just so you can see how this lines up, we're gonna switch this back to HTML. The goal is to create something that ends up like this that gets sense of the browser. That's the ultimate goal. And what PHP does is it does most of this for us. So instead of. Right now, this is almost identical to what we had in HTML. Now if we, Nope. Now, if we wanted to do this with PHP, Let me switch this back to HTTP file. Cool. So now, what this does is it does a very similar setup, and it sends out, whoop, Forgot to switch to the PHP. Sends out almost the same thing, that it just sends out the word text or test. And so that was the ultimate goal was to get it to HTML. Now, what we need to do is tell the Apache server, hey, this is the PHP that we're rendering and put this together, figure out what we're trying to do, and send that to the browser as HTML. And how Apache knows that we're typing in PHP or what we're doing is we start with these opening and closing tags. So, any file or any part of PHP, you're going to start with this open tag, you're going to do sum PHP, so PHP stuff. So we're going to go PHP stuff right here. And then you're gonna end it with this ending tag. Now, you can have HTML as outside of this, so for example, if the switch, I'm not gonna re-type all that now, but we're just gonna say, if this was some HTML here, and then some HTML stuff here. And now, I'll go over that part in the middle in just a moment. But now, so we can see that there's. We can see our HTML here and here, and then our PHP part right in the center. And so Apache goes through, looks for these opening and closing PHP tags and goes, I need to run this part in the middle, create that into some sort of HTML and then send all this as just HTML to the browser. And that's how that whole work flow works. So now, let's take a look at this thing in the middle here, trying to figure out what's going on here. In PHP, like I said, we're trying to get to HTML. Find a way to get that out. So there's a few different things we can do here. And we're gonna use this part quite a bit. And essentially, what this does is it prints out or echoes out some text. So in this case, we're echoing out test, so when it goes to the server, it changes this to just this or when it goes to the browser. So what echo does is it puts whatever you have in here, and we're going to go into more cool things you can do in a little bit. Or whatever you put in here will end up replacing this PHP section, sort of. So, and then so echo, just in case I didn't explain that clearly, is a way to output anything, any context, any text you want to put in there, any sort of If you want to do just some cool programming steps which we'll go over in just a moment. The goal is to convert it into some sort of text you can put it into the HTML part. So, now there's some extra things we can do here. We can do Maybe you had a lot of text you wanna put out, and you're gonna do some really cool differently coding and programming and different manipulating of these things you're gonna be putting out into the HTML. So you wanna do something a little bit more. Now, we couldn't call this a programming language if all it did was Output some text to the screen, we probably wanna do something with that, or do take its inputs, modify it, manipulate it, change some things, and then output. And to the basics of getting that started, it's something called variables. Now if you've never used any programming language before, this might be a new concept to you. But if you've used any programming language before, this is going to be a very similar set up. There's a few things different than some of the other languages, but for the most part, it's very much the same. So, if you're not familiar with variables, You can think of variables somewhat like a box. Again, each box has some value, some content. So for example, maybe Justin and I started a new e-commerce store called Justin and Frank's store, and you went on there, and you bought a camera and some coffee. So we get a box in of coffee, a box in with a camera in it. So here comes box one, I open up there is a camera, here comes box two, there is some coffee in it. So box one, inside box one is camera, inside box two is coffee. And we can rearrange these things, we can manipulate, we can add the coffee to the camera. We can add a new thing if I wanted to get a microphone and toss it into the camera box. I can do a lot of things with these boxes and variables are a very similar concept whereas there's a box, it contains some things. We can things out of that box, we can take the camera out, get out a new thing in it such as a microphone. We can divide it, maybe the coffee we're going to divide it into two separate boxes. We can do a lot of things with this but at the end of the day, it's contents within a box. Does that make sense, Justin? >> It does. I like to think of variables as like, there's a way of assigning a name, for being identifiable. So I don't have to worry about, I need you to give me that string that has 600 words in it, that describes this other thing, or being very specific like that. It's just, let me grab that box, let me get what's out of it, do some stuff with it, maybe put it back, maybe put it in another box. Who knows? So yeah but here's your thing and said kind a crap up as you learn programming and whether it be PHP or any language. >> Yeah, definitely. And if this doesn't make sense, once we start actually using this, I'm sure you'll pick it right back up. But so for example let's go ahead and use of these variables that we created. So what we're gonna do, we want to say hey, we want send camera and coffee to the user HTML speaking, we not actually going to send them a camera. We're going to use our friend echo again, instead of going echo camera, like we did before echo test, we can send the actual variable. In this case, we still have our camera but we're going to put it inside this box and we're going to send the box to the user. And we're going to do box two as well, just so we can see what that looks like. So we're going to come over here, and we're going to see that the test turned to, actually let me make that a little bit bigger here. We're going to see that test turned to cameracoffee. And you'll notice that that's not on separate lines or there's no space there or there's nothing really. Like, we see here that there's two separate lines, and maybe some space here. Well, PHP doesn't see any of that. PHP just makes this a simple text that it sends to the browser. So essentially, what we're doing here, what it's sending to the browser is cameracoffee is what's being sent to the browser. And that's something to remember. So if we wanted a space here, we would have to add a space inside is variable, or in the next lesson, we'll probably have some ways that we can manipulate this text to add in very different spacing, maybe new lines, things along those lines. But I think I think this is a great place to wrap up what would be called a PHP overview. >> Well I've got a really quick question about that. Actually, had a couple of questions. >> Okay. >> So even though PHP, it seems to be geared toward the web. We're not necessarily limited to just accomplishing web things with PHP even though that's it's main goal. Am I correct in that assumption or am I way off there? >> Well, in the grand scheme of things PHP could be used for a lot of things. In reality, it's almost always used website related, but you could technically, you could run this through the command line. You could have some sort of analysis, you could use files, you could do a lot of things, completely away from the web. But there's usually better languages and tool suited for that purpose. PHP is usually, it's optimized and well suited for website technologies such usually what it's used for. But you could definitely use this for a variety of different projects. >> All right so, I have a follow up question about some syntax. Yeah Darren, the previous screen if we could just take a look back here, your code here. There's echo box, right? >> Yes. >> And then echo box two but there's a semicolon after box but not after box two. Is that, is PHP okay >> So- >> But is it fairly lenient in the use of semicolons in that capacity? >> No. >> Okay. >> And we just got lucky- >> [LAUGH [CROSSTALK] >> Okay. >> When it comes to syntax, if you're not familiar with programming language. Syntax is how the programming language expects you to type things down. And in this case, we only had a couple of lines so I didn't want to dive to far into the concept, but the semi colon probably something we should go over here. So before we said hey camera is inside this box. And a term here that we're going to use a lot later on is assigning. We're going to assign a value to the variable. In this case, we're going to say, the camera's inside the box, and coffee's in box 2, and every time you finish, in PHP terms, a statement, or a loop, or things along those lines, we have to end it with a semicolon, so PHP knows, this is a complete thought. So camera's inside box, end complete thought, so there's a semicolon. Coffee's in the second box, that's a complete thought, so we end in a semicolon. The only exception, this is why we got lucky here, is the echo. Because when it's in PHP, or when it's in HTML, a lot of times you're doing something like this Which is not usually the way we would normally use PHP anymore. We usually use templates and all this fun stuff. But you could do this right inside HTML. So they dropped the semicolon because you do this quite a bit. So in the echo, and only the echo normally, if you leave off a semicolon it'll still work. But normally, it's best practice to still use the semicolon. That way you're just used to it and everything's consistent. But in this case, we could leave it off if we wanted to. >> So we're not chasing crazy syntax errors really. I don't know why this isn't working, you forgot a semicolon. >> Followup question on syntax- >> Sure. >> Not to get too deep into it, do all of my variables need to start with a dollar sign? >> Another good point that I probably should have mentioned. Yes, so when I said before a lot of different languages have variables but they're slightly different. So if anyone's familiar with JavaScript, I'm not gonna type out the whole thing. But you might have encountered jump = 1, something simple like that where it's a var keyword. And then you can name it whatever you want. Python is very similar, that you can name a variable whatever you want usually. PHP's usually the same way. You can name it almost whatever you want, letters and numbers. But you always have to start it with this $ figure. And that's how PHP knows, hey this is a variable that's coming up. So in this case we say $box. If we did something like box_3 equals tablet you're going to get an error. It's going to go hey, I don't know what the heck you're doing. Actually, let's see if we can make that error show up. There we go, look at that beautiful error. I love these things. So this is, Justin mentioned it a second ago about a syntax error and what that is, is that hey there's something in the actual way you've typed your PHP normally. In this case, it's saying unexpected equal sign. Well because right here is our equal sign. And it's saying I don't quite know what box_3 is and why we're trying to put a tablet in it. Because right now PHP is just saying box_3 is something else we're gonna go over vonstance later on But essentially, to make this a variable, make sure I'm on the right line, we need to add this $ so that PHP knows this is a variable that we're putting the tab within to. So now we'll see I'm not even echoing out, if we echoed out, box_3- >> So, dollar sign is a way of denoting a variable, like var in JavaScript, or some of the data types in other strongly typed languages, things like that. So I just have to make sure I use it whenever I'm actually declaring a variable, right? Exactly Jess, and all we have to do is just add that dollar sign, and now box three is recognized as a variable. And we can put the tablet in it, and you're good to go. >> All right, I am starting to see how this will come together. And being someone who has struggled a great deal with trying to template out some web stuff. I'm interested to see how PHP comes along. And hopefully you are as well. But we got more DevPro coming at you. But for this episode, we're gonna go ahead and finish it up. So signing off for DevProTV, I've been your host, Justin Dennison. >> And I've been Frank Corsell. >> And we'll see you next time. [MUSIC]

Just you? Training a whole team? There's an ITProTV plan that fits.

With more than 5,800 hours of engaging video training for IT professionals, you'll find the courses you and your team need to stay current and get the latest certifications.