Start learning today, and be successful in your academic & professional career. Start Today!

• ## Related Services

### Start Learning Now

Our free lessons will get you started (Adobe Flash® required).

### Membership Overview

• *Ask questions and get answers from the community and our teachers!
• Practice questions with step-by-step solutions.
• Track your course viewing progress.
• Learn at your own pace... anytime, anywhere!

### Expression & Operators

• An expression is anything in PHP that has a value.
• An operator takes one or more expressions, called operands , and performs some operation on them to yield another expression.
• Operators can be divided according to how many values they operate on:
• unary operators: single value
• binary operators: two values
• ternary operators: three values
• Assignment operators (=, =>) are binary operators used to assign the value of one expression to another expression .
• There are arithmetic operators for addition, subtraction, multiplication, division, and negation. There is also the modulus (%) operator used for getting the remainder of a division operation.
• The increment/decrement operators are used to increment or decrement their operand by one. They come in both pre and post forms.
• The concatenation operator (.) is used to combine two strings into one.
• PHP provides combined assignment operators for all of the binary arithmetic operators, as well as the concatenation operator.
• Operators have both a precedence and associativity .
• When an expression contains multiple operations, the operations with higher precedence are completed first. If an expression contains two operations of equal precedence , then the associativity of the operations determines which operation is completed first.

### Expression & Operators

Lecture Slides are screen-captured images of important points in the lecture. Students can download and print out these lecture slide images to do practice problems as well as take notes while watching the lecture.

• Intro 0:00
• Lesson Overview 0:10
• Lesson Overview
• Expressions 0:41
• Expressions Definition
• Example: Literals
• Example: Variables
• Operators 1:44
• Operators Definition
• Unary, Binary, and Ternary Operators
• Assignment Operators 2:52
• Assignment Operators
• Array Assignment Operator
• Arithmetic Operators 6:15
• Operators for Common Arithmetic Operations
• Modulus Operator
• Arithmetic Operators Example
• Increment/Decrement Operators 10:48
• Increment/Decrement Operators
• Pre- and Post- Increment/Decrement
• Coding Example
• Combined Assignment Operators 16:44
• Combined Assignment Operators
• Combined Assignment Operators Examples
• Coding Example
• String Operators 20:28
• Concatenation Operator, String Variables, and String Literals
• String Operators Example
• Precedence & Associativity 23:40
• Precedence & Associativity
• Expression Containing Multiple Operations
• Expression Containing Two Operations of Equal Precedence
• Using Parentheses to Force Precedence
• Precedence & Associativity Review
• Homework Challenge 31:08
• Homework Challenge

### Transcription: Expression & Operators

Hello, and welcome back to Educator.com's Introduction to PHP course.0000

In today's lesson, we are going to be covering what are known as expressions and operators.0005

Specifically, we are going to be going over what expressions are and what operators are.0012

Then, we are going to go through a number of different operators that are available (and operators are things like addition, subtraction, and so forth).0018

And so, we are going to have a couple of slides going over a number of different operators available in PHP.0025

We are also going to go over a topic known as precedence and associativity,0031

which is like rules that apply when you do multiple operations at the same time.0036

What is an expression? That is part of what the lesson is titled.0042

Well, in PHP, an expression is anything that has a value; so literals have values, variables have values--they are all considered expressions.0046

For example, here we have three different literals: an integer, a float, and a string.0056

Those, obviously, are literal values; they have a value; they are considered expressions in PHP.0060

Additionally, variables have values; you set values to them.0066

Here we have an example of three different variables; we have a regular variable, an indexed array, and an associative array.0070

All of those have values, and they are considered expressions in PHP.0080

In this lesson, we are also going to be learning about operations, which are expressions in themselves, which means they have a value.0084

And then, in other lessons, we are going to be learning about constants, functions, and objects, all of which are expressions, as well.0092

And again, "expression" is just a synonym for something that has a value.0099

Operators work on expressions; an operator will take one or more expressions or values, which are referred to as operands,0105

which you may remember from your algebra days.0116

It performs an operation on those two expressions, those two operands, to yield another value for another expression.0119

The operators can be divided into categories, based on the number of operands they work on.0128

The ones that you are probably most familiar with would be something like an addition; you have 5+1.0133

It is what is called a binary operator; it has two operands--it has the 5 and the 1 that it operates on to yield a value.0138

In PHP, there are a couple of what are known as unary operators; and they are operators that work on a single value.0148

The majority of the operators we will see are binary operators for things like addition, subtraction, and multiplication.0155

PHP has one operator that has three values, known as a ternary operator.0162

But the majority of the operators are all binary.0168

The first operator we are going to specifically talk about is the assignment operator, and this is something we have already been using in our code.0174

We have already been using an operator, and maybe didn't even realize it.0182

An assignment operator is a binary operator, and as you know, it is used to assign the value of something to a variable, for example.0186

Basically, what that means in operator parlance: it is assigning the value of one expression to another expression.0195

For example, with the assignment operator (which we know is the equal sign), we have this expression here, 98.6, a float,0204

and we have this expression over here, bodyTemp, the variable.0211

The assignment operator says "Take this value and assign it to the variable on the left."0215

We have two expressions there.0225

Also, we have seen the array assignment operator, which is the equal sign followed by the greater than.0228

We have seen that in associative arrays where we have a key, and the way we associate a value in this case...0235

maybe Sunday it was 75 degrees; we associate the number with it using the array assignment operator.0241

And again, it takes the expression on the right-hand side and assigns it to the expression on the left-hand side.0247

Now, because both of these operators (the equal and the array assignment operator) are operators,0255

and we learn that operators work on one or more expressions to yield another expression,0262

these operations that we have seen up here (let me erase this) actually have a value.0268

It may not seem like it does, because normally you have something like 5+1, and you know that you have two operands and it yields a value of 6.0275

Well, this--because it is an operator, it has two expressions; it is a binary operator; it actually has a value itself.0284

This whole operation itself is an expression and has a value.0291

For the assignment operator, the value of the operation is the value being assigned.0296

And the way this might seem more apparent is: let's say we want to set a and b both equal to 5.0301

We have an expression like this, which is a legal statement in PHP.0315

PHP, with assignment operators, evaluates the one on the right first.0323

So, what PHP is going to do is: it is going to take this operation; it is going to assign the value 5 to the variable b.0327

And the value that that expression yields (which, for the assignment operator, is the value being assigned) is 5, is going to be assigned to a,0336

which makes sense, but it is essentially as if we had written it like this--if we had done it explicitly,0346

because when we assigned the value of a to the value of the assignment operation b=5, a gets assigned the value of 5.0362

There are arithmetic operators in PHP, as in most programming languages; and they have operators for all of the different typical arithmetic functions.0376

There is an addition operator and a subtraction operator.0387

You can do multiplication and division; and in PHP, like most programming languages, the multiplication operator is an asterisk.0390

So, if you want to multiply two numbers together, you would write it like...5 times 5 would look like that.0400

And the division operator is a forward slash; and so, if you want it to say 6 divided by 3, you would write it like this.0408

These four are all binary operators: addition, subtraction, multiplication, and division.0418

There is also the negation operator, which is a unary operator; that just takes the value of its operand and returns the negative version of it.0425

For example, if we have a=-5, this minus sign is the negation operator; it is saying to take the value of its lone operand, 5,0436

make it negative, and then the expression of value that that operation yields is -5, and so -5 is what is going to get set to a.0452

There is also another arithmetic operator known as the modulus operator, which is specified by the percent sign.0463

It is not as well known, but what it does is: it is basically a remainder function, so when you divide your numbers, it provides the remainder.0471

For example, if we have 7, modulus operator, 3, the result is going to equal 1; and the reason for that is:0480

if we divide 7 by 3 (go back to our elementary school math days), and we get 1, the remainder is 1.0487

And so, that is what this function outputs; that is a function to output remainders.0499

Let's take a look at a little code to demonstrate some of the different operators, which we will be able to look up from the Educator website,0505

basically just showing all of the different operators in action.0515

We are going to look at the HTML version of the code, because it's prettier and easier to look at.0519

For the addition operator, it is simply: as a binary operator, you take a value on the left and a value on the right.0526

It adds them together, and the result is, in this case, assigned to the variable a.0532

Here, if we have num=1, and then we add 1 to it, it is going to equal to, and a gets equal to 2.0538

All of the other arithmetic operators work intuitively the same sort of way.0545

For subtraction, if we have num set to 5, and we subtract 1 from it, the result of this operation is going to be the expression or value 4,0549

so a is going to get set to the value of 4.0559

And so on for multiplication and division: 2 times 2 equals 4; 6 divided by 3 equals 2; this is fairly intuitive.0562

The negation operator, which, as we mentioned before, is a minus sign, which is the same as the subtraction operator--0570

the way it is determined what it is going to be used as is the context it's in.0577

If it has two operands, it is treated as a binary operator, which means it's a subtraction operation.0580

Down here, it has one operand, the num variable; and so, this operator, the negation operator (let's blow this up a little bit) here--0588

the minus sign--says "Take the value of my one operand, num; negate it, and then set it equal to a."0598

So, in this case, a is going to be equal to -1.0608

And down here, we have this same modulus operation we had shown on the previous slide, 7%3.0611

That results in a remainder...when you divide 3 into 7, you get a remainder of 1, so a, in this case, is going to equal 1.0621

Just one thing to note is that the equation 7%3=1 is spoken "seven mod three equals one"; that is how you say it.0627

Like here, you would say, for division, "five divided by five equals," if it was a modulus operator, you would say, "seven mod three."0636

And so, that is how you speak it.0645

There are also what are known as the increment and decrement operators; what these operators do is:0650

they are unary operators--they operate on one value--and they increment or decrement the value of their operand by 1.0656

For example...first of all, they are represented by two plus signs and two minus signs together; this is the increment operator, and this is the decrement operator.0665

For example, we have variable i=0; right now, at the end of this statement, the value of i is equal to 0.0678

Now, we are going to perform the increment operation; we take the variable i, and we prepend it with the ++ operator,0687

the increment operator, which says, "Take my operand and increment it by one."0697

So, because i was equal to 0, and we have incremented by 1, at the end of this operation, i=1.0704

Likewise, for the decrement operator, it does the opposite: it subtracts 1; so this says, "Take the value of i and subtract 1 from it."0711

And again, that is two minus signs.0721

And so, because i previously was one, and we decrement it, i is now equal to 0.0722

There are kind of two operations going on at once here: i++, which...we will see there are actually two forms of the increment and decrement operators...0728

there is a pre form and the post form; this is the post form; they essentially mean the same thing, as far as incrementing or decrementing goes.0740

This says, "Increment the value of i."0746

It is essentially equal to if we had written the statement i=i+1; so, it's kind of an addition operation (we take the value of i; we add 1 to it),0749

and then an assignment operation, where we assign it back into itself.0759

Now, as mentioned, there is a pre form and a post form, and that is...either the operator goes before the variable, or it goes after the variable.0764

What that does is: because this is a combined operator of an addition and an assignment, whether you do a pre operator or a post operator0776

determines what value is yielded by this operation, because we know operations take one expression,0784

perform some operation on it, and yield another expression.0793

Well, with the pre increment operator, the value of the expression ++j is the value of j after it has been incremented.0796

So, if we assume j is 0 to start off with, and then we use the pre increment operator and set it equal to 1,0805

j is going to be equal to 1, because it has been incremented; and num is also going to be equal to 1.0813

Now, if instead we used the post increment version, where we have the operator after the variable name,0818

with the post increment operator, the value of the operation is the value of the operand before it is incremented.0826

So, right now, j is equal to 1 from the previous line where we incremented 0 to 1, the value of this operation,0835

because it's a post decrement, is j before it gets incremented.0843

So, num2 is actually going to get set to the value of j beforehand, which is 1.0848

So, num2 is going to equal 1; then j gets incremented, and j equals 2.0853

It is two different ways of using those operators.0859

Typically, you don't see the post one as often; typically you see the pre increment one, because it's a little less confusing.0864

When you use the post increment operator, I personally don't like to use it in my code, because it makes things a little less intuitive.0874

It adds some complexity to your code, because you have to think, "Well, is this setting the value before I've incremented it, or after it?"0882

So, in general, you don't see the post operators that much.0888

The thing that we will be using these operators for, mostly, is when we get to loops, which are ways to repeatedly go over lines of code.0894

Basically, they use a counter, and we use these pre increment operators0904

to continually add one to the counter each time it goes through the loop, for example.0909

If we look at another page that demonstrates some of these operations, we can see some of the same things that we went over here.0915

We have a variable num=1, and we pre-increment it; the value of num is going to be incremented, so num is going to be equal to 2.0924

Then, that value is going to be set to a, so a is going to be equal to 2.0933

We reset the value of num to 1, and then we set a equal to the value of the post increment operation on the variable num.0937

Well, the value of the post increment operation is the value of the variable before it's incremented,0947

so a is going to get set equal to num, which here is 1, so a is equal to 1.0952

Then, num is going to be incremented, so num equals 2.0956

So here, in this one, a and num end up having the same value at the end of this statement; here they have different values.0959

This, likewise, applies for the decrement operators, as well.0967

So, for example, if we have num=1, and we do a pre decrement on it, both a and num end up having the value of 0.0970

Now, with the post decrement operator, it's just like with the post increment operator.0981

The value of this operation is the value of num before it's decremented, so a is going to be equal to 1, because that is what num is set to.0985

And then, after num gets decremented, num gets set to the value of 0.0995

Those are the increment and decrement operators.1000

There are also what are known as combined assignment operators, kind of like what we just saw with the increment operator.1006

PHP has them for each of the different arithmetic functions, so they each have the corresponding combined assignment operator.1013

For example, the addition combined assignment operator is a + followed by an equal sign.1022

This is the one for subtraction, multiplication, division, and modulus.1026

Basically, it is kind of like when we had i++, we said that was the same as if we did i=i+1; that is kind of how these combined assignment operators work.1031

So, if we have num+=10, it is the same as saying "num is equal to the value of num, plus 10..."1050

What it does is adds 10 to whatever num is, and then reassigns the value to that variable.1063

It is kind of like the increment operator, except that here we have 10, and the increment operator always increments (or decrements) by 1.1072

Here, with the += operator, we can add however much we want to the variable.1080

This assignment is equivalent to this; and again, it's a combined assignment operation, because the first thing you have1089

is an addition operation that occurs: you take the number, and you add 10 to it.1095

The result of that--the value of that operation--is then assigned back into num.1099

Here are some more examples: if we start off with num=10, and then we do num+=10, at the end of this statement,1104

the value num is going to be equal to 20, because we have taken num's value, which was 10, added 10 to it, and reassigned it back to num.1112

Now, if we use, for example, the multiplication combined assignment operator, this says,1122

"Take num, multiply it by 5, and then store the result of that back in num."1128

So, up here, num was equal to 20 at the end of this statement, so 20 times 5 is 100; so at the end of this operation, num equals 100.1134

A similar pattern follows; if we do num and then use the combined division assignment operator,1144

it is going to say, "Take the value of num (which is 100), divide it by 4, and set that back to be the value of num."1151

So in this case, num equals 25.1156

And then, the modulus operator--the same way: it is going to say, "Take the value of num (which is 25); mod it with 6;1158

and store the result back in num," and just to show the mod operator again: you do...6 goes into 25; we get a remainder of 1.1166

So, that is why this is equal to 1.1176

We have a page that demonstrates this as well; and this page kind of echoes what we have just done.1180

It shows how all of these look in code; we have a variable name, and then here, we have done the += operator.1188

That is going to result in the value of num, incremented by 10.1196

Here we have the -= operator, which is going to say "subtract 10 from the previous value of num and set num to that value."1202

So, num is going to be equal to 1 here.1208

Here, we see the multiplication combined assignment operator again; so it is going to say, "Take num and multiply it by 20."1211

So, num is going to be equal to 20.1215

Here we divide it by 4; num is equal to 5; and then, mod...you end up with num=1.1217

Now, we are going to talk about string operators: there are two string operators.1230

Well, it's one string operator known as the concatenation operator, and then it has a combined assignment version, just like the arithmetic operators.1236

The concatenation operator is simply a period, and it is an operator that is used to combine, or concatenate, two strings together.1245

It is a binary operator; so here we have the binary operator, the period.1256

And it has two operands: on one side is hello--the string literal hello; on the other side is world.1260

And this operator says to take these two values, these strings and concatenate them together, and the result is the string concatenated.1268

So, the message (at the end after this concatenation operation occurs) becomes the literal "hello world" where they are all combined into one string.1277

You can use this with both literals and variables.1292

For example, we could create a variable called day and set it equal to Monday, and then we could create a variable that's going to hold a message.1295

We started off with a string literal, "Today is", and that is one operand of the concatenation operator.1303

The other operand we can set to the variable day, and what that is going to do is concatenate these two strings together.1311

It is going to take "Today is" and combine it with the string that day represents, which is Monday,1318

and output that as one string, and set it equal to message.1324

So, at the end of this, message is going to be equal to "Today is Monday."1327

Now, just like the other operators, the arithmetic operators, there is a combined assignment version for this one, too.1333

That is this here--the .= ; and basically, it is just like how the arithmetic ones work in that, if we have begin.=end,1339

that is saying, "Take the variable being, concatenate it with end (the operand on this side of the operator), and then assign that back into begin."1349

Let's take a look at a page demonstrating this.1364

This demonstrates the two examples that we saw: we have a string hello, and what we have done is saved it in message.1370

And then, we have taken the variable message, which is equal to the string hello, and we have concatenated it with the string literal world.1380

So, at the end of this operation, message is equal to the string literal hello world.1388

We could also do the same operation using a combined assignment operator, where if we set message equal to hello,1394

as we did up here, instead of having to write a full expression like this, we can just do .=world.1400

What that is going to do is just concatenate the world string to the string that message already is, which is hello.1406

And so, again, at the end of this, message is going to be equal to hello world.1412

Now, one thing I want to talk about to finish up the lecture is a topic known as precedence and associativity.1421

Basically, all operators have assigned to them a precedence and an associativity.1428

What those are for is when you have multiple operations occurring in the same statement.1433

For example, this statement right here has two operations: it has a multiplication operation, and it has an addition operation.1438

When PHP sees that, it has to have rules as to which operation it proceeds with first.1449

It could go left to right and say, "OK, I'm going to do 5 times 6 first; the value of that is 30, plus 2, and the value is equal to 32."1454

It could go the other way and say, "6+2 is 8; 8x5 is 40."1463

Well, the rules in PHP are that multiplication has a higher precedence than addition.1469

So, when those two operators occur in the same statement, the one with the higher precedence, multiplication, gets completed first.1475

It is sort of the same thing as if we had written this in parentheses here.1482

The value of this expression is actually 32, and not 40.1485

And actually, if you go...I have a link on the homework challenge to the PHP website and the PHP manual;1492

there is an operator precedence page, and what it does is: it lists all of the different operators, and most of these we haven't seen yet,1499

which you can just ignore for now.1505

And it ranks them in decreasing precedence; for example, as you can see, multiplication and division are at a higher level than addition and subtraction.1507

So, as we can see in the last example, multiplication happened first.1521

Now, what happens then, and the way this chart works, is that everything on the same line has the same precedence.1527

So, what if you have two operations that are occurring in the same statement where both operators have the same precedence?1533

Well, then you defer to what is known as associativity.1541

What that does is determines, "If you have operators in the same precedence, do they get evaluated left to right or right to left?"1546

So, go back and look at the slide here: the multiplication sign and the modulus operator both have the same precedence.1554

In order to decide which one gets operated on first, PHP defers to the rules of associativity.1566

The associativity for these operators is left, which means they get operated from left to right fashion.1572

So, what is going to happen is: 3 times 3 is first going to get multiplied, and then it's going to be mod-ed with 2,1578

as opposed to the other way around, 3 mod 2 times 3.1586

The result of this is actually 1 and not 3, because 3 times 3 is 9, and if you divide it by 2, the remainder is equal to 1,1594

as opposed to 2 goes into 3, remainder of 1, 1 times 3 is 3.1604

This demonstrates left associativity.1609

Now, as we saw in a previous slide, the assignment operator is right associative; so when we have a=b=5, this statement contains two assignment operations.1614

It has two assignment operators; they have equal precedence; so how does PHP decide which operation gets performed first?1630

Well, the associativity rules say the assignment operator is right associative, so the right one is going to get performed first,1639

and then move to the left, so then the left one will be operated.1646

As we saw before, b=5 gets operated first; that assignment operation occurs,1651

and we know that the value of an assignment operation is the value that was assigned.1655

This one occurs first, and then that value was assigned into a; so the assignment operator is an example of a right associative operator.1661

Now, one other thing that you can do, which you may remember back in math days, is force precedence using parentheses.1672

So, as up here, we had seen that, since multiplication has a higher precedence, it gets evaluated first, so we have 5x6 is 30,1680

and we add 2 to get 32; if we wanted to do reverse and add 6+2 first, and then multiply times 5,1688

we can force the precedence by adding the parentheses.1695

What that is going to do is tell PHP, "let me do what is in parentheses first: 6+2 is yielding 8; then multiply that times 5," so that is how we get 40 here.1698

The same way for this other operation: because, in this case, the multiplication and modulus operator have the same precedence,1707

associativity determines which operation occurs first, and associativity for these operators is left, so multiplication would go first.1716

By adding the parentheses here, we are saying we want to do this operation first; so 3 mod 2 has a remainder of 1, so it's equal to 1.1725

1 times 3 equals 3; so now, the value of the equation changes.1733

I have some examples of that, as well.1740

Basically, this is just a review of what we talked about; and we talked about precedence, saying that operations with higher precedence execute first.1748

In this case, which is a little bit different and shows the importance of precedence (because normally we read left to right,1758

and we would think this operation occurred first), in this example, multiplication has a higher precedence, so 2x3 is executed first,1766

which is equal to 6+5, is equal to 11; not 21, which would be the case if we added 5 and 2 and got 7 and multiplied it times 3.1773

Now, this example down here demonstrates associativity; again, the modulus operator and the multiplication operator1782

both have the same precedence, so the rules of associativity apply,1788

which say the leftmost operation gets performed first, and then it proceeds to the right.1792

In this case, 11 mod 3...3 goes into 11 three times with a remainder of 2, so we get 2 times 2; this results in num being equal to 4.1797

That is not 5, as if we had done this instead--if we had done the multiplication operation first.1809

For example, if these were right associative operators, we would have 3 times 2 is 6; 11 mod 6 is equal to 5.1817

And so, you just want to note that these have left associativity;1827

and as we talked about before, some operators, like the assignment operator, have right associativity.1830

And again, you can use parentheses to force precedence; so these examples that were shown above are now shown with parentheses,1836

to have them operated a different way: in this case, we can get the value to equal 21 by forcing the addition operation,1843

the lower-precedence operation, to happen first.1849

And then here, whereas the multiplication has the same precedence as the modulus operation,1852

but those operators are both left associative, we can force the right operation to occur first with parentheses.1859

For today's homework challenge, what I would like you to do is to go out and take a look at this link.1870

It's on the PHP manual, and what it does is shows you all of the different operators that are available in PHP.1874

And you can, like I said before, ignore the ones that we haven't covered yet, but you can see where all the different operators rank,1882

because when we eventually start writing arithmetic functions, and doing calculations, it is going to be important to know how they work.1889

And so, here, you can just see how the precedence works, and you can see that multiplication and division and modulus are higher up1897

in precedence than the plus or the minus or, in fact, the concatenation operator, as well.1904

That ends today's lesson; thank you for watching Educator.com--I look forward to seeing you next time.1911