Locked lesson.
About this lesson
How to assign things in PHP.
Exercise files
Download this lesson’s related exercise files.
Assignment Operators.docx58.8 KB Assignment Operators - Solution.docx
59 KB
Quick reference
Assignment Operators
Assignment operators assign things to other things.
When to use
You'll often use them with variables, to assign data to a variable.
Instructions
Assignment operators assign. Here's the main list:
=
+=
-=
*=
/=
%=
Hints & tips
- Assignment operators assign
- Equal to = is the most used assignment operator
- 00:04 In our last video we talked about arithmetic operators and math operators.
- 00:08 In this video I wanna talk about assignment operators.
- 00:11 And assignment operators do pretty much what they sound like they would do.
- 00:14 They're going to assign things to other things.
- 00:17 And we've already looked at the main assignment operator, this equal to sign.
- 00:21 We're assigning the number 10 to the variable $number 1.
- 00:25 That's pretty much the most important and the most popular assignment operator.
- 00:29 And very simple we already understand what's going on here, how to use it.
- 00:32 But there are bunch of other ones and
- 00:34 we're gonna talk about those in the rest of this video.
- 00:36 In the last video we talked about math operators.
- 00:39 We can use those same math operators as assignment operators.
- 00:43 And those were basically +, -, x, /, %.
- 00:50 To turn these into assignment operators, we just put this main assignment operator,
- 00:54 the = sign right after each one.
- 00:57 +=, -=, x=, /=, and %=.
- 01:02 So what is going on here?
- 01:03 Let's say we have our number, $number 1 and we assign it 10.
- 01:07 Let's say we wanted to add 5 to it.
- 01:10 Now we could do it like this,
- 01:13 we could go $number 1 = $number 1 + 5.
- 01:18 And then echo $number; don't forget those.
- 01:24 And let's save this and hit Reload and just see.
- 01:28 And boom that's 15.
- 01:29 But this is sort of all complicated, right?
- 01:32 It's a lot of writing and that's a lot of stuff.
- 01:34 And the purpose of programming language is you wanna be as elegant as possible.
- 01:38 Meaning, you wanna write as little code as possible.
- 01:41 Partly, cuz it's easier to read and mostly cuz we're all lazy.
- 01:44 So how can we take this and make it less code?
- 01:47 Well, we're gonna use one of these assignment operators.
- 01:49 And what we're trying to do here is add 5 to our $number 1.
- 01:53 To do that, instead of writing $number 1 = $number 1 + 5,
- 01:59 we can just go $number 1 += 5; what that's doing is
- 02:03 assigning 5 to what is already in $number 1.
- 02:07 We're adding and assigning 5 to 10.
- 02:10 So we save this, come back here and hit Reload.
- 02:13 Oops, We gotta get rid of all those.
- 02:16 Okay so echo $number 1 += 5.
- 02:18 Hit Reload, 15.
- 02:20 If we change this around to 11 += 5, that's gonna be 16.
- 02:24 So, let's change this back and paste these in again.
- 02:28 +, -, x, / and %.
- 02:32 And these all work just the way you would think, -= 5 would be 10.
- 02:36 -5, 5.
- 02:38 Multiply 10 x 5, that's gonna be what?
- 02:43 50?
- 02:44 And on and on and on.
- 02:47 That's gonna be 2.
- 02:48 And finally %, that's gonna be 0 cuz 5 goes into 10 two times with 0 left over.
- 02:55 So, those are assignment operators, very, very useful, very very helpful.
- 02:59 You are gonna use this a lot because there is a lot of times when you just need to
- 03:02 make quick little changes to numbers.
- 03:04 Add 5, add 2,
- 03:05 think of like a shopping card online when somebody adds a new product and you wanna
- 03:10 += their running total of what they're gonna be charged, something like that.
- 03:14 You'll just use these all the time.
- 03:16 And like you've seen very simple, just to post these out again +, -, x, /, and
- 03:21 % assignment operators.
- 03:23 In the next video, we'll talk about comparison operators.
- 03:25 A little bit more complicated but pretty simple and also very useful.
- 03:29 And that's all for this video.
Lesson notes are only available for subscribers.