PHP Front To Back
Lección 3 de 22 · PHP Front To Back [Part 3] – Variables, Constants & Data Types · TI y software
PHP Front To Back [Part 3] – Variables, Constants & Data Types
hey guys welcome to part 3 of PHP front to back in the last video we've set up PHP Apache and MySQL using xampp and now
we're going to jump in and we're going to take a look at some code so these first couple videos are going to be really basic stuff and I'm sure that a lot of you probably already know some of
this stuff but I would still suggest you watch it and just use it as a refresher all right I have to do it because I want this to be for people that are that know nothing about PHP as well and I want
them to be able to know what's going on so in this video we're going to talk about things like just basic syntax comments variables constants and data types to also look at concatenation and
combining variables as well alright so I'm using sublime text my editor if you guys want to use that you can if you want to use something else atom is another one that I use all the time no
pet notepad plus plus is alright you could even use just Windows notepad but I wouldn't suggest it as it doesn't have you know sufficient highlighting or anything like that
so what I've done here is in my htdocs folder which is my web root I created a folder called PHP sandbox and that's where we're going to create our files for this series so I'm going to create a
new file here and we'll call this variable dot PHP okay we're going to be looking at things other than variables but that's the main focus so that's what I'm going to call it alright I'm going
to open the project up and sublime just a add folder to project and we're going to go to see xampp htdocs' and then PHP sandbox okay so let's open up the variables file alright now if I know
matter what I write in here I can just say hello and save it and then if we go to HTTP localhost / PHP sandbox / variables PHP and we get hello okay
I'm going to make this bigger so it's a PHP file but we can just as well put whatever we want here you can put plain text we can put HTML so if I wrap this in h1 tags it's still parts I'll get rid
of that okay so you can see that it does parse the HTML okay now if we want to write PHP we have to surround it in PHP text look okay now some developers will leave out the ending PHP tag as long as
there's no HTML or anything down here it's not going to cause a problem sometimes it's even recommended to do that so with your preference if you want to keep it or not okay so we're going to
take a look at echo which basically just prints stuff out on the screen so if we say echo hello world and reload we're going to get hello world okay we could just as well wrap this in an h1 if we
want okay we can mix and match PHP with HTML as long as the PHP is wrapped in tags all right I'm just going to get rid of that now another command that does the same thing really is print okay if
we reload that now print the main difference is that echo doesn't return a value print will actually return one so you can use it with an expressions but echo is a little bit faster than print
and that's normally what you would use that's what I see okay so we're going to stick with echo so let's talk about comments okay every programming language has comments and tax so there's
basically three ways to do it in PHP if we want to do a single line comment we can do the double forward slash okay we can also do the number sign and you'll see if I go on another line
it's not a comment anymore now if you want to do multi-line comments you can use the same things active CSS uses which is the slash and then the star and then star slash so this would be a
multi-line with a multi-line comment okay so let's see comment syntax so let's take a look at variables so let's create a variable called output and we'll just set that to a little world
and let's echo it out here okay so a variable is basically just a container so if we save that and reload we get our hello world okay very simple now I want to go over the different the different
rules of variables in PHP alright I'm just going to put a comment here okay so first rule is you have to prefix with the dollar sign in PHP okay you have to do that or it's going to be looked at as
a constant and you're gonna it's not it's not going to work out for you so make sure that you use the dollar sign and then the variable itself has to start with a letter or an underscore
okay and underscore is the only character aside from numbers and letters that you can have and you can start a variable with it okay but you can't start it with a
number so like I said only letters letters numbers and underscores okay and they're also taste sensitive so if we try to start this with a1 like that and we reload we're going to get a parsing
error okay and there's different types of errors in PHP and we'll get into that later on okay if I try to start it with an underscore let's see what happens okay so that works okay because you can
start it with an underscore now there case sensitive so if we if we say output with an uppercase o here and we leave that one lowercase we're going to get an undefined variable error okay so make
sure that you have the right case in your variables so there's different data types in PHP as with most programming languages so let's go over those data types okay so we have strength which is
this here case Oh strings can be wrapped or have to be wrapped in quotes and they can be either single quotes or double quotes all right and I'll go over the difference in a minute we can also have
integers integers are numbers which can be positive or negative so for instance if we say create a variable called num 1 instead of 2 4 okay notice that I didn't put quotes around it because integers do
not have quotes if you put quotes around it it's going to be looked at as a string okay we also have slopes which are decimals so let's say float 1 I'll set it to 4.4.4 okay simple float we
also have boolean boolean's is a true or false so we could say 1 equals true we also have a raise now I'm not going to get into arrays or objects just yet we will go over those later on and then we
also have no and then we also have a resource which could be for instance a return or a reference to a function or something like that ok it's not an actual data type so let's now talk about
combining variables concatenation and using arithmetic so we have this number on here let's say we want to say number two and we'll set that to ten and we can actually add these together if we
say num1 plus num2 and let's set that to a variable called sum okay and then let's tackle out some okay so if we reload that we get 14 and you can use obviously multiplication division
subtraction whenever you want now let's say we want to take two strings and put them together so if we say string 1 equals hello and string 2 equals world and then say we want a variable called
greeting and we set that to string 1 plus string 2 okay let's see what that gives us if we try and echo out greeting okay so that's giving us an error telling us that it's
a non numeric value and we're trying to add it now as a few things we could do here to get the result we want which is hello world one is to use concatenation now when you concatenate two strings
together you want to use a dot okay in JavaScript you would use a plus sign so that would actually work but in PHP use a dot so if we reload that we get hello world now if we want to put a space in
between which you probably do you would have to then concatenate and have string with an empty character basically a space so to do that in the middle here it's going to be a string which needs
quotes and then we're just going to put a space okay so we're concatenate these strings so if we reload now we get that space if we want to put an exclamation mark on the on the back here
well again can cat me to a string and we'll put in our exclamation reload and that works now there's an easier way to do this and that's by using double quotes okay so let's do
greeting will say greeting to equals and let me first show you what happens if we do a single quote so if we just do Greek not Gris string one state string two and then we want to echo out greeting to
just need to save it it actually looks at these as strings okay so single quotes will give you exactly what you put in it's not going to parse the variables however if you put double
quotes and we reload we get hello world okay so the double quotes will actually parse the variable now even though looking at this this is a much easier thing to do than this but you need to
know how to do this as well how to concatenate alright so the next thing we're going to look at is escape sequences so sometimes we want to take away the special meaning that PHP gives
a certain character and to do that we can escape it so let me give you an example so if we say string 3 equals and let's say we want to say they're here so we
would do they and use a single quote here and then our e ok now you can tell right off the bat pictographic ending quote you can tell that something is not right here
and the reason for that is that PHP doesn't understand that we want this whole thing to be one string because it sees this this quote here so it's stopping the string there all right now
for this to actually work well first of all let's try and run it okay if we try and it looks like string three if we try and run it we're going to get an error okay now what we can do is we can escape
this character we can make it basically lose its its meaning its power by putting a backslash in front of it okay so now it's just looking at this as just another character in the string so let's
save that and reload and now we get their hair now you only need to escape it if this is going to match this okay if we were to wrap this in double quotes like that then we actually don't
need to do that okay if we reload we get the same thing however if this was a double quote then we do have to escape it reload and now that works and I think that's good for the basics of variables
if you guys have any other questions you can leave it in the comment and I realized that I can't get to everybody's question so if there's other people that know the answer please don't hesitate to
help them out as well okay so now what I want to look at before we go is consonant variables are just that they're variable they can change constants however you want to use
for values that you absolutely know aren't going to change an example I guess would be like your server name or something like that that's most likely not going to change so you could use a
constant so let's go ahead and create a constant now to create it you need to use the define function so we would say define and constants are a popular practice is to give them all uppercase
so if we say greeting and then the second parameter will be what we want it to be let's say hello everyone okay and then down here if we want to output that we can just say echo greeting let's save
it reload and we get hello everyone okay so you should only use this if you know that this string right here is never going to change now if we were to make this lowercase
and reload we're going to get an error saying it's undefined because by default constants are case-sensitive if you want to make this case insensitive you can by just adding a third parameter of true so
if we save that reload that's going to work okay that's about all there is the constants at least at this point just remember only use them as we know that that value isn't going to change all
right so I think that's it guys I don't want to make this too long hopefully that gives you a good explanation of just you know basic syntax comments variables data types in
the next video we're going to take a look at arrays and we're also going to look at loops okay so that's it and I will see you in the next video
In this video we will look at very basic PHP syntax including tags, comments, variables, constants and concatenation.
CODE: Code For This Video
http://www.traversymedia.com/downloads/phpsandbox/phpsandbox1.zip
COURSES: Premium Eduonix Courses
http://www.traversymedia.com/eduonix-courses
SUPPORT THIS CHANNEL WITH A CUP OF COFFEE PER MONTH:
https://www.patreon.com/traversymedia
ONE TIME DONATIONS:
https://www.paypal.me/traversymedia