PHP Tutorial Introduction - Learn PHP Programming

hey guys what's going on its climate icky and in this video I'd like to introduce you to PHP programming let's go ahead and dive right in starting off with a variable names every variable in PHP must start with a dollar sign followed by the variable name the variable name must start with a letter or an underscore it can't start with a number we assign a value to the variable by using an equal operator and in PHP and expression is anything that expresses a value the value would be an expression of itself and once the variable has been assigned a value the variable also becomes an expression now the statement in PHP ends with a semicolon and you can think of statements as any kind of action in programming so in this case the action is simply a sign in a value to the variable and we complete the statement in PHP with a semicolon which is how all the statements and in PHP and also just to get you guys a little bit more familiar with the programming lingo when the variable has been assigned a value we can say that the variable has been defined and initialized and it's ready to use ok let's go ahead and start programming i'm going to pull up the ide here i'm going to be using netbeans with a custom dark effect theme that I've created ok so we start coding PHP by an opening PHP tag and we ended with a closing tag just like that now just to show you an example of comments a single line comment in PHP starts off with two forward slashes and the comments and PHP are just a way for us programmers to understand the code a little bit better and an alternative ways to use a pound for a single line comment now the multi-line comments start off with the forest / star and their ended with a star /and you can type whatever text or code you want in between these and it's going to be ignored by the PHP interpreter just like that now let me go ahead and define some variable names i'm going to go ahead and define some variables here and they're all going to have different data types and data types are just basically value types ok so these are four different data types this is a string datatype this is an integer datatype this is a float data type and finally boolean data type and just to clarify a little bit more there's three categories of data type scalar competent special in PHP scalar you can think of as a single unit of data so those are the ones that I just defined so the first one here is a string which is a text and it's going to be in closing single or double quotes next one is an integer which is a whole number float which is a fractional number and finally boolean which is a 0 or 1 and the bullies can also be expressed is true for one and false 40 now that under the composite you can think of composite as a collection of data so an obvious example is an array which can hold multiple values the second one is object which refers to the instance of the class which I'll cover that thing and the video and finally we have special under special we have a null which is like a like a variable like has been assigned constant now has not been assigned any valley or has been unset and finally there is a resource which holds special kinds of Ali's like database connections ok so those are the data types supported by PHP now let's go ahead and output some of these variable values to the screen I'm going to use a keyword called echo which outputs the values to the screen and i'm just going to say the game game name was release and release here and it now costs costs now at i'm going to create on dollar sign here and at the end i'm going to use a VR HTML tag which is a tag used to start another line in a browser ok now i'm going to go to the output and as you can see all the variable values are been printed out to the screen just like we wanted to now if i copy this whole line here and i enclose it in single quotes the single quotes and PHP are also known as little quotes i'm going to go back to the output and now it's literally printing out the variable names instead of values which is why single clothes are also known as little quote so I'm just going to come at this line out here so basically using double close allows you to use the variables inside those double quotes ok let me show you what constants are so there's two ways to define a constant PHP one way is to use a built-in function called define and the functions in PHP whenever you call a function you use the round brackets in this case the values are going to be constant name enclosed in the quotes separated by a comma is the constant value so that's one way to find constant and the second way which I actually prefer is to use the construct or the keyword const followed by the constant name and then just like the variable we use the eco operator and then the value and also by convention all constant names are uppercase and never prevent a constant with a dollar sign unlike the variable and not a really important thing to note about constant is that just like the name says constant constant never change once they have been defined ok so I'm going to actually create a constant out of this BR tag because i'm going to be using it a lot throughout this video and i'm going to use the constant keyword method and i'm going to name the constant BR and just place the our tag inside the constant now you can just use this constant inside the core with so I'm going to have to use something called concatenate and concatenate basically just adds value to the string of texts just like that and we use a . to join the string of texts together so . and then the constant name and if i go here and refresh the page nothing changes the end if i go to view source you can see that the BR tag is still being printed out so everything is working just like we expected it to now let me just show you guys arithmetic operators real quick now all of these are mostly self-explanatory and if you use plus or minus four x our division or modulus or even expansion which was introduced in PHP 5.6 all of these will work just likes you expect them to the negation is just going to give you a negative variable or negative number now the one that I was just one explain real quick is identity and here it says conversion of variable to integer float as appropriate let me just explain what that means and a little more detail so if i had that variable and it had a number as a string here let's create a float number if i use a built-in function called bar underscored dump which is a function we can use to find out what the data types are function to find out what the data types are in PHP and i'm going to use i'm going to pass this number able inside this function go back to the output refresh the page as you can see PHP is telling us that it's a string datatype with six characters 1234 456 ok so if i put the plus sign operator in front of it it's going to convert it to float so it's just a useful way to convert string numbers two integers or flows and it can be useful in some situations ok so that's what it is just so you guys know now let's go ahead and just another example so if this function was for example just a hundred if the valley was just 100 it was converted to integer so that's how that works okay all right now let's go ahead and move onto eres now there's two types of areas in PHP numeric and associated and the numeric area is created we create the Ares with their work with a keyword erre or rather array is the proper way to say it and then we use the round brackets and we list the values of the array with comma separated we we separate the valleys inside their array with a comma now the word numeric refers to not the valleys but to the array keys and just to clarify here this is what that array an example would look like so whenever create and numeric area like that the keys are automatically created and the key count starts with 0 so you can think of keys as like an identifier for all the values so the valley for would be identified with the key 0 the valley 3 you would be identified by key one Valley five would be identified with key 2 etcetera and when this array is created like this all the keys all the numeric keys are automatically created for us so we'd have to worry about that okay so that's numeric area and the associated very we actually specify what the key name is and we enclose it in either single or double quotes and we use this rocket operator to assign whatever value of that name key is going to be so this is an example of what the associative array would look like you would have a name key and that key would identify value in this case starcraft ii would identify value 1998 the witcher of key would identify variable here etc ok so those are their numeric and associated areas now let me show you guys some examples let's just go ahead and create an array called game genres using the air a keyword and i'm just going to put a bunch of string values inside this array string out data types rather and see here action puzzle strength to wrap the g MMO RPG and FPS so those are just different kinds of games onerous and then what I'm going to do here and I'm just as i'm going to use the equity word to try to print out this and airy to the screen so echo and then the name of our variable air a and here I'm going to get this notice area to string conversion that's because we cannot directly access the array in PHP in order to access the array keys we would use square brackets so for example in this case the key here is of the RPG for example keys the key of the RPG is 0 right 0 is RPG adventure is one action is to set all right so here's what we do we just use square brackets and we put the key in side of the square brackets and that access is the value of that key so if we go back as you can see the RPG has been printed out which is the value of ki 0 just like we have in our a if I put one here the next key is going to be accessed the next value is going to be accessed just going to be an adventure so now you can clearly see how all these Keys have been automatically generated for us and we can access all the values in the array using those keys now let me just show you an example of a multi-dimensional array so a multi-dimensional array is just a array which has an array within itself so if I created in order ray out of these three values here now what this array would look like is this so first of all is the key 04 RPG but the next key which is one is going to have a data type array and we can't access that directly so if i tried something like this again you can see that we're trying to again access the array directly and we getting this notice because we can do that so what we do here is we specify we append the square brackets with more scripts square brackets and then we access whatever the keys are inside the second array so in this case let's access adventure which is zero and then that would access adventure so just like shown in this table here first we're accessing key one but that's a data type array so we can access that directly this data type array has its own keys so we access its its its key with more square brackets put the key value inside of those square brackets and that's how we access its values so those are multi-dimensional arrays and if i wanted to access more keys i would simply go one and two and i'm just going to pretend this with our BR constant here using concatenate and this should print out all of our values yep adventure action and puzzle and just to also going to print BR here so we get get all the valves printed out like this okay so as you can see we're accessing all over this this secondary skis 0 1 and 2 an adventure action puzzle has been printed out so that's how we access our a skis and that's how we access multi-dimensional arrays now let's just let's create associated berries she ate it hurry ok so i'm just going to create an area called going to create a variable area called years same using the Cherokee word and then this array i'm going to go ahead and create game names along with their release years and i'm going to use the rocket to specify what the values are those keys are the wheelchair rocket was released in $MONTH 2009 comma the mass effect 3 and here i'm just going to use the constant which we have created earlier we chose the valley of $MONTH 2013 and finally Diablo i'm going to go to the next line here rocket 1996 ok so just like in the example earlier we create our key name use the rocket and specify the value of the key so if i was going if I'm going to print out this airy and actually we can print out the whole area using a function called print underscore are so this is exactly what i'm going to do here and i'm also going to use HTML tags called pre to format the air a little bit nicer go back to the output and get this error because something you forgot to do something here online 57 ok so comma here ok so i put the rocket inside the quotes okay there you go refresh ok so wait now the constant is freaking out all k so i forgot to i'm actually going to create a constant out of this here just forgot to do this and I'm going to use the other technique so what I forgot to show you guys release year and i'm just going to put the value of 2013 so this is just another way to define a constant as you remember from the constant example so now we have the constant release year and it holds the value of 2013 so now everything should work undefined variable release here this is all this is what i was going to do okay so i'm just going to print this release year constant in the middle of this quote so in order to print the constant here in this string i used concatenation on the left side on the right side and I also closed the quote on each side as well so that's how you would print out the constant in the middle of the string ok ok let's go back to the print our years and now we're not getting any errors finally and you can see that the keys and values have been printed out and in this case instead of numeric keys we have named keys and their associated with their respective values so this is our associated very now the eres can also hold different data types so for example in this case you can see that the air a eris keys can hold variables and constants and numbers but they can also hold strings and other data types so just mentioned that real quick okay let's keep going here and we've covered the numeric areas associated areas and multi-dimensional eres now if i was going to print out this game John or right here see what happens ok i'm also going to use this pre-owned our tax so this format on the each individual line ok you can see how this multi-dimensional area's been printed out with their numeric keys and for the numeric e1 the data type is an array just like was shown in our table here one is array and it's has its own keys and here has been printed out just like shown in the chart ok so that's it for the eres Nellis wanted functions so here I'm actually going to create the function but before I do let me show you the function syntax in PHP we define a function and construct is just another word for a keyword so we use the keyword function followed by the function name and then the round brackets are used and inside the round brackets we have our arguments separated by commas now you can think of arguments just like them as just like the variables and after the round brackets we have curly brackets and inside the curly brackets we have a group of statements which is basically functions code which is going to be performed when the function is called now when the function is called we pass the parameters which will act just like the variables and these variables will hold values so they're called expressions hence the name expression1 and expression 2 and then these parameters get passed into functions arguments so the first parameter is going to get past the first argument and the second parameter is going to get past the second argument and then inside functions inside this function the arguments are going to be used to do whatever we want with those with those arguments ok so i'm going to create a function just spelled a function right here using the function keyword and i'm going to call it pretty underscore art and music early and round brackets i'm going to pass one parameter or argument here called array and this function what is going to do is just going to print out the pre tags automatically for us so we don't have to do it every time and as you can see we're using the were also using the print our built-in function to print out the actual array but what it does is additionally it echoes out the pre tags automatically for us ok so let's utilize this function let's go ahead and delete these pre our tags we don't need them anymore and just utilize our pre our function which will also clean up our code a little bit more and go back to the output and nothing is nothing should change just like expected everything remains the same but now we won't have to print those property tags yeah i'll just use the pre our function and now we have successfully defined the user I user-defined function and just to show you guys another example of the function i'm just going to create a function called add which is just going to have to arguments X & Y and it's going to add them together and return them so i'm just going to use the + operator and the return keyword basically returns the value of the function to us so if we're going to do something like Nam equals 100 now add 100 to the num is now people's just to give you a common the numbers now 200 economic we are constant now the number that we should be printed out is 200 yep ok so it just returns the value which is calculated from the function and we can do this because I the return value is going to be whatever is returned by the function in this case a hundred and hundred plus 100 equals 200 and that we print out the value to the screen ok so that's the difference between printed out function values as opposed to return them all right let's see what's next next is conditional expressions now the conditional expression is just any expression which returns true or false that's as simple as that so 10 is greater than 5 is true that's conditional expression which evaluates to true 10 is less than five that is false so this is a conditional expression which evaluates to false now the truth expression comparison expression and boolean expression all mean the same thing as a conditional expression alright guys we're now ready to move onto if statements ok so here's our the if statement works so we use the keyword if and then inside the statement we have we have a conditional expression or we can have multiple conditional expressions like shown here and end is a operator it's called it's called a is that a comparison now so let me see what is it area / is assignment comparison ok it's called the logical operator so and true if both a and B are true so we have two conditional expressions here age is less than 21 and age is greater than 18 so if this if both of these are true then our statement is going to be executed so agent if the ages 19 or 20 which is between 21 which is between 21 and 18 and then the statements going to be executed next we have an else if another condition is true so if ages over 21 then the next statement will be executed in else if ages less than or equal to 18 then this statement will be executed so you can put you can think of it as if expression evaluates to true our first experience statement is executed etc if this one is true this one is executed if both of these are false this else is I guess executed so let me just show you a quick example here for example if 18 is less than 21 echo it's true so so this will evaluate to true and should print X it's true yes it does ok so next one is if 18 is less than 21 same thing going to use sending example you do that but else if it's not so if it's false do something else eka it's false and statement with a semicolon echo it's true so if for example if if if i put 18 is greater than 21 here then it's going to print out its false because we have a false condition it's false ok so that's how that works and finally i'm just going to copy and paste this code here we have Alice if so if this expression is true then it's going to execute this statement if the next expression is true is going to execute this statement and if that expression is true that i execute this statement and it's nothing up none of those are true it's going to execute this one so you can have a nested-if and else if conditions and you can have as many as you want in this case release year is equal to 2013 so is the year greater than 2010 yes it is so the game was released after the year 2010 is going to be printed out the game was released after the 20 2010 I mean after 2010 yep ok so that's how that works and finally I wanted to show you guys another conditional called switch and switch is like a it's like a short version of a bunch of else if statements so if the expression we evaluate expression and if it equals 0 then execute the statement and then we use a keyword break to exit from the switch statement and if it's one execute this one and if it's to execute that one so etc and it's important to use break at the end of each case statement because otherwise it will just keep executing and printed out all these statements so let's just go ahead and give you example of us which so for example going to define a variable named best gain and I'm gonna have a value stream valley of starcraft and then i'm going to just place this code here so in the case the game is starcraft the best strategy game of all time you're on Blizzard is going to be printed out then we break to exit the switch in cases the witcher this code is going to be executed etc so in our example since the best game of starcraft k starcraft the statement is going to be executed the best strategy game all-time Iraq blizzard ok so those are conditionals and now let's move onto loops the first look I want to show you guys is the for loop so we got three expressions here in the first expression we define our counter and the next expression we have a conditional expression which is going to define when the loop is going to be ended so until this condition is true the loop is going to keep going and finally we have a modifier of the counter and expression three which we either increase or decrease the counter or modified and the other way so the local continue executing statements within the curly brackets as long as the conditional expression which is expression to evaluates to true when it evaluates to false the loop will end and the I duration in the loop is something that repeats the code so every I duration of the code is repeated the expression one is only executed during the first iteration so whenever when the loop is run the first time this expression one is a red but during the next I duration this expression one is ignoring and the easiest way to understand any loop is if expression is true then continue looking and if its expression to is false then exit the loop all right now let me show you an example just clear out this code here a little bit ok so for example for I equals 0 I is less than or equal to 10 so just a classic example i plus plus and then we're going to saute I and use the BR constant so this is going to print out the i 10 x 0 123456789 10 just like we wanted to and that's the basic syntax for the for loop now let's go ahead and look through our multi-dimensional airy that we have created earlier ok so I'm going to say four key equals 0 ki less than count and then our games honors genres are a c++ so basically what I'm doing is I'm using the function count which counts how many how many elements are inside the array in this case it's going to be common elements 12345 so it's going to read the count and game and runner runners is going to return five and it's going to keep looping just going to comment this countess five and then i'm going to echo out going to hell go ahead and echo out games honors genres key and then use the BR constant and then let's see what happens notice aerate the string conversion so what happened is we try to access that key one that we talked about earlier and it didn't print it out because well it's trying to access the array data type but i did print out strategy mm RPG and FPS which means let's see if we go back here strategy mmm RPG and FPS now why did a skip RPG want to i skipped RPG because key 0 let me just cut this out and bring it back here ok so RPG should have been printed out but it didn't strategy of BS ok I'm just going to debug this in Oh her ok so it messed up okay all right well let's go ahead and actually in order to print out this multi-dimensional area we need to check if key one is data type array and there's a function to do that so we're going to go ahead and create an if statement inside this loop and say if is err a game genres key then we want to create another loop and this one is going to go key to equal zero key to less than count game runners and is going to count the key and increase the key by one and then echo out that game generous right here using that multi AB square bracket syntax that we talked about earlier ok so that's going to brl if statement else else is just going to print out the Regular Air a just like that and this should work yep now let's bring out all the multi dimensional variables now guys this is a little bit advanced here but just go through it and look at this chart a few more times and you'll get it so when we get to the key one we're just checking if it's an array just like specified by function is underscore erre and here we have a key so we know that we have that were accessing key one here and now when it gets to one that is area is true so we create another loop to loop through all the values of our secondary and our secondary has three values so the count is going to be 3 instead of five so the count count is 3 & here count count equals count is five so because the secondary only has three keys ok alright so and then we use the the double we set the 2nd t20 and we also look through it by using the second set of five square brackets so just a play around with a little bit more and look at the chart and eventually understand what's happening ok let's keep going here finally I want to show you the for each loop and for each loop we use it to look through the associated very so this area here because we can use the for loop since for loop only looks through numeric keys ok so I'm going to look 30 years as game here this is our for each loop let me pull up syntax chart ok for each expression one expression to an expression three so expression 2 is going to have the key of the airy and then this variable key is going to get assigned to the actual key off the array and then the expression three which is a value variable is going to be assigned a value so let me print out an example so echo game was released and year and then the BR constant I ok so the game is going to get assigned the actual the actual value of the key and the year is going to be value so let's see here ok so the key announced their own anyway let's see what happens here ok starcraft was released in $MONTH 1998 so starcraft is a key 1998 is value the which was released in $MONTH 2009 the witcher's the key 2009 is a value the mass effect is a key the mass effect 3 is a key 2013 is value and then finally dabbler is the key and 1996 it is the value so you can see how the game has been assigned the key and the year has been assigned a value so that's how you look through an associated very and then finally there's two other loops in PHP called while and do-while and they work very similar to the for-loop basically what they say is while expression is true keep looping and then the do-while basically the difference between these two is do while execute the statement during the first iteration for sure while the work while the while loop first iteration is not guaranteed so let me just show you a quick example of the that not to get too confusing just going to paste these loops here so the first one I is less than 10 keep printing I i plus plus and start the i-10 and then the second one I see basically the same thing print out the sea okay so we got two loops printed out doing the same thing and just to show you the difference if I did this here so i change the operators the greater than this would be immediately falls here so you want to print anything but in a do while it would print it out because because the condition is evaluated at the end of the I duration so watch so 0 has been printed out by the do-while loop because first we do so we print out the statement which is 0 c equals 0 and then at the end we do their comparison I duration so that's the difference between need to these two i hope that makes sense finally I wanted to show you guys what classes and objects are all about and PHP so here is a very basic syntax of the object and the difference between class and object you can think of an art class as an abstract concept like a for example an animal or a car and an object is something that lives in the real world like a card type itself a red Ferrari for example and when you when we create the object we use a new keyword which creates the object instance of the class and when that happens the object inherits all hold the variables and functions of that class now the variables in the class out are called properties and the functions that called methods and we use the this object operator will just call it a dart to access all the variables and functions inside the class ok so objects and classes are a little bit advanced and I think if i show you an example it will make a lot more sense so here we go let's define a class called car and we define every classes in class keyword and then we're just going to create some properties which are the variables inside the class and we use varkey order to do to do that so we're going to have our make bar type bar color bar max speed so those are all their properties are variables and then i'm also going to define that constant ER because we can use the original one because this is inside the class which is different scope and then we're going to define functions as well inside this class and we're going to output some messages now just to make this video little shorter I'm just gonna copy and paste all this code here and as you can see all our variables and then there's a bunch of functions and what these functions do is they basically output text message for example accelerate this this color this make this type is now accelerating so it's going to have the values of the object place them here in place of the this color is going to be the actual color of the car instead of this make is going to be the actual make of the car and instead of this type is going to be the actual type of the car and the way so this is a car class clock car and it's an it's like a abstract concept right now in order to actually create our car here's what we do example let's define a variable called acura and assign and create the object using the new keyboard new car car is our class so now the object has been created object is created and now we can do something like this we can access those properties using the dart and assign all kinds of values to our properties gallery goes black and then and then I'm just gonna paste all these other things here so basically what i'm doing here is i'm taking up creating the copy are creating the instance of the car class and i'm creating an object out of it soak accurate is known object acura is now an object data type and then I'm assigning all the variables which are present inside the class and i'm creating the values out of those variables and then i'm also using the same dark symbol calling all the functions which are also part of the class and if we go to the output the black acura RSX is now accelerating is honking loudly has turned on the right signal so all these functions are being used and the values are been replaced by the actual values that we have created in the class and finally it's using the information function which prints out all the information about the car to the screen so make accurate type-r sex color black max beyond 40 okay hopefully this is not too confusing i will cover classes and objects in another video but i just wanted to give you guys a feel for what the classes and objects are all about and just think of them as think of classes as a collection of variables and functions and objects as basically a copy of the class or more correctly an instance of that class which can utilize all of its variables and functions and we can provide the values for whatever properties that class has and we can invoke or we can call all the functions which that class has as well once we have our object ok so you can play around with this a little bit more in I think you'll understand how it works and this is a very basic class in PHP okay let me see what else I got here ok guys this concludes the introduction video to PHP now before you go before you go there's a lot of great graphics like a bunch of these cheats that I've created which we have which you've seen in this video and I've also created a bunch of study guides for you with all kinds of arithmetic operators array operators Simon operators comparison operators etc also created these tables where we covered eres so you can understand these a little bit more and I've also have a dark effect theme which have used in the netbeans ide here here's what it looks like so that's pretty cool right so you can go ahead and download all these in the link one sec okay so these are all the graphics and study guides and cheats and themes they can download by clicking on this link here and you will be asked for your email address and don't worry i won't be sending you any spam how only be sending you great videos like this with all the awesome graphics and downloads in the future so that you never have to subscribe to get again so go ahead and the download all that and lastly please like and share this video with your friends and also make sure to watch the next video in which i'll be covering PHP variables in more detail till next time clever taking out