Learn PHP basics in 1 hour | Chris Dixon | Skillshare
Play Speed
  • 0.5x
  • 1x (Normal)
  • 1.25x
  • 1.5x
  • 2x
10 Lessons (1h 12m)
    • 1. Introduction

      0:44
    • 2. Optional Video: How to install a localhost

      7:50
    • 3. Introduction to PHP & hello world

      7:00
    • 4. Strings, variables & constants

      10:14
    • 5. PHP data types & operators

      5:56
    • 6. Arrays

      6:59
    • 7. Functions

      4:39
    • 8. Conditional statements & more operators

      12:25
    • 9. Switch statements

      4:31
    • 10. Loops

      11:12

About This Class

Begin your journey into the PHP programming language with this short, straight to the point course. We cover all the basic syntax in just 1 hour including:

  • Setting up a localhost to run PHP
  • PHP tags and hello world example
  • Comments
  • Strings, Variables &¬†Constants
  • Data Types & Operators
  • Arrays
  • Functions
  • Conditional statements
  • Switch statements
  • PHP loops

This course is ideal to begin learning the PHP programming language or refreshing your skills.

Transcripts

1. Introduction: Welcome to this course on PHP basics. My name is Chris and I'll be guiding you through step-by-step. The aim of this course is to give you an introduction to the PHP programming language and to get you familiar with the syntax, in just one hour. We'll begin by setting up a local host, to create a web server on your own computer to room PHP, then we'll take a look at things such as strings, variables, arrays, and data types. We'll also cover functions, conditional and switch statements, as well as PHP loops. So thank you for taking an interest in this course and hope you'll enjoy. 2. Optional Video: How to install a localhost: Now let's get to work on installing our local host on our own computer. I'm going to head over to my web browser and I want to search for MAMP, which is M-A-M-P. Then we need the mamp.info. We just select that. For this video, I'm going to be using the MAMP package, which gives us Apache, the MySQL for our databases and also PHP and as well as the server-side languages. There is different local hosts which you can download, such as XAMPP or but I'm going to use a MAMP for this video. It's free to use, it's free to download, and it's available for both Windows and Mac. To get started, just click on the Download button and then just scroll down to the version of your choice. I'm going to use the Mac version, click on the Download button to get that started. Just give that a few moments to download. Basically MAMP is a package which includes a web server. It replicates a web server on your own computer so you can work with server-side languages such as PHP, without having to upload your website to a live web server and also allows us to create MySQL databases that we can interact with. You can basically develop your website on your own computer and then once it's finished, you can upload it and start only paying for a web server once your website is complete. There is a pro version available with MAMP, but the normal version is more than good enough for our purposes. Just give that a few moments to download, I'll return in just a minute. Welcome back. Hopefully the MAMP package has been now downloaded onto your system. If you just go through the installation process in the usual way. I'm just going to to click Continue and agree to the license, install for all users, and I'm going to go for the standard installation and then just let that install. There we go. Now that's fully installed on the system. Let's just get rid of this for now. Depending if you're using a Mac or Windows, just head over to the location which you've installed it. In my case, it's in the Applications and then as standard MAMP folder. We just click on the MAMP folder, and then you can see inside there's various different files but the one we're going to be mainly concentrating on is the HT docs, which is short for the hypertext documents. This is basically the folder or the location which we are going to put our website in. Then we want to be served up by the local host. I'll just leave it off just for one second. I'm going to create a new folder just on the desktop and I'm just going to simply call this PHP website just as a quick example. Then it's going to open this up inside the brackets as an example. We just drag that in, I'm going to create a new page. I'm going to create a very basic index page using PHP. To save that inside the folder as index.php. Then open up our PHP brackets and also the closing tag. I'm just going to create a simple echo and then I'll put this between H1 tags. I'm just going to make a message saying, "PHP website is working." Then save that. If we now to open this up inside the browser, you should know there's a problem. Let's opened so up inside a Chrome. Just scroll down to find your web browser. That's the contents of our index page, but it's not exactly what we're wanting because when we're saving these pages as a website, we don't want to display all the PHP, we just want the content or the HTML to be returned. In our case, we just want the text in between. Now what we need to do to get this to work is, we need to drag our PHP website folder inside the HT docs. Just drag it over and drop that inside there. Then if we just open up our MAMP program, and then click on Start Servers, and you know it's in the top right corner it's the Apache server and MySQL. If we scroll down, we can see some information. We've got the host as a local host and the user and password is being set to root. That's all working correctly. Inside the MAMP program, if we click on the preferences, you can find out a little bit more about the version of PHP we're using. You can click on the ports, and the Apache port is the one which we're going to be using inside the browser. Is that 8888. If you go back over to Chrome, and now we can reopen our index.php inside the local host. Then if we type in localhost, and then we need a colon, and then because we're using port 8888, just type that inside inside there. Then [inaudible] folders we can either just go into it just as is there, and we're going to see exactly what is inside the HT docs. In our case it shows php-website, or you can actually type that in at the end of the URL. Just click inside there, and there we go. Now rather than having all the PHP tags on the echo printing to the screen, we've now got the actual HTML returned, so now the PHP website is now working as it were on a live web server. If you got any different files or folders inside the HT docs, you just need to use exactly the same local host, the 8888, and then just change the forward slash to the directory that you put inside there. You can see which directory we're working on if you just go into the web server. You can see there's our file path to the HT docs. If you want to look a little bit further into it, you can also click on the Start page, and it takes you back to the welcome sign, and you can click on the tools and you get an option to go to phpMyAdmin. That's the sign that we need to use if we want to create any databases. You can also see on the right hand side all the different versions of PHP, Python and Apache for example, that we're currently using. We'll just leave that video there. That's how we can create a local host on your own machine and several PHP websites without the need for the live web server. 3. Introduction to PHP & hello world: Okay guys, so this video is all about getting started with PHP. We are going to take a look at the basic PHP syntax, the you throw it out, and do a Hello World example. So to get started we're going over to the ht docs, inside your local host and then create a new folder. I am going to call this folder the PHP-basics, and then drag this folder into your text editor. So I'm going to use Brackets by Adobe, and then drag that into there. So to get started, I'm going to create a PHP page to start working with, so create new page with Command or Control N inside the brackets. First of all, I want to save this page as Hello World, make sure it's got the PHP extension at the end. Save that inside PHP-basics. Then we need to create a basic HTML skeleton to begin with, so I'm using the Emmet plug-in, so If you don't use that you will need type these out manually. But if you do download Emmet , all you need to do is type in html:5 and then hit the tab. Then it fills in all the HTML five skeleton for you. So let's call this the hello world. Now let's get to work within the body section, so to work with PHP, we need to provide a opening and closing tag. The opening tag in PHP looks like this, it's the <?php, and this indicates the PHP code is about to start. Then at the end of the PHP we need to add a closing tag, which is the?>. All the PHP code goes in between here. So the first thing I want to do is just print some text to the browser, so I'm going to do that with echo. So type in echo, and then because this is going to be text we need to put in quotations. So Hello World, and then at the very end, we close a statement with a semicolon, so save that. This is how we print something to the browser using PHP. So go over to your browser and then type in local host, then go to the file that you put inside the ht-docs, so it's php-basics and then hello-world. So there we go so displayed in the browser is our texts that were just echoed of Hello World. We can mix and match PHP in among-st the HTML. So instead of having the title as typed in there, which is what's called hard-coded, so instead of typing into that we can open up the PHP, and then do the echo Hello World. Then make sure the PHP is closed off with a question mark on the angle brackets, so save that. Then if we refresh, we should now see that the title at the top of hello world is now being produced with PHP. Just to make sure we'll type in PHP and then refresh. Now also this is being generated with the PHP, so we can mix and match the PHP in among-st the HTML as long as we have the opening PHP tags and the closing PHP tags properly set. Comments in PHP is similar to lots of other languages. We can comment out some code that will no longer needs, or can use a comment to type in some notes for ourselves or the programmers that we want the program to ignore. For example, if we no longer wanted to display this and we wanted the program to ignore it, we could use two //. You can see the text editor makes the line go gray,, so you can tell that's being commented out correctly. So if we save that, and then refresh that should disappear, because it's no longer being processed. So let's just remove this. Comments are also useful for creating notes, so we can make a note to ourselves saying, Display Hello World To The Browser. This is what's called a single line comment, if we just want to comments out a single line of code, we just use the two //. If we want comment more than one line, we can use that with a multi-line comment, so it's a /* and then we can comment out more than one line. So we paste in Hello World two or three times, and end up with a */. So this is how we comment out one line, and this is how the comment out more than one line. So in this whole PHP section, only the single echo of Hello World will be displayed in the browser. Before with the title we will look at it in PHP in among-st the HTML. We can also add HTML tags in among-st PHP, and these will be displayed in the same way as if they were HTML. So for example, in the echo Hello World, if wanted this to be a HTML level one heading, we could put the HTML tags of h1 before the text, an and then we can put a h1 closing tag after this. Then instead of being displayed as normal text, this will now be a level one heading. So if we save that, and then refresh, we now get Hello World as a heading in the larger font size, and you can put in any tags that you want. So we can do the same while this has a h2 and save. Let's go over to the browser and there is our level two heading also. Such basic PHP syntax, that's how we can echo or print texts to the browser, and also how we can comment out code as well as combine PHP with HTML. 4. Strings, variables & constants: Welcome back. In the last video, we took a look at some PHP basics, such as echoing text to the browser, comments, and also how to make PHP and HTML. In this video, we're going to move on to taking a look at strings, variables, and constants. I'm just going to delete the code between the PHP tags from the last section. First of all, we're going to take a look at strings. A string is a sequence of characters, such as a single letter, a word, or a sentence. A string is what we used in the last video, where we were doing an echo. So we printed a string between quotations. So this is a string, don't forget the semicolon at the end. The string must be surrounded with either a single or a double quotation, they both work perfectly fine. So echo and then this time a single quotes. This is also a string, let's take a look at that in the browser, refreshing. So this is a string, and this is also a string. Most of the time it doesn't matter if we use single or double quotes, the only time it really matters is if we want to surround one of these words with the quotation marks. For example, if we wanted the word string, we have the quotations printed on the screen, we need to make sure that the quotations we use around the word, opposite to what is surrounding the sentence. So if we save that and then refresh, the word string has the double quotes. But if were to do this, and all had the same style of quotes, save that. The text editor has picked up a issue, so it's making a different color. If we were to refresh, we see the local host page isn't working, because we've got an error. But if we change this to be single quotes, and then the surrounding ones back to double, and then refresh. That's how we can add single and double quotes within a string. There's lots of other ways we can work with strings by using string functions. For example, I'll just delete this example there, if we wanted to reverse a string, we could use the string reverse function, which is strrev, and then we need to surround the string inside the brackets, and then save that. Let's check out the browser and see what happens. That reverse is all the characters inside the string, so it's now a bit different. We can also do other things such as make all the words uppercase or lowercase. To make them all uppercase, we can use the string to upper function, so str to upper, and then save that, and then refresh, and now all the characters inside the string are uppercase. We can do the same with string to lower, so str to lower, save and refresh, and now there's no capitals at all in the string. Of course, there's many more different string functions, which you can find with a quick Google. So we search for our PHP string functions, and now we're instantly taken to a list. It's either a php.net or W3schools, which has lots of examples. As you can see, there's quite a list, so there's lots of different things we can do with strings, such as split the string into an array. We can randomly shuffle all the characters in a string, we can check out the length of the string, which returns the number of characters. So we have a play around with these. You may not have a need for these two functions, which I've just shown you. I'm sure if you work with PHP regularly, you'll come across many of these different functions. Next, we're going to move on to variables. Like many other programming languages, PHP uses variables as containers for storing information. The information inside these variables can change. I'm just going to delete this string. The way we declare a variable is by first using a dollar sign, and then assigning a variable name. For example, number1, I'm going to set the value to be equal to a string, a number, a Boolean, or various other things. So I'm going to set the number1 to be equal to 10. You know, when we're dealing with numbers, we don't need to put the quotations either side of it. What we've done is create a variable with the name of number1, and we've assigned the value of 10 to this number. Then we can do the same, we can create a second variable, this time I'm going to call it number2. This time I'll set the value to be 20. Now, we can use the echo that we looked at before. So instead of echo in a string, we can echo the variable name, so number1, now it should get printed to the browser the value of 10. Refresh, and there's our number 10. We can also add variables together. We can echo the value of number1 plus the value of number two. We should now get first in the browser. We can also combine strings with variables. If we have a variable, so the number of posts. Let's set that to be 7, and have a couple a variable name. So the variable called name, assigned to a string. I'm going to call that Chris, so if the user call Chris, has created 7 posts. We can then print this to the browser, so echo, and then because it's string, we need to put this in quotations. If we wanted to print, "Chris has 7 posts," we could start with the variable name. So place that in this, that would be Chris, and then has, and then we want the value of posts, so put that onto there. So currently it says," Chris has 7, " and then posts. Then like we mentioned before, we can put a HTML tag inside there. So I'm just going to add a break tag into that. Hopefully, we should get the value of, "Chris has 7 posts," which you do, so that's working fine. We mentioned that values of a variable can be changed. If we go back to our number example, number1 equal to 10, and then echo number1. Now we should get the value of 10, print it to the browser. But fairing down the program, if you wanted to change the value of number1, we can reassign a value. So number1, we can change it to be 20. Then, when we echo number1, we should get the value of 20 printed to the browser, which we do. This is because the program is read line by line, so when it gets to line 11, number one is set to the value of 10, and then moves down to the next line, and then realizes that number1 is now set to 20. So then when we echo number1, we have the value of 20. The last thing I want to look at in this video is constants. Constants just like variables, can store some information or store a value. However, unlike variables, the value can't change once it's been set. This is how you set a constant in PHP. First of all, we use the defined function or the defined keyword to declare a constant, and then we open up the curly brackets, and then we need to pass in two parameters. The first parameter in-between the quotations and in capital letters, is the name of the constant. For example, if we wanted to store a value of your own name, we can call it NAME, and then separate it by a comma, we add the value of the constant. This would be my name, and then a semicolon at the end. So we've defined a constant with the name of NAME, and then we've added the value of Chris to this constant. We can print this to the browser in the same way as before. All we need to do is echo the name, and then we should get the value of Chris inside the browser. There we go, that's how we use strings, variables, and constants in PHP. 5. PHP data types & operators: Welcome back. So far in the last couple of videos, we've taken a look at how variables can store data in PHP. We've looked at numbers or integers, and we've taken a look at how we can store them into variables. An integer, we can have various different types and you can see some examples such as a positive number, a negative number, or a hexadecimal number. We've also taken a look at strings, which you know is a series of characters. I may compile the strings in different ways, such as single or double quotes. But there's also many different other types of data that we can use in PHP, for example, there's a boolean. A boolean is a simple, true or false. For example, we can see if something returns true, and if it does, we can perform a certain action. There is also other data type such as arrays, which we'll look at in this section. But next we're going to take a look at some PHP operators. To begin, we'll take a look at some arithmetic operators. Let's use our number example we looked at before. Number 1 equal to 100, a second variable of number 2 equals to 200. Then we can do an echo, so echo number 1 plus number 2, and we should get the value of 300 in the browser. This plus symbol is a arithmetic operator, it adds value of number 1 to number 2. We can also take these values away with the takeaway symbol. So now we'll get minus or negative 100. We can also multiply, and to do that in PHP, we need to use the star symbol. So we get a value of 20,000. We can also divide with the forward slash, and we can also find out the remainder. We can do that with the percent symbol. The percent symbol gives us the remainder after the division and to do this we need to change the values to something more suitable. Number 1 equals 10 and number 2 is equal to 3, and then refresh and we should get the value of 1. The reason for that is because 3 goes into 10, three times and then leaves a value of 1 as a remainder. Let's take a look at some more examples on the W3Schools website. W3Schools and PHP operators, then let's scroll down. We looked at some of the arithmetic operators, which you can see here. There's also assignment operators. The most basic assignment operator is the equal symbol and we've looked at these in previous videos. So we assign the value of 10 to the variable name of number 1. There's also other assignment operators, such as addition, subtraction, multiplication, division, and the modulus, which is the remainder. There's also comparison and logical operators. We can check if two values are equal or less than or greater than, but we'll take a look at these in more detail in the if, else statements section. We can also increment and decrement a value. So we can increment, for example, a value by 1 or we can decrement a value by 1. We'll look at increment and decrement operators more in the loops video but for now, let's take a look at a few basic examples of how we can use them. We had a variable called number, and we simply set this to the value of 1. If we echo this, the browser would just get the value of one printed. But we can increment this value by 1 every time by setting the variable name of number and then using plus plus. Save that. So now the value of 1 should be incremented by 1 and then we should get printed to the browser the value of 2. Refresh, sorry, a semicolon at the end of that, and then refresh, then should be the one of that and save. Refresh the browser and then get the value of 2. We can keep repeating this. If we wanted to increment by 1 once more, and take the value to be number 3. We can add that in again, and then we should get the value taken to 3. If we wanted to decrement it, we could use the negative symbol and if we use it twice, we should get the value of negative 1. So refresh and there we go. The value of 1 gets decremented to be 0 and then decremented again to be negative 1. That's a quick look at data types and operators in PHP. I'll see you in the next video, we'll take a look at PHP arrays. 6. Arrays: Welcome back guys. In this video, we're going to take a look at PHP arrays. In previous videos, we looked at variables, which are great for storing data, which can be changed. However, they can only hold one value. But if we want to store more than one value, we need to use an array. We can store as many values inside of that array as we like. This is how we create an array using PHP. There's two different methods I'm going to show you. The first one, we can set the array's name, just like a variable name. I'm going to create an array of shapes, and then set it to be an array, and then we need to follow the array keyword with the curly brackets, and then a semicolon. So all we've done there is create an empty array with the name of shapes. To add some values inside of this empty array, let's use the array's name, and then followed by a set of square brackets, and then we need to put in the index number, so we start with zero, which is the first position of an array. I'm going to set this to be the first shape, so a square, it's going to add the value of square inside the array, and let's add one or two more. So shapes, and then position 1. This can be a circle. Then shapes, position 2. So a triangle. We've set a empty array to have the name of shapes, and then we've added three values to array of square, circle and triangle. Let's check this if is working. We can do echo. So we need to echo the array's name of shapes, and then the elements of the array that we want to print. Let's start with number 2. You should print out to the browser, the value of triangle. Save that, and then refresh. There we go, that's the position number 2 of the array, which has the value of triangle. I'll exchange this to number 1, so we should get the circle. This is quite a long way of writing an array. There is a simpler way to write an array. This is by using the literal method. Instead of what we've done above, we can set the array's name of shapes. Then we can set out to an array, and then we can put the values straight inside the array. First of all, we can put in the square, and then separated by commas, the circle, and then the triangle. Let us delete this one for now. I'm going to do the echo, just as we've done before. Just echo the name, which is shapes, and in square brackets again, we need to put in the index number. Let's start with zero, which is the square. Save that, and then refresh, so we get the value of square. If these two methods works perfectly fine. A few videos ago, we took a look at string functions, where we reverse the order of the string, and then we change the text to be uppercase. Arrays also have similar functions, and there's a list of available functions at PHP.net. But I'm just going to go through one or two examples, just to show you what we can do with array functions. The first one is the count function, which counts the number of items inside the array. We use the count function. Then we put the array's name inside the brackets. We can delete that zero, so you should echo to the browser the number of items inside the shapes array. Refresh. There's three items inside the array. There's also varies other functions, such as the array pop, which removes and returns the very last item from the end of an array. To do this, I'm going to create one more variable, and I'm going to call this the last shape. The last shape we want to grab is a triangle. To grab the value of this, we're going to set this value to be equal to array_pop. Then to drag the value of triangle, or the last item, we just need to put in the name of the array, which is shapes. Now, what we've done is we've set the last item on the array inside this variable name, so now we can just echo this variable names to browser, and we should get the value of triangle. Echo the variable name of last shape, and then refresh. There we go. That's the value of triangle. We can also find the minimum and the maximum value of an array. Let's create array with numbers this time. Numbers is equal to an array. We don't need to put any numbers inside the quotations. Let's just put some numbers inside there, and then we can echo. If we wanted to find out the largest number, we could use the max function, and then inside the brackets, the name of the array, which is numbers. Now we should get printed to the screen, the maximum number, which is 67. Which we do. We can also replace max with min, and we should get a value of one. Great, so that's how you can create an array using PHP, and also some basic PHP array functions. Thank you, and I'll see you in the next video, where we'll take a look at PHP functions. 7. Functions: In this video, we're going to take a look at how we can use functions in PHP. A function is basically a block of code. We assign a name to this block of code and call it [inaudible] program when we want to run it. It's really useful when we have the same code more than once in our program, and rather than type all the code over and over again, we can place the code inside a function and call the function when it's required. PHP also has lots of built in functions at our disposal. We've looked at one or two of these in the last few videos. Okay, so we get started by creating a function by using the function keyword and then after that, we then give the function a name followed by the parenthesis or the brackets. The name can be myFunction and then use the brackets after that. Then the contents of the function is within a set of curly braces. So every time this function is called, the code inside these curly braces will be run. For example, a basic way of given function is just a echo, so my first function. On it's own, this function doesn't do anything until we'll call it. The way we call it is just simply by typing in the function name, so myFunction, and then the parenthesis, followed by a semicolon. Now when we run myFunction, we should get the text of "my first function" printed to the browser. Inside the function, we can even have different variables inside there. Just like we looked at a few videos ago, we can use a user name. So a user name equal to Chris, and the second variable of posts equal to five. Then we can echo to the screen "Chris has five posts". So echo within the quotations, so the user name has five posts. Now when we call this function, we should get the message to the screen within the echo. Save that and then refresh the browser. We can take functions even further by passing here in parameters. Let's create a new function and if we want to create a function to multiply numbers. This time, instead of just leaving the brackets empty, we can pass in two parameters or even more. I'm going to use this to multiply two variables. So variable number a, and then separated by commas, variable number b. Every time this function is called, we want it to multiply the value of a with the value of b. So let's echo variable a multiply by variable b. Then to pass in the numbers, we'll call the function by it's name. So multiply numbers, and the two numbers that we want to multiply we can pass inside these brackets. So 12 and two for example. Now when we call this function, we now have the value of 12 multiplied by two. So we should get the value of 24 on the screen. Okay, great. What we've just done there is we've effectively created a variable of a and b, and then these two take up the values of 12 and 2 and then will multiply them together and display the results to the browser. That's how we work with functions in PHP and it's also how we can pass parameters into functions. I'll see you in the next video where we'll take a look at conditional statements and more operators. 8. Conditional statements & more operators: Welcome back. In this video, we're going to take a look at conditional statements and some more useful operators. Conditional statements are used when we want to check if a certain condition is true. If so, we can then tell the program what to do next. For example, we can check if a user is logged in and if they are, then we can show the user's information on the screen such as the username. Let's first get started by looking at the most basic conditional statement, the if statement. I'm going to begin by creating a couple of variables to work with. I'll use the dollar sign and the first variable is going to be logged in. I'm going to set this to a Boolean value, so if you remember that could be true or false. Let's initially set that to true and then the second variable, this is going to be the username. This is a string. I'm going to add the username into that and then I'm going to use a if statement to check if a condition is true. We begin by using the if keyword and then after that, a set brackets. Inside that is the condition that we need to check against. We want to check if the user is logged in. Let's test if logged in is true. Just inside the variable name of logged in inside there, and then we add the pair of curly braces. If this condition is true, then we run the code which is between the two curly braces. As an example, if our user is logged in, we're just going to echo to the browser a message just saying welcome back and then a comma and then we can just add with the period or the dot, the username which is stored in the variable called user. Then don't forget the semicolon at the end of the statement. Just save that. Then I'm going to go over to the browser and then refresh this, so great. We've got the message saying welcome back Chris because our condition is set to true. If we change this to be false we shouldn't get anything on the screen, so our message is not displayed. Such a basic if statements. Now, we're going to move on to taking a look at using comparison operators. I'm going to head over to the W3 Schools website. I'm on the comparison operators section. We can also use these comparisons with if statements. We can check if two values are equal. The double equals is for checking if two values are equal values. There's also a triple equals which checks if the two values are equal value and also the same type. By type we mean if they're both a string or if they're both a number. We can also combine with an exclamation mark to do the exact opposite. This will be not equal. We can use the less than or greater than symbol to check if a condition is less than or smaller than. We can also use less than or equal to, or greater than and equal to. Let's put these inside our if statements as an example. I'm just going to use a new variable, so this is a number and I'll set this to be the initial value of 10. Now, we check in if the variable or number is greater than five, then we can do an echo saying the statement is true. That is checked out. Our number is greater than five, so we should get the statement is true, print it to the browser. Let's just check that and refresh and of course we do. If we change this to be less than, this of course is false so the text should disappear. Just like on the W3 Schools website, we can check using any of these operators. We can use less than or equal to. We set this to be 10, this is of course equal to 10 so this statement should be true, which it's. We looked before at the double and the triple equal symbol. Let's begin with the double equals. Rather than number, let's put this inside the quotations, turn into a string. Let's just save that and refresh and see what happens. We've got the statement as being true. Although the value is above 10, this is a number, this is classed as a string because it's in the quotations. Let's just put a third equal symbol into there and save and then see what happens. We'll use the text in statement is true and this is because using a triple equals checks both the value, so the value is equal and also the type. The type is not equal because this is a string and this is a number. Using if statements are really good if we only want to test against one outcome. But if want to test against more than one outcome, we need to use a if else statements or even else-if. Let's first begin by using if else statements. Let's just change this back to be a number. If the number is less than 10, let's change this to be the number is less than 10. As you know, the if statement will check if the number is less than 10 and then print this to the screen. But we can also provide a second outcomes test against in case this is not true. We can use the else keyword. Then inside the curly braces, we can provide a second echo. The number is not less than 10 and the semicolon. We'll initially check if this statement is true and if not, this will be printed to the screen. Let's save that and then refresh. The number is not less than 10 and of course it isn't because it is number 10, so we can just put a nova operator in there. To test if the number is less than or equal to 10. Save that and this should now change to say the number is less than 10 or in fact equal to. But what if we wanted to test against three or more conditions? Well, this is where else-if comes in. I will just put these on separate lines just to make it a little bit more readable. In between the if and the else, we can change this to be else-if and then use the brackets for a condition to test against. First of all, we're checking if the number is less than 10. We can then use the else-if to test if the number is equal to 10. Again, the variable of number is equal to 10. We'll change this to be number is 10. Then finally, if these options are both false, we'll get the backup. I'm going to insert the else statement back into there as a fall back. This time we can echo number is not less or equal to 10. Let's test this. First of all, we'll set the number to be less than 10. We should get number is less than 10 and fresh, so the number is less than 10. If the number was changed to be equal to 10 of course we should get this statement of number is 10. Then finally, if the value is greater than 10, so let's change that to 14, we should get the number is not less or equal to 10. Refresh and there we go. That's how we can use if else and else if statements to test against multiple outcomes. The final thing we want to take a look at in this video is logical operators. Let's go back over to the W3 Schools example and let's take a look at logical operators which is a little bit further down. As well as the tests that we used before to test if conditions are less than, greater than or equal to for example, we can also use the and operator to check if more than one condition is met and all. We can also check if one value or all is true and also the exclamation mark to test if a condition is not true. I'm going to start by creating two variables, so number 1 and then also number 2. This value can be 10 and then we can remove everything else apart from the if statements. If we wanted to test against both of these conditions rather than just one, so if number 1 was equal to five, I also want to check if number 2 is equal to 10. We can use the double ampersands or the and keyword, so I'm going to use these. Number 1 is equal to five and also number 2 is equal to 10. Echo statement is true and just change it back to be the dollar, there we go. Of course both of these statements are true, so we should get this print into the screen. But if all these two statements return false, we shouldn't get anything at all. Let's change that to be two. Of course number 1 is not true and we need both of these to be true for this to be printed. Now, if we save it and refresh we should get the message removed. But we could change this to be or which is the two pipes. Just like we've seen before, we can use the two pipes or the or operator. Now, we only need one of these statements to evaluate to true. Number 2 is true and number 1 is false. Now they should get the message printed again on the screen, so let's just test that and then refresh, which we do. That's it for this video and that's a basic look at conditional statements and some PHP operators. Thank you and I'll see you again. 9. Switch statements: In the last video, we looked at if else statements and how we can use them to tell the program what to do based on if a certain condition is met. Now, we're going to move on to look at a switch statements, which is a great alternative to the if else statements if we need check against many conditions. I'm going to get started by creating a variable called favouriteFood. I'm going to set this to be a string of Pizza. Then we're going to use a switch statement, check many different types of food, and then check if these match up the variable of favouriteFood. To begin a switch statements, we use the switch keyword and then inside a set of brackets, will then pass in the variable I want to check against. So place inside there and then just like the if statement that we used before, we then use a set of curly braces. Then within these curly braces we can put in all the expressions that we'll want to check against and then also provide outcome if this expression is met. With switch statements, we check against different cases. We use the case key word. So the first case we could check if the favouriteFood is a curry. So let's test a if this is equal to curry. Then we need to use a colon and then once we've done the colon, we can then provide an outcome if this is a match. I'm just going to use echo and then we're going to say, "I love curry." Put the semicolon at the end. Then after each one of these expressions, we need to use the break keyword with the semicolon. So this breaks out of the switch statement. If this is true, and if not, we'll just move down to the next case. So use the case keyword again. This time, we're going to check if the case is Chinese. Again, the colon and the echo of I love Chinese. Then just like before, we use the break keyword. Then one more, so case and this time Pizza, echo, I love pizza. If this is true, we can break our switch statements. That's how we lay out a switch statement. One more piece we need to add into here. Before we test it, we need to write a default case, just in case any of these expressions above don't match. This is more of a backup. Defaults and then the colon, and then, we'll do echo, I don't know. Let's save this and test it. You can probably work out what's going to happen here. We've got one favouriteFood, says its pizza, so we should get the case of pizza, matched up and then echo, I love pizza. That will do. Let just changes to be Chinese and I should change to, I love Chinese and then curry, that works fine, I love curry. Now, we'll test default so we can change this to be Pasta. We don't have a case for Pasta. We should get the default of, I don't know. Which we do so, that's good. That's it. That's how we can use a switch statement in PHP to test against multiple outcomes. 10. Loops: In program when we can use loops to make repetitive tasks much easier. For example, we can loop through all your friends names inside a database, and then display them onto the screen. This saves typing on the same line of code, over and over again for each friend. In this video, we'll be taking a look at the four types of loops, we can use in PHP. We're going to begin with what's called a while loop. While loops basically run a block of code as long as a condition is true. So let's take a look at what while loop looks like in PHP. So I'm going to create a variable set to be one, and while loop is set out a little bit similar to a if statements, that we looked at a few videos ago. So but the while keyword, the condition within the brackets to test, and then the outcome to perform between the curly braces. So I'm going to start by checking if our variable of number is less than ten. So while our number is less than ten, we're going to create a echo of number and I'll just type this so fast, and then I'll explain what it's doing. So is less than ten, and we'll also need a brace second there, and you'll see why in just a moment. Then the variable number, we're going to increment with the plus, plus. So you may be looking at this and wondering what's going on. So basically, we're creating a loop, and will start to test if the number is less than ten, which is. So in that case we're going to print out number, so that will be number one is less than ten. Then once this is printed, what I'm going to increment the number by using the plus plus, so this will change the value of number to number two. Then this condition is still true because number two is still less than ten. So then the second time around on the loop we should get printed, number two is less than ten. Then again it's incremented, and the number becomes a value of three, which again is still less than ten. So we'll get the text printed to the screen, number three is less than ten, and so on. This keeps repeating until the condition is no longer true, or in our case, until we get up to the number nine. Still I'll change it back to one, save, and then refresh. So there we've got to get number one, all the way through to nine, to be less than ten. That's why we needed to insert a break tag. You just saw each one of these is on a separate line, and it's more readable. The next we're going to take a look at a variance of the while loop, and this is called a do while loop. So the while loop that we just looked at, will only work while a condition is true. However, the do while loop will always run once first, before checking if a condition is true. So the code between the curly braces will always run a minimum of once. So let's take a look at how we can do a do while loop in PHP. So we start with the do keyword, and then we can just leave the brackets there, because it is going to run once wherever we do. So we're going to print to the screen the value of the variable number, and then a break tag, so it's on its own line. So this section will always run, and then we can add a while, and this is the condition we are going to test against. So while number again is less than ten. So whatever we do we'll always get number printed to the screen, and then the loop will continue while a condition is true. So because this condition is true, we should get a loop all the way through to nine. In fact you should need to put the increments inside there. So number plus-plus and save and then refresh. So we've got the values all the way through to nine. So let's see what happens if we change this to be the greater than symbol. So of course, one is not greater than ten, so this is false. So using a while loop, we wouldn't get anything printed at all. The two section will always run once, and then we shouldn't get anything after that. So let's test this, and we'll just get the number one because the loop does not repeat. So there's two more types of loops we can look at in PHP, the for loop and the for each loop. So next we're going to take a look at the for loop. For loops are useful for when we know how many times we want to repeat the loop. A while loop will run until a condition is no longer true, whereas a for loop, we need to set how many times the loop will run. So let's take a look at the for loop. So we use the for keyword, and then the brackets, and of course the curly braces, just like the other loops. But this time when it's pass in three parameters into the brackets. So the first value we need to enter is the initializer, and this is effectively going to be like our variable, that we used in the while loop. So I'm going to set the variable once more of number. Then we're going to initialize this to be zero. Then each one of these parameters need to be separated with a semicolon. The second parameter is our condition to test against, so I'm going to use the number variable. We want to test if this is less than, or equal to the value of five. Once again, the semicolon. The third one we're going to use for incrementing. So every time you go through the loop, we're going to increment the variable of number by one on each loop, so number plus, plus. So just like the other loops between the curly braces, we're going to echo the number. So number, and then the variable number, and then the break tag, and don't forget the semicolon at the end. That's what for loop looks like. So once again, we begin by setting an initial value of zero, and every time we loop around if the value is less than or equal to five, if it is, we're going to echo the number, and also increments by one on each loop. So lets save and then refresh. Great so we've got all the values, so we've got the initial value of zero, and then we loop through until we get to the value of five. So the last type of loop we'll look at in this video is the for each loop, it's designed to work on arrays. So in the arrays video, we looked at how to create a array, and how to print a value to the browser, by selecting it by its index number, just like this. So we had an array name shapes, and we set that equal to an array with the values of square, circle, and also triangle. Then if we wanted to display any of these values inside the browser, we needed to echo the array of shapes. Then inside the square brackets, we needed to select the shape by its index number, so zero, one and two. So triangle will be number two. So we get the value of triangle. Creating a new echo for each array item, can be a long boring task. In program, we should always try to avoid repetition. So let's take a look at how we can create a for each loop. So we don't need this echo because we're going to do this inside the loop. So we'll begin a for each loop with the for each keyword. Like the other loops, have a set of brackets and then the curly braces. So the first thing we need to do is pass in the name of the array. So that is the name of shapes. Then every time we loop through the shapes array, we need to store the values inside a new variable. So do this by declaring as, and then a new variable name. So I'm going to call this value. Then let's create our echo. So because all the new values is stored into our value variable, we need to echo out the value variable, and then we'll put a break tag inside there, and a semicolon. So that's how a for each loop is laid out. So every time we loop through the shapes array, will in store these values inside a new variable, which we've called value. Also with each pass of the loop, it also moves on to the next item in the array. This means the loop will continue to loop through all the values of the array until it finds the last one. So let's save this, and then refresh the browser, and there we go. So loop through the first item, the second item, and the third. That's a lot more convenient way of printing out all the values of an array rather than selecting each individual index number. So I hope this video leaves you with a better understanding of how loops work in PHP.