PHP Tutorial (& MySQL) #4 - Variables & Constants

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
alright then gang so the first thing I'd like to talk about in PHP is the idea of variables now variables are used to store bits of information or values or data which we can then recall and use later on in the program for example I could have a variable to store a user's email address and then I could call upon that variable later on if I want to output it to the screen okay so how do we create these variables well first of all we say dollar sign that says to PHP look I'm going to create a variable then we gave the variable a name so if I wanted to store someone's age I could call it age for example or if I want to store their name I could call it name now there are some rules when giving these variables names and they are very simple first of all the variables have to start with either a letter or an underscore so an N or P or an underscore or even a capital n but they can't start with numbers and they can't start with special characters so after the first letter you can use a combination of any letter upper case or lower underscores and numbers but you can't ever use special characters like the outside so this would be a valid variable name something like that yeah it's silly and I wouldn't ever call a variable that name but that would be valid okay now also you might hear something called camelcase variable naming now that basically means that if you have a variable which has two words for example first name it's formatted like this first name and the first letter of the second word is capital this is camel case now you don't have to name them this way you can name them however you like for example you could choose to do an underscore to separate those instead that's entirely up to you but these are just some different conventions for naming variables anyway I'm going to name mine simply name and I'm gonna set that equal to a string which is Yoshi now we've briefly talked about strings we said that they are strings of characters or numbers or special characters like at symbols it doesn't matter contained within these quotes that could be used to store names like this or a sentence or a paragraph or an email and if you like that now we're going to delve into strings into greater deeds in the next couple of tutorials but for now just know that our strings are characters inside and the single quotes or double quotes so we could just as easily do this instead and then Yoshi okay now there is a difference between single quotes and double quotes and we'll talk about that later on but for now it really doesn't matter we can use single audible so anyway we're storing this string now in this name variable now if I wanted to access it I could just say later on echo and then the variable which is dollar sign name okay so that's going to take this value which is stored in the variable and echo which of the screen so let's save that and view this in a browser I'm going to refresh and we see Yoshi right there we see this because that's in the HTML template hard-coded but either way we're now taking this variable and we're echoing it to the screen we can also output this variable inside the HTML template so for example if I come down here and do div inside I could say PHP to do my PHP tags I always have to do that if I'm writing PHP and we could echo the name right here instead so if I save this and refresh now we see the name down here in the HTML template pretty cool right okay so let's do a different variable I'm gonna do age and set that equal to this time not a string but an integer so if I say 30 notice first of all they're a different color right and that's because it's recognizing this as a different datatype this is an integer this is a string now we don't have to store integers or numbers inside quotes there are different datatype we don't need to do that now there's many different types of data type and we're going to talk about them all as we go through this course but know that this is now a string and this is a number either way we can still access this number later on in exactly the same way if I say div and then PHP and echo the age this time it's gonna do pretty much the same thing it's going to take that value and output it to the screen over here cert it all right so that's variables in a nutshell now we can override variables so for example if I wanted to override this name later on in the program I could say dollar sign name is now equal to Mario okay so save that and when we comes out put it down here what do you think it will be well it should be Mario because we've overridden this thing we initially sell it to Yoshi then we set the age then we're overriding the name again by the time we get down here because it runs from top to bottom then the name is now Mario makes sense okay cool now I'm just going to comment a couple of things out and by the way this is how we do comments in PHP just two little forward slashes you could also use a pound sign that comments it out as well but I tend to use forward slash I'm going to comment out this stuff as well and when something's commented out it's not ran okay so the PHP is still there for your kind of memory if you like for your comments but when the PHP file is run on the server then obviously anything commented out is not run okay so now I'd like to talk about constants because we can override this right so it's not constant now say you didn't want that to happen say you wanted to define some kind of variable and you wanted it to remain as that value indefinitely you don't want it to be overridden well in that case you could define a different type of variable called the constant and the way we do that is by using a function called define now I know we've not talked about functions but essentially we just use the defined keyword then parentheses to invoke this function and we will talk about functions later or more and then we set the variable name or the constant name and it's common practice to do this in capitals because then we know it's a constantly to run when we use it and then we set the value of that constant so I'm gonna set this to Yoshi okay now if we want to access this later on we can do we can just output the name here but this time we don't use dollar sign because we didn't appear anywhere we use caps because that's how we defined it and this is how we know later on that this is actually a constant because it looks totally different from this right so now we should output Yoshi to the screen if we refresh we can see Yoshi and now this time if I try to override this if I do this again down here so define name and set this to Mario then we should get an error and this shouldn't work because this is a constant and we're not allowed to override constants right so it's saying here that constant name is already defined now even if we try to override it this way and we say name equals Mario something like that then it's still not going to work if we refresh we can see we get a syntax error now so that's not allowed okay so that's how we create variables and how we store data in them and that's also how we can create constants as well if we don't want them to be overridden
Info
Channel: The Net Ninja
Views: 89,769
Rating: undefined out of 5
Keywords: php, tutorial, php tutorial, php tutorial for beginners, mysql, mysql tutorial, mysql tutorial for beginners, sql, sql tutorial, php for beginners, learn php, php mysql, php and mysql, phpvariables, variables, variable, php constants, constants, constant, php constant, php variable, variable vs constants, variables vs constants
Id: 2CXme275t9k
Channel Id: undefined
Length: 7min 18sec (438 seconds)
Published: Thu Jan 31 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.