PHP in action | Laurence Svekis | Skillshare
Play Speed
  • 0.5x
  • 1x (Normal)
  • 1.25x
  • 1.5x
  • 2x
16 Lessons (1h 20m)
    • 1. PHPinAction

    • 2. 1 PHP in action introduction

    • 3. 2 Setting up your PHP environment get ready to code

    • 4. 3 HTML and PHP together

    • 5. 5 echo vs print

    • 6. 6 PHP number Types

    • 7. 7 PHP In reference to

    • 8. 8 PHP get variable information and type

    • 9. 9 PHP troubleshooting and errors

    • 10. 10 Include vs Require PHP

    • 11. 12 Server Variables

    • 12. 13 PHP delete and destroy

    • 13. 14 Power of PHP arrays

    • 14. 16 Check if variable exists PHP

    • 15. 17 PHP String Functions

    • 16. 18 PHP Implodng and Exploding Arrays and Strings


About This Class

Explore PHP learn about common PHP functionality for better coding.

Review with examples of common PHP questions.  Build upon the basics of PHP with this next steps course to help you learn more about what PHP can do and how you can create your own applications.  Experience the power of PHP

Explore PHP, within the course we demonstrate some of the most commonly used PHP code.  Course centers around common mistakes that beginners make and useful functionality to help you code more productively.

This fast paced course includes all the source code used within the course, in addition to fully explained examples, and links to the top resources to get you coding quickly.  Jump right and and start writing PHP code.  The course is taught by a web developer with over 15 years of PHP coding experience.   Find answers to common questions about PHP.  Learn to write PHP for the real world!

  • See PHP in action and explore different ways to output content into HTML for web users.
  • Discover the difference between echo and print and how to use them
  • Examples of different number types
  • Var dump for more information about variables
  • Common errors and how to debug them
  • Server variables, includes, deleting files and variables
  • Working with arrays
  • The power of string functions
  • Plus a whole lot more

PHP is a perfect scripting language for beginners, learn how to use it and see it in action.  Examples and script demonstrations to get you started quickly.  

Learn more about PHP, I'm ready to answer any questions you may have.  Start creating your own PHP applications today.


1. PHPinAction: explore PHP, learn about common PHP functionality. For better coding, build upon the basics of PHP with the next steps course to help you learn more about PHP and what it can do and how you can create your own. PHP Applications. Experience the power of PHP. Explore PHP. Within this course, we demonstrate some of the was commonly used PHP code snippets course centers around common mistakes that beginners make a useful technology to help you code more productively. This fast paced course includes all of the source code used within the course We have example, samples, links to all the talk resources and everything you need. In order to get started quickly, jump right in and start writing your own PHP code. This course is taught by developer with over 15 years of PHP coding experience. Find answers to common questions about PSB. Learned to write PHP for the real world. C PHP Annex in action and explore different ways. Output content into HTML for Web users discover the difference between echo and print examples of different number types, bar dumps of information on variables, common errors and how to debug them. Server variables includes deleting files, deleting variables. Working with the rays and the power of string functions and a whole lot more PHP is the perfect scripting language for beginners. Learn how you use it and see it in action examples and script demonstrations to get you started quickly. Learn more about PHP. I'm ready to answer any questions you may have start creating your own PHP applications today. 2. 1 PHP in action introduction: welcome to our course on PHP in action. This course is designed for beginners who want to know more about using common PHP code seeing common PHP code and its functionality in action and in general, getting to know PHP better. My name is Lawrence, and I'm so excited to have the opportunity to present this course to you today. I've been developing PHP applications since 2002 and I really, really enjoy developing PHP because PHP really provides so much flexibility for the developer. It gives you a lot of opportunity to develop amazing applications from scratch. And there's so much flexibility with PHP. And it's one of those things that once you start quoting and PHP, you're not gonna want to put it down because your imagination is the only limit you have when you start coding in PHP. So I again, I'm so excited to have this opportunity to present PHP to you within this course. This course is going to be taking you through some of the really cool and amazing functionality. It's available within PHP. We're gonna look at different ways of out putting content into HTML. We're gonna look at differences between Echo and print number types, all the different number types, how to use references. And PHP is more about variables. Error types and Ph peas includes versus requires. There's a lot of that those common questions that we get on a regular basis within PHP, and I'm gonna be showing you and taking you through all of this information on a step by step basis. Eso again showing you different functionality that's available within PHP that's used on a regular basis. Such a server variables how we can delete files and variables within PHP how we can count and utilize a raise within PHP, even Mawr on variables in PHP and then one of my favorite things with PHP are string functions. A lot of really cool things you can do with string functions, and we're gonna be showing you all of those and a whole lot more within this course. So when you're ready, join me and let's start creating our own PHP applications from scratch. All of the source code is included as well as I've got resources, how you, too, can set up your development environment just like I have within the course, and I do encourage you Teoh to work alongside the code within the course and practice it. Try it out for yourself. Experiment, Explore PHP and really get to know PHP because guaranteed you're gonna love it. S o P H p is one of those languages that continuously expands. And as more and more people begin to use it, there's more and more growth within PHP and ah, lot more opportunities with PHP development. And a lot of the main sites are already on PHP. CMS systems like WordPress and so on are all using PHP. Eso made the right choice looking at PHP And now let's join me and start creating some PHP code from scratch. 3. 2 Setting up your PHP environment get ready to code: in this lesson, I'm going to be showing you the different resources that I'm gonna be using throughout the course in order to write my PHP code and create the lessons for this course. So one of the resources that amusing and one of the resources that you're gonna need tohave in order to write PHP code is an editor and editor that I'm gonna be using within this course is called Adam. Asus is a free editor that's downloadable at adam dot io, and it's really versatile. Editor works Cross Platforms has a lot of really good packages and does a lot of really cool things. Eso This is one of the others that have been using more and more lately, and I really find it to be a great editor. So if you are looking for an editor to write code with, I would suggest checking out Adam. Or if you're looking for something you to try out. Adam's a really good options. So I've got Adam open over here and with Adam, everything is contained within the settings here so we can update the font size. We can pick up additional packages themes, see updates, install additional information and so on. So really easy to do and update and configure to make it look the way that you want it to look eso That's Adam. Another resource that I'm gonna be using is gonna be local machines. So that gives inability to go to local host and output PHP code. So I'm just going over to the PHP folder and not contains this index dot PHP file. And within this file, this is where I can write content. I can go back out, refresh it and see it displayed within my browser. And, of course, we do need to have a server to render out PHP code eso the server that amusing. It's really easy and simple, one to set up. It's probably one of the quickest ones to get going with, so this is called Zampa. It's available at Apache friends dot org's cross platform, So windows Lennix os X. Ah, and it's really quick and easy to install. You just need to click through, so select the version that you're looking for and download it for their operating system and then install it. And once you get it up and running, you're gonna start it with a control panel. So this is the basic control panel, and all you have to do is start the patchy server. If you've got a database, you can start my sequel as well. If you're looking Teoh, run an FTP server. It's able to do that as well. So you can start up any one of these If you need to configure. It s so right now, what I'm doing is I'm actually not using the default directory. So I've got a folder that I've got in my K drive where I've got websites PHP. And this is the folder that I want to use as my route. So I've made some updates to the config file us. You go to Apache config. It opens up. So I've said it to open up in note pad and I've updated where my root directory is. And that summer around line 2 50 some somewhere between 2 46 2 to 48. And there's these two eso There's the document root and there's the directory. So just update those Teoh whatever directory you want to use. If you want to use one, that's not the standard one. If you just go ahead and install Samp by default. It's gonna be looking at the sea on a Windows machine. It's gonna be see Zampa ht docks. That's gonna be the root. So all I've done is I've just hash those out and I updated it and created new paths to the root directory, and that gives me the ability to run. That particular folder that I'm on right now says is my PHP folder, and I've got my index file running here as the root file, and that's why I'm able to go to local host and see that PHP dot index dot PHP file. So same thing here index dot PHP and it lets me run that file directory as typing in local host. Ah, and that's it for the set up. And you're gonna need to set up something similar in order to follow along with in this course. And I do highly advise that you try out the PHP code for yourself and very do some variations of it. Play around with the variables, make some updates, make some changes and see what happens. The best way to learn PHP is to practice and setting up your environment so you're all ready to go to practice. PHP is a good place to start. So with downloading these two Adam and Zampa, you should be in the same environment as me and up and ready and running to go to start creating some PHP code. So the next lesson we're gonna dive right in to PHP, and I'm gonna show you some really cool functionality and help you get to know PHP a little bit better. So that's coming up in the next set of lessons. 4. 3 HTML and PHP together: So now that we're up and running and ready to code, let's jump in and start creating some PHP code. The purpose of this course is just to show you some really common functionality and PHP some really cool stuff you can do with PHP and allow you to better know and understand what you can do with PHP code. The first thing that we're gonna be looking at and this is a very common thing that people are looking to do, and that is output your PHP code. So it's actually visible within your HTML. So a lot of times you'll see that we create such as this on index dot PHP. So if we've got the extension PHP than if you do have a server running that's got PHP installed than your server's gonna no, to render this out as a PHP file and it's gonna no to run all of the code that's contained within here and often times Just see that, uh, PHP code and then you've got your HTML codes. May might have HTML code down here open and close, and you've got your head and body and so on. So I'm just going to write some quick html here and ah, body open. And then lastly, closing off that body eso maybe you've got an introduction page. So welcome, Teoh my website and then boom, You want to add in some PHP code? So I'm gonna show you several different ways to do that. And of course, there's different ways to output content. Eso you can render it out as a template and so on. So this is a very rudimentary way, Teoh Output some html code. So let's say you had a variable and I'm just gonna call this variable Hello and we'll do a standard Hello world. So now we've got a variable Hello containing the string value of hello world and we actually want out Put it into our website. So how do you go about doing that? So let's just quickly take a look and pop over to our local host. Look at the source code there and of course, always see is the HTML so we don't actually see that PHP And we do want to render out that PHP variable and probably we want to do something more with that PHP variable it. This is just to simulate how you can get from PHP variables. And I put that court so there's a number of ways to do that. And one of them would be just to incorporate that PHP directly within your code So you could do something like this where you can open up the p X p and eco outs, echo out hello, just like that and then close that off so that processor again knows that the PHP code is finished s so we can go out here and we can refresh it. And boom, There we go. We see Hello. So another way to do that. So there's a few shorthand ways of doing it. And the shorthand methods don't always work on every PHP eso depending on what your PHP I and I file allows you to do and how it can interpret PHP code. The short format may or may not work, and the benefit of the short format is it just looks a whole lot neater within your output code. So it's jumped back into here and I'll show you what I mean. So we had one example of it, and for shorthand, you can do something like this where you've got the question mark and you're skipping out that PHP. So everything else is the same. And then this is the shorthand code, and I guess I forgot to say that. So go back out and refresh it, and we can see here within the source. So what's happening here is that we're echoing out Hello, but it's not actually being read out. So let's take a look at this code a little bit closer, and we noticed that this particular server is not rendering out that code s. So this is where we would need to go into our PHP. I and I file eso that's located just over here. So I've just opened that up and there's going to be somewhere in here. We're gonna look for ah Command called short, open tag. So by default, it's on, and we can set that to be either on or off to equal on or off. So just find next. And we noticed that this right now it's off. So let's just turn that on. Save that and we probably need to restart our server, So just stop and start. And now let's try this again when I refresh the page. We see that hello world is working and that we're able to actually use shorthand within our PHP code. So we didn't change anything within the code and notice that we are able toe would put that value. So this is essentially what you can do. You can update your PHP I and I file and you can enable that short, open tag line. So on mine, it was located in lying to 02 And simply by enabling that gives me the ability due to the shorthand. So again, depending on what your PHP I and I file enables you to do is where you're able to go with the shorthand. So there's another way. Actually, toe put that content as well. And again, this only Brill benefit is it just looks a whole lot neater. So instead of echoing, we can just do an equal sign and weaken, remove some of that spacing there and refresh it and we see that this also outputs the same variable value. And again, this is just shorter. Niedere and some developers will choose to do it within this type of shorthand format eso a lot of several different ways to accomplish the same thing, and it just it just flows a lot better within that HTML. And that's why you'll see, a lot of times developers will do it this way. So another thing that you might want to do within your your source code of your HTML is maybe you want to render out something depending on a condition. So this is something that is often done with it. PHP. So again, we're gonna open up that condition and we're gonna put the condition in here. And so what we're gonna see is if hello exists then and closing that off and we're just gonna do a cool in here eso if that hello exists than what we're gonna do is we're gonna So I noticed that this is gonna be html code. So hello, is there close off that and also open up ph piece or type in some more PHP code. And all we're doing here is we're doing else and notice the coal in there. And maybe for this one trips Hello is go on. And then what we need to do, we need to close this off so again, opening up that PHP and we need to add that. And if there so and if so, that it knows that if statement now is closed off. So let's go take a look and see what happens with their code now. So we see that hello is there. And if maybe we change this to hello A or something like that. So we're gonna probably throw an error there. But we noticed that the output still is. Hello is gone because hello, A is not defined variable. So it's an undefined variable. So it comes back as false instead of true, which we had with hello. And we get the else condition firing off here. And that's how you can fairly neatly place PHP code within your HTML. And of course, optimal way is to try to keep as much of it separated out from your HTML as possible to really render it out nice and neatly within your source files. But of course, there are cases where you're going to need to place it within your HTML code, and this would be a way to go about doing that in a nice, neat fashion. So the next lesson we're gonna look at the difference between echo and print. So for the previous lesson we've been echoing out are variable values. And for those of you that are familiar with PHP, you might know that you can also print out those variables and those values. So we're gonna be taking a look at what the differences between the two of those and that's coming up in the next lesson. 5. 5 echo vs print: in this lesson, we're gonna be taken closer. Look at echo and print. Essentially, these are two ways that we can output PHP values into our web browser and there is actually a difference between the two of them. So for the most part, if you want to output some content, you conduce sprint. You can use echo and print actually has two ways of doing it. You can do it with the the rounded brackets around print as well. So let's go take a look at these. So we're gonna use that same variable from last time, and I'm gonna echo out that value so echoed hello and print. Hello. And I'm gonna add in some html here, so we have a line break. Let's go take a look and see what the difference is. So nothing. There is actually no difference. It renders out in the same way. Looks the exact same way whether you're using print or whether we're using echo. Now let's take a look closer. Look at what we're doing here and we can see that print actually enables us to print eso. It returns a value of one eso it print enables us to enables itself to be used within expressions. And so this is the difference between print and echo. We've got an expression here, So simple expression. So we've got B. And if b is true, then we're printing out. True. And if B is false, we're printing out false eso It won't work if we do the echo so we won't be able to print out True with Echo and true with false Let's try that out here within our, uh, within our editor. So we're gonna do be and we're going to see if B is true. Then we're gonna print out true. And if it's not, then we're gonna use print and we're gonna do false. So essentially, that's this value is going to get printed out if it's true or if it's false, and then now we're going to do another one here and we're gonna try to use eco. So first of all, let's comment that one out and refresh it and we see that we get that output of false And of course we do throw that variable. There s so we do need to find that variable. If we don't want to throw an error so we can just do be equals blank. And so there we go that we've got false. So the value for B is not true. But what happens if we try to do it this way? If we try to echo should be echo s. So what happens if instead of print, we use Echo? Let's check out what happens now. So in a refresh it we get unexpected echo in lowing seven. So we're not able to use it within this type of format because of the fact that it's an expression and print still returns that value of one where as Eco does not, and that's why we can't use it within that argument and actually expect an output. So if we actually had a value for for B, we could see that it comes back as true. So again, just to show you that the output does show as true. So that's the big difference between print and echo eso, depending on how you like to use it, how you like to you use your code, you can do either one if you just simply want to output that content. But if you do want to use it with an expression. Make sure that you're using print and not echo. So the next lesson we're gonna look at number types within PHP. There's a number of different types of numbers eso predominately were using decimals and negatives. Eso There's a few other ones that you should be aware of and you're gonna occasionally find them coming up with in PHP code. So it's always good to be aware of what they are and how to spot those. So that's coming up in the next lesson. 6. 6 PHP number Types: in this lesson, we're gonna explore the different number types used within PHP. So as mentioned that commonly you're going to see decimal numbers. You're also going to see negative numbers, and occasionally you're going to see something else. So there's Octel numbers. Octel numbers start with a zero eso. This this one isn't equivalent to 123. Just because it's got a zero in front of it. It actually is equivalent to 83. There's Hexi decimal numbers. Eso These are numbers and their combination off. They also have letters in them as well. So I believe going up T e S O. That gives us a full spectrum of 16 characters per for number here. And then there's also beina ring numbers, So binary numbers are a combination of zeros and ones, and this gives us the ability to create an equivalent of 255. Ah, and then this is how it's represented in as a variable. So let's also take a look at what PHP has within its output. So it's got all the different numbers there that we just went through. Eso decimal numbers are represented 1 to 9 or 0 to 9 accidents. Ismael start out with a zero than they have a character. 0298 f or uppercase eight f So that doesn't matter. We can use either one. We can also use OC tolls Eso Octel czar represented with a zero and then it 0 to 7 and then binaries are zero b and then plus the bite, binary value and the Binali number. And then with integers eso These aren't integers. Integers can be hexi decimal. They can be octo, they can be binary and they can be decimal. So just to get a better idea of it, they've got a few examples over here and with these types of numbers as well, you can do mathematical equations. So let's set some of these numbers up here and give it a shot. So if we want to set on Octel number So the example had this as 83 and let's do be as five . So if we echo out a minus bi So what do you think that answer here is gonna be when we're out putting that values? Let's go into our local host. Refresh it and we see we get a number of 78. That's because that number now is represented as an Octel number. And so we're doing a combination here where we've got decimals. Eso this is obviously just a five. And then this represents 83. So we've got essentially 83 here minus five, and were returning back that value of 78. So, as you can see, the numbers are interchangeable. Eso let's say we had a hexi decimal numbers I'm going to do see and let's do hex decimal number. So this was represented by zero X and we've got one a eso what happens if we just put that value? So if we want to see what what that hex sensible number looks like We see that looks like 26. And of course, now we can use it interchangeably so we could subtract five from 26 get a value of 21. So it is good to be familiar with these numbers now. Obviously, a lot of times you're not gonna be counting binary and counting hexi decimal or octo numbers, but there are cases where you would need to use them within your code. Eso it's good to be aware that they are available. They are interchangeable, and you can use them within your formulas as well to come out with with values so they can use different operators, and you can pull those values out. Eso If you do want to use them, make sure that you format it within this type of format so that you could properly call to them within your PHP code. And I do. I do advise you check it out to try it out for yourself and see what you can make happen with your PHP code. So the next lesson we're gonna look at how we can reference variables into other variables and gonna show you some examples of how to do that in the upcoming lesson, So that's coming up. 7. 7 PHP In reference to: So this lesson we're gonna look at PHP and referencing variables within other variables. So essentially, what that means is that if we reference a variable so just like in this example, and I'm going to go through this within the code as well So we can see here that the and sign references a and to be. And then when we add two plus B, we get a result of 21 and then as we update a So we've got initially a starts as one, and we had 22 to a. Then when we echo out B, we get the value of B so to and then 1 22 sui notice that even though be, we haven't changed anything within B, we actually get a different output value for B. And that's how we use references. So if we want variable, that's constantly changing, and we want to have access to those new values as they update, then we can use referencing. So let's take a look at that within our editor. So opening that up and first of all, let's set some values will give, uh, a value off. Hello and we'll give be a value of five and in this case will give see value of maybe we can reference we can reference be within C So how we do the referencing we still have to set the operator as the equal to and we use the and sign eso I want a reference be in there . So when be increases, then we're going to get a different value for C. And now let's just put see? So take a look and see what that looks like. I'm also gonna add in a line break there, So some html and then now what I'm gonna do is I'm got be and I'm gonna add to be, so I'm gonna add Or maybe we'll add. So maybe we'll add a and then to a will add world, and then just copy this line out. So what do you think is gonna happen here s O Do you think the first line here? So I know I didn't use B, but I got to use that in the upcoming example as well. So let's take a quick look and see how this renders we refresh the page and looking in the source. We see we've got hello and hello world. So let's run through this. I run through this again. So initially we set a as hello and see we referenced as a value off of a So we use that and per signed. So if I take that away and I refresh it, we see that we don't have any change here when we change the value of a But when we do use the referencing and we add that value to a and now becomes the value of C so we can do that as well with be so if you want to add in B into that as well. So let's, um let's move, be around here and I'm gonna add in D So with d we're gonna reference be so you can't actually double reference eso We're just gonna reference at one time there. And then maybe what we'll do is will increment be over here? That's I'd be into our output so just added in there because we are used in the double quotes, so we should be able to get the value of B, go back out and refresh it. So now we've got both of them are referenced, so we've got Hello. And when we add world to a we get hello world and we see that when we increments be, we get on output off. Initially we get an output of five here and then we'll put it again. We get a value of six And that's what you use referencing within PHP and to summarize essentially, when we reference, it means that when we make a change to the referenced variable than we're also going to change that new variable as well. Eso Next. We're gonna look at variable information on how we can pull out some information about that variable using VAR dump. So that's coming up in the next lesson. 8. 8 PHP get variable information and type: in this lesson, we're gonna look at variable information and how he can pull that information and take a closer look at it using VAR dump. So far, dump is very useful function in PHP and essentially allows you to dump out that information about the variable. So if you've got a variable that you're using and you want to find out maybe the type of it and I'll show you how we can use var dump So far, dump is a function and built in functions in PHP. So I'm gonna var dump out a and I'm also gonna var dump it be and it will take a closer look at actually what comes out in the output. So we see that the first bar dump. So maybe I can refresh it here and this is in the source code for index dot PHP And we see that initially we've got a string So the string length is five and we've got five characters in there. So it's ah five character string and next we've got an integer and the value of the integer is five. And that's what we're seeing Get out! Put their within the var dump so really cool way to actually see some additional information that's contained within variables, and it can also be useful when you're trying to find out more information. You've got dynamic variables, and maybe you lose track if it's an integer or if it's a string s. So this is a great way to tell. There's also a raise within PHP. Now we know that we can print out a raise within PHP. But let's say, for instance, we want, actually some more information about the structure of that content. So let's do this one here and let's add in C and refresh it and we see that now the var dump. So I'm using the source files there because it's listing at all on the same role. Eso here within the source code, we can actually see the output and as it's being output from the PHP. So you see, we've got an array, so the initial variable type is an array. It's got two items within the array and as we know a race start at zero. So the first item is at zero, and the interesting thing here is that all the items within the array also get their values dumped out so we can get even mawr information in regards to those items that are contained within that rate. And this is so super useful. If I had a whole bunch of items within an array, I can really easily dump that eight and pull back some more information in regards to that . Now there's some or information at PHP dot net in regards to var dump so you can see that there's a bunch of different information here, and some she's doing the same thing where it's out putting that content, showing you what it's gonna look like, how you can var dump it out and we see it's got some examples here. So if we've got a float or if we've got ah Boolean value so it will output those values as well when were out putting out that Bardem so again very useful to find it the type of variable that we're looking at and get some additional variable information. So throughout the course earlier on, we saw that we saw some errors within our PHP code, and there are three main types of errors within PHP, and we're going to take a closer look at those and how they get output in the upcoming lesson. 9. 9 PHP troubleshooting and errors: when it comes to PHP. These errors, even if we don't like them, they're a great way to find out what actually happened within our code. So there are three different types of errors in PHP. There's notices. So these air simple there non critical errors. And essentially they're just giving you some additional information. So maybe if you're accessing and undefined variable. And we saw that earlier when we were trying to echo out Ah, value that didn't exist. So if we had something like this and we echo out a, then we go over to our local host and we see we've got this notice air. So the interesting thing about this one is that the court will continue to execute out eso We see that the cold will write out the next line. Even though we're throwing an error, it still continues on, and it doesn't actually interrupt the script. Execution s so this is the lightest of the errors. And of course, if you do see these errors, it is a good idea to take a closer look and try to fix those. Ah, but they're not critical to the execution of the script. So it's still executed. Next, our warnings. So these arm or important than notices And for example, if ah, file that you're trying to include, If that doesn't exist, then you're going to get a warning message. So if you're doing something like include, and you're referencing a file that does not exist, then that will through an error. But luckily, it's not going to stop the execution s. We've got that warning air, their file or directory. And the really good thing about errors is it actually gives you some more information in regards to what's causing that air. So this is a great place to start if you're throwing these errors. If your script isn't working properly, then look at these errors. Read them over and they're very self explanatory. So no such file or directory in and gives us the line number. It gives us the file that's throwing the air. So great way to troubleshoot and find out how to fix that air. And it's really good because you've got the line numbers there. Just go over to the line number here. We've got the variable here. We've got the file that doesn't exist and were able to continue with the script. Lastly, we've got a fatal error and this is if we're requiring a file that doesn't exist or an object that doesn't exist. And this when we're using, require and we're gonna look at include and require in an upcoming lesson. So I'm not gonna talk too much about that at the moment. But require is gonna actually force us to try to actually find that file. And if we do throw that air than we're not gonna actually be able to continue with the script. So let's check out what happens now. So we've got a fatal error require, and we noticed that after the 1st 1 we got hello. After the 2nd 1 we got hello. And lastly, fatal error stopped execution the script and were no longer able to proceed. So these are the most crucial errors, of course, because if you're running a script, you don't want to encounter any of these errors because this could throw off quite a bit of what you're doing with in your code. And especially since you're not able to to output any additional court eso here within PHP error reporting, we can actually turn off some of these errors. There's a way to turn off all the errors. You can do error reporting zero or you can do simple errors. So just so you brief example. So if we change air reporting to zero, go back over here and we don't actually see that errors. But those errors are still preventing. The rest of our script executes. We've got one Hello to hello. And once we hit that fatal error were not able to see that output. So sometimes it is good to turn off your errors on because you might not want to see users coming in if you're missing a variable. If you're missing a path you might not want your Web visitors to see this information s. So this is something to keep in mind with errors that you do have some control on how they get output. You can change it back to see all the errors, which is the default. According to my PHP, I and I file eso error reporting and we've got all eso again going into my server here, going at a PHP. I and I and I can search for that error reporting and we see that by default within PHP I and I, I've got e all which basically means that we're showing all the errors, So there's two places where you can essentially turn that off. You can turn it off in the PHP I and I file. Or you can just place it at the top here code, as I've done here, and you can remove it, those errors. But if you are troubleshooting or if you are in if you are developing your code than the errors are, great way Teoh be able to find out more information about what's happening within that code and be able to deal with those issues. So, as promised in the upcoming lesson, I'm gonna talk more about include versus require. So initially they look like they do the same thing. But as we saw with the errors, and that's a hint to what we're gonna be talking about in the next lesson, as we saw with the errors, they're not actually performing the same way, so that's coming up in the next lesson. 10. 10 Include vs Require PHP: So oftentimes you get this question. What's the difference between include and require? Essentially include gives us a way to pull a file in Teoh our current PHP file and utilize that So this is really useful. Especially when maybe you're building out a template and you've got ah, header. You've got a footer. You want to include those? So, in the last lesson, I gave you a little bit of a spoiler alert as to what's gonna happen and what's gonna be the difference between include and header Still, it's or include and require. Let's do it this way. So we've got a header, we've got a footer and that's going to create a new file and I'll just give it. I just call it header and I'll create another new file and I'm gonna call this one footer. And now when I go back Teoh here I can echo outs, some content. So this is a very basic template that we might be using for a website. When I refresh it, I can see my source code there. I've got my header. I've got my content and what happened here with Footer and I didn't have any content in there. That's why nothing is being put there. So refresh it. It's a very basic way to create a Web template. And we can see how useful utilizing include and require can be eso. The only difference, really is between include and require. They do the same thing. They allow you to bring in external files and act as if they were right within your current file. So even if you did something like you had some PHP so get me give you another example and so my favorite thing to write Hello world. And so now I'm calling in that header so I can echo out content and let's start eco I would a eso what do you think? And I almost made a mistake there by going with the single quotes because then we would truly just echo out dollar sign A s. So now we're gonna echo out the value of a So do you think that we're able to echo out were ableto output out that value of a And if you said yes, we are able to output out that value of a because when we include or when we require that file runs as if that cold was just sitting right above instead of the include that we had all of this code instead over here and accept that we'd obviously we wouldn't be breaking out of the PHP and just rating header. Then we throw some errors. So again, the difference between these are that if those files don't actually exist, then we actually run into a fatal error with require. And generally you want to try to avoid that, especially when you're building out a template. So try not to use, require, try to use, include. And this will allow you to Even if you've moved some of your files around or if you accidentally deleted them, you're still gonna be able to render out your code. Although your website will look much different because you're gonna be missing the there, the foot or the head, or whatever fall you're trying to pull it. Ah, and then you should be able to figure out at that point if there's any issues with your code and make the adjustments and updates then and hopefully it's you still catch it within the development process. And at that point you've got your errors. They're showing up and you can take a closer look at what's throwing those errors. It's always tried to take include over require if it's a file that is just being output. But if it's actual file that you need in order to run your script to avoid a bunch of other errors than use require. So the next lesson we're gonna look at some server variables and server variables could be really useful within your PHP script. You can really utilize that information and make use of it within your code, show you how in the upcoming lesson. 11. 12 Server Variables: in this lesson, we're gonna be looking at server variables. So this particular one gets the contents of the host header from the current request and outputs it within our Web page. Let's take a look and see how this is gonna work. S opening up our editor to echo and the server variable and the variable that we're gonna look for is the http underscore host. So essentially, that just means we're gonna output the domain, so will refresh it. We see we've got the domain there, and there's a whole bunch of server variables that you can make use off. We see that we've got some that have been updated and removed on the different versions of PHP. So again, boo, always mindful of the version of PHP that you're on and check to make sure that the values and the variables and the functions that you're trying to access are also available s we can do things like we can get. We can get some host information, we can get the remote address, so that gives us the I p address of the host. That's another really useful one. Especially if you're making an application and you want to make sure that the Web user, maybe you want check to see if the Web users the same Web user. You can use the i P address to see if they're coming from the same i p or you can utilize it when you've got a log in system. Just keeping track of all of that, making sure that people are abusing your system. There is a whole bunch of registrations coming from the same I P address and so on. There's remote host, remote user Uh, just a lot of really good useful files here. So it is worthwhile Teoh just to take. So maybe we can try a request. You are I as well. So I'm just gonna cheat here a little bit. And I, you know, put that request you are I And let's see what that looks like. Essentially, we get that full address there because the request you are I that's going to be indexed up PHP and the server http host is the local host or the domain that we're sitting on s so that way we can get that information. And of course, this is all useful tracking users. And so on. So we've got ah whole listing of different server variables that are very veil available. And again, another one that's used often is remote address. In order to get that I p address. That's where you can make use of these built in variables and add them into your coat. So the next lesson we're gonna look at deleting within PHP. So we're gonna look at how we can delete a file, how we can delete variables and so on, and this is coming up in the next lesson. 12. 13 PHP delete and destroy: So this lesson this lesson is gonna be fun because we get to actually delete and destroy variables and files. So get rid of them off of our server. And this is a very powerful lesson because this functionality is super powerful. And always make sure when you're deleting files when you're using unlinked that you actually want to delete that file because once you delete it, there's no getting it back. So make sure that you're very careful with this functionality. So first of all, let's try out the variables, so these are not as destructive. So we're going to create the variable call it test, and we're just sign a value string value to it. Let's echo that out. So I put it out, and then what I want to do is I want to unset it and again, we're just gonna put that into the into the browser. So see what actually happens here. Eso again, keeping in mind that unset actually destroys the variable Eso My question is, do you think that line number five is actually gonna throw an error? That test is completely gone out of the PHP system, even though we've used it up here once we unset it, are we gonna throw an error undefined error or are we not going to throw on air? And if you said that we are gonna throw an undefined error, you are correct because noticed that we've got an undefined variable test in line five. So this is the same thing as if test never existed. Eso We are fully able to destroy that variable. So this is much different than if we did something like test and we just equal test to nothing. So show you what happens here. Comment Thought it would refresh it. And we see that we don't throw that at that issue or that notice error, because in this case, tests still exists. Only difference here with test is it still exists. But it has nothing no value assigned to it, whereas unset actually destroys that that value and completely gets rid of it. So keep in mind that this is a very powerful a swell, because if you're unset ing your variables, that means you don't want to use them again. Within your code, you don't want to reference them, begin or you want them completely to be fresh and you once you start using them eso The other thing that we want to look at is another very powerful way to destroy and delete. So it's a very destructive lesson and we're going to show you how to get rid of files. So notice that in the previous lesson I create a footer I created Header. Well, now I want to get rid of them, so I want to get rid of them using code so I could do something like PHP Ah, header unlinked And let's see what happens down. So I go out here and if I refresh it, I go back to my web editor and I see that that header file is gone. Footers. Only one that exists s Oh, this is maybe would give it deletes. And we can do it this week as well, eh? So of course this looks a lot neater. So if I wanted to delete something, I could do something like this and notice that we're not actually out putting anything because this is a built in function here, and we don't always have to output when we run our our PHP script so it could actually do something in the background here, and you're going to notice that once I render out once I run the code, once I refresh this page, we're going to see that. So I haven't refreshed it yet. And I'm just looking at a way that I can possibly do this and I can refresh it and we can see what happens. So notice Footer is sitting there when I refresh the page footer is gone, so code in the background is actually deleting that specified file. If I refreshed again, we get a warning message because Footer no longer exists and we can't delete it again because it's not there. So that's where we're getting that warning message. And hence, from this point on, unless we have a file, another file called Footer that we create, we're not going to be able to see it. It's just simply deleted and removed, and this is very useful. If you are creating files on the fly, you want to get rid of some of those files. This is the way to do it. So again, always keep in mind that this is powerful, and you don't want to take this slightly and remove out files because you always run the risk of removing useful information. So the next lesson we're going to dive into a raise, show you a little bit more information about how to work with the rays and PHP arrays. PHP arrays are super powerful, and they can do a whole lot of stuff, so I'm gonna show you some more of that in the upcoming lesson. 13. 14 Power of PHP arrays: in this lesson, we're gonna show you more about using a raise, working with the rays within PHP. So it's open up our editors and create in a race. We're going to create a very basic ary and give it some values within that array of defining the array. And maybe the first value is one for the first item. And then we've got two and then three, then four and then five. And we could just keep going there. So maybe we want to do six as well. So we got multiple items here within the ray, and with the race, you could actually make really good use of a raise. So let's say, for instance, you want Teoh count how many items you had within the array, those built in function there and this will return back how many items you have within the ARY. So how many items do we have there? We have six. Let's see what the output is. Refresh and we've got six items there. So let's say, for instance, you want to get rid of an item within array Eso, for instance. I want output, my array, but I want to get rid of one of the items in the array. Speaking of deleting and destroying from the last lesson. Show you how to get rid of an item within the race. Let's stay. We want to get rid of this one here, so it's the starting at 012 So it's item number two. So this is where we would use on Set, and that's how you get rid of something in the ray. So if I do unset and I list out that array there, this is actually going to delete that entire array. So no, we don't want to do that. We only want to get rid of one item that's contained within the array, so it's in position number two. So let's unset that. And output that. And also let's print out our array. Remember again to print out in a rate we use print are taking the array, and maybe we can show it before we unset it, going back out to local host. We see that we are able to get rid of, so we've got one in position zero two in position one and three in position to, you know, that's kind of confusing. There because I use the 123456 Maybe shows started with zero. But either way, so I've got rid of that. And now we only have five items within their rate. So again, a raise, their super powerful the Tanase stuff you can do with the rays. And when you go to PHP dot net, Just look at all of these things you can do with a raise so you can reverse in a race of reverse order an array eso if we want to take that array, Open up our editor here and maybe way wanna reverse order that we can define array and use a ray reverse. And essentially, when Now, when we output it, we see item number zero is 654 And then, of course, we did get rid of three, and then we've got to And one That's how you do a reverse order. There s I've got some examples here, and we can actually set a different variable Reverse set and so on. Ah, so this is how we can really work with the rays? We can remove portions of the res so we can exact a slice of the raid by doing a ray slice is another commonly used one where we slice at a certain point. So in this example, they're using a race slice. They've got their rate to find there. So they're slicing at point at item number two and onwards. So 012 onwards returns the c d e e. Here we can count how many positions we want a slice. So we're doing negative, too. So we're starting at the end here, doing negative, too. Returning back one item from negative to which is D here. We're starting at 0.0. So starting at the beginning of the raid, the first item were returning back three items from that array at most. So that's returning ABC. So quite a lot. Once again that you can do, you can build these functions in, and there's a whole lot that you can really do with the Rays. We can a sort so we can sort an array and maintain index association. We can do I are sort to reverse order, but maintain that index association on and so on. So these are all very commonly used. Another one that we use is if in a Ray checks if of value is within an array. If if it is that returns true, we can also use shuffle. So Shuffle is another one that's really cool within a raise gives us the ability to shuffle that. So just show you what that looks like so we can do shuffle. So it's just a function here and we can grab the array. And once we shuffle that back and refresh and we see every time we refresh it, we're getting a reversed order. So it's a way to kind of randomize your outputs. So again, another useful function within PHP arrays. And then there's some more sorting options, so definitely worthwhile taking a look and seeing all the really cool things that you can do with the Rays, providing some really incredible power to using a raise within PHP. Next, we're gonna check to see if a variable is empty. So this is within a conditional statement, and this is something that we can use to see if there's a value within a variable. If it's empty or not, and I return return a Boolean values of true or false that's coming up in the next lesson. 14. 16 Check if variable exists PHP: So in this lesson, we're gonna look to see if the value is available. And if it's not, we're gonna essentially check to see if it's empty. Previously we saw when we're looking at errors that when we actually try to use a variable that hasn't been used before then we throw an error so quickly to check to see if that variable actually exists is to make use of empty. So here, for in this example, we're gonna check to see if is empty. And if it's if it isn't empty or if it is empty. So remember again, this is Boolean values. So true or false. So for checking empty, that means it is going to return back. True, it's not there so true that it's not there and else maybe we can write. It was found, so let's ah, let's see what this produces when I refresh it so true, it's not there because a in fact does not exist. So that turned out kind of long. There s let me fix this up a little bit here. It's more readable and it's gonna in debt. Those s Oh, there we go. So, essentially, we're checking to see if a is there and because it's not there, we get true. It's not there. But if we define a value for a so let's just give up a string value of hello, so now it should be able to find that and notice. The important thing to take away from here is that we didn't throw any errors, even though we hadn't defined a eso. Using empty is a great way around that to ensure that those variables that you're trying to access, especially when you've got a really long script, so might not be as useful here because you can really see if a exists or not. But if you've got knelt, multiple files all linked together, multiple variables on the goal, it's a good way. It's a good practice to get into, to check, to see if it's actually there before you try to call it and utilize it within your script. So the next lesson we're gonna look at string functions and PHP has a lot of string functions. So just as what we were looking at within a raise, there's a lot of really cool stuff you can do with strength functions that's coming up in the next lesson. 15. 17 PHP String Functions: So I truly have saved the best for last because string functions, there's a whole lot of them available within PHP, and they really do a lot of really cool stuff that you wouldn't expect from just a simple function. So just like as their raise, your actual string has a lot of functionality built right into it. So let's go take a closer look at that. And of course, we've got a link here. Teoh, What We've got available for string string functions here and we can see we've got a whole lot of functions, just like with the re is a lot of really cool things that we can do with this string functions. So in this lesson, we're gonna take a look at some of them and get familiar with them. So first of all, let's look at our default string there. So we've got our string there and with the string functions, One of them we were looking at here is str underscore shuffle. So what do you think that that does? So if I apply, let's do a new S t r so new variable here and this variable is gonna be s t r shuffles. It's going to use a string function and it's gonna use that on the variable a. And then let's output what happens here and in now. I know, of course I could do it this way. So maybe better practice would be just to do it this way. Eso echo that out and let's take a closer look and see what happens. So what's happening now is it's actually just like what we saw within their array, it shuffling the order of each and every item here within the string. And this is where the interesting part of string functions comes in because when we're looking at string functions and this is another example, we can actually get the length of the string. So let's output that as well. So we're gonna echo out and we're going to do the length of the string. So another string function here, which is S t r l e n and actually probably doesn't matter which one we use, but we'll just use a so we see that it's always five. So we know that the length of the string value is five, because that string is contains a value five. But what happens if we actually odd? Or actually, first of all, let me show you what this looks like. So this kind of looks like what we do with an array where we've got ah value here, So maybe we'll do one. So what do you think gets output here? Has noticed that the string it's not actually an array, but we're kind of putting it in treating at lake injury. And we see that what is actually happening here is that it's actually treating in the same format, so starting with zero, So if you want to get each, that would be zero. And I should put a line break in there as well, and I do another line break after this one. Eso notice that what's happening is that we're actually able to get to treat it as if it was broken apart into separate string values and output all of those separate letters separately, by doing the same format as we would with a raise. So what happens as within our example, if we do position number 10 So if we add in Teoh a and actually stick position number 22 me get really interesting. So the position Number 20 we're gonna add in world And let's so echo out a stake of closer look and see what actually happens. So you've got hello W which is really interesting because we're only out putting I w from the world when we had string. Position 20 is equal to world. So basically what's happening here is we're only able to identify that one character. So even though we've got more than one character here, we're only able toe odd that one character into position number 20 and another really interesting thing that happens here is let's check out what happens with the length thing that's gonna add in a line break here, save that, go back out and refresh it. So I should actually asked because a trick question. What is the length of this output here? You're probably looking at this and saying, Wow, this does not look like 21 characters, But remember how html outputs white space and when we position W at position number 20 it's doing 0123456789 10 11 12 13 and you get the point position number 20 And that's where we've got 21 characters available because we're setting that position of that character. W two position 20 So treating it as if it waas all broken up into the different characters . We can position items, and it's just because each TML gets rid of all that white space. It only treats. Ah, whitespace doesn't matter how much white space you have in HTML. It treats it as one space, and that's what we're getting that output and it looks a little bit odd and it doesn't look like it should contain 21 but in fact it does because of that white space. So that's ah, really interesting part of string variables that we can specify strings by their character number eso some of the other string functions that you might make might want to make use of . So there's the first character if you want to make it lower case, so we've got the ability to do first character lower case. We can also transform it to uppercase, weakens, transform all the strings to upper case you just take that first letter. So let's maybe take a look at string to upper case, and we're transform our variable output here to upper case. So I'm gonna just put and use the function here that we had. Esti are uppercase a copy. Paste that and change that to a and show you what that looks like again. I know I need toe keep these line breaks because when we output it, it turns out pretty ugly. So we see that we're able to transform that string to upper case. So all of a now becomes upper case. And of course, again, remember, we've got that spacing in. There is just a lot of really cool stuff here within the string variables. We're also able to split them out. So, uh, we're able to convert a string into an array s that's another useful function within were able to strip slashes s. So this is something that comes in useful when you're working with html eso here we've got another one for hte emails or stripping out the HTML entities Eso This essentially transforms this into more of an html friendly output. So show you how this looks But of course, we need to add in some html there eso if we have a and maybe let's add in and so this is actually going to change quite a bit with our output within PHP because we're no longer going to be contained. Onley at the six characters were actually going to contain at 12. When we shuffle it, we see we got a whole shuffling of all of those characters I would put 21. And over here, lastly, we see that we're stripping it down and wrote putting it as if it were characters. So gets rid of all of that. HTML strips those HTML entities, and we're having a lot of weird stuff happening here because of that shuffle, because it's actually treating. So it doesn't take this as one item again. It takes it as one string value, so it doesn't recognize that this is HTML and it does shuffle this around. So it's regardless of the fact that we have HTML in here. It's reordering this stuff and doing some really wacky stuff here with the outputs s. That's why we're getting all of this weird have put happening here. Eso again. Maybe I can remove those out and just go back to our initial example, so it out puts a little bit neater, so it's not shuffling there that was causing that it in issue eso you could use HTML entities. If you're trying to get rid of any HTML there that might be deposited into your you're a code, you can also use HTML special characters. Eso This allows certain characters in HTML, and we can see that the output once again gets into character set instead of as it would with HTML. So that's another great way to to really utilise your HTML output. So next in the next lesson where you'll look at one of my favorite ones again, it's another. It's another string function, as it just that's what we've been looking at. But that's explode and implode. The sounds really cool and guaranteed. It's very useful functionality within PHP, so it's coming up in the next lesson. 16. 18 PHP Implodng and Exploding Arrays and Strings: this lesson where you re looking at some or string functions and those ones that we're looking at are really super powerful because they give us the ability to create a fully functional array from a string and then also implode allows us to reverse that and turn an array into a string value and utilize it. So just because, as we see here in the example, were exploding our string and it out puts us in a rate Let's take a closer look at this within our source code and within our editor s O First of all, let's create a string here, so welcome to PHP and PHP explode eso I know it sounds really cool and, uh, a great a great way to create array really quickly. So let's create my array and out of the string we're going to explode this string, and I know I just love seeing that word. Ah, So first argument here that we have to look at is how we want to explode the strings. So if we have Siris of commas, we could explode it via comma as well. So maybe we'll try that first and then lastly, the array that were the string that we want explode. And then let's let's print out our output here of the array and take a look at this within the browser. So when we refresh it, we noticed that our first array Hello world, welcome to PHP explode. And that's because it's looking at the commas. We can also do it by space so we can do something like that. Refresh it and we could see that are Ray builds out even further because we're fully exploding at via the spaces. Eso again Very useful toe. Have the ability turn those strings into an array and then you can utilize it as you would any other way you could sort it. You could shuffle it, account it and merely make use of all of those powerful a re functionality that's available within PHP. So now what happens if you have an array and you want to turn it into a strength? Well, PHP has a really easy solution for that, and that's called implode. So instead of exploding, let's implode. So let's just maybe call this my str and we're gonna implode. And so this is the reverse of what's happening over here and again. We got the ability to specify how we want to break apart, that that array Ah, and how we want to separate it within its implode eso how we want to separate it out within our output and then here we don't need to use print are we can echo that it and we can take a look at it just as we would normally within the browser. So see that now its added in the Comus in between them. So let's refresh this and we see that we've created a string. So just one line of quote. So this could as easily be pretty much anything we want. Maybe if we want to add in some quotes in there some brackets in there, we can do that as well, so we could use any number or any length there of Of I items are characters between the different ary items as we transform it back into strength. It's a really powerful and really functional way to work with PHP and of course, that PHP dot net. They've got some more examples there, so they've got the delimit er the string and we've been able to set a limit there. It's a lot of times that I don't use the limits, but this is there in case you need it. So we exploding it into pieces, we got a number of pieces. And then, as you can see in this example, you can treat it as you normally would within a ray s. So it does have a limit here or implode. So implode does the exact opposite way set how we want to separate it out and we implode it , and then we can output it as a strength. Eso a lot of really cool stuff here within the string functions off PHP. And this is just another great example. And I do encourage you to check it out and try out some of these some of these different functions that available for string functions within PHP as they become really useful within your source code and much like everything else that we've gone through within this course, try it out for yourself. Open up an editor, set up your local machine and try out the code for yourself. See what you can make happen with PHP. Because that's really the fun of PHP that when you do a little bit of code and you see what really amazing and cool things you can do with the code and within the previous lessons. We've gone through a lot of the common functionality that's used within PHP on a daily basis, and it really does illustrate the power of PHP.