0:08

So an important part of any programming language is how it handles expressions.

Those are things that are kind of on the right hand side of an assignment,

statement or all over the place, right?

You have pluses and minuses.

It works very much the same as all other languages, using +,

-, /, *.

Actually, this whole convention this thing came from Fortran.

That's how old that is.

That's from 1955.

That's 1955 when they decided multiplication was an * and

probably even before that.

And we still use it to this day.

So then when every time

you type in * when you want to do multiplication think that's from 1955.

And so the expressions are on the simplest expressions sort of here,

we have on the right hand side.

The interesting thing about PHP is it has something that's actually rather rare.

Well, JavaScript is kind of another language that

has very aggressive type conversion.

And that it doesn't want to blow up, it wants to convert it.

And so if it looks at this string 15 + 27,

you're like what is going on here.

Now one of two things could happen.

It could treat that as a string and

end up with 1527 and concatenate those two things together.

That's not what it does.

It actually converts this to an integer.

And then adds them together and gives us 42.

And so there's a lot of implicit type conversion.

This + is a numeric, and so

it's going to try to convert its operands to be numbers,

and so that's how we end up with 42.

So the values can be strings, numbers, we can use function calls.

And there's this order of evaluation that is the same for all programming languages.

And they can also produce operations.

Some operators of note, if you're coming from Python,

some of these have Python equivalents, some of them don't.

We'll talk about some of these,

there is a increment operator that just says add one to this variable.

Subtract one to the variable.

Concatenation that’s quite special.

Equality and not equal these are both common across all C languages.

This also came from C.

Identity this is equal in value with type conversion.

2:35

is not triple equal 0 because these are different types.

False and 0 are different types.

Here it's going to convert that to a 0, then that's going to become true.

Here it's not going to convert that to 0.

The ternary operator here, we'll talk about that.

This is also kind of a classic 1972 C idea and the side effect.

And then there's other things that come from these that also come

from C that are very bit oriented operators.

And that's the zeroes and the ones.

And they're ANDing and ORing shifting these things around.

These usually have no use unless you're doing something like a compression, or

encryption, or something like that.

So if you see these you're not going to tend to use these operators.

Okay, so let's look at them, so increment and decrement.

This plus, plus is a side effect, so if we have $x and it has a 12 in it.

So we come in here.

And again like any right-hand side, it's evaluating this right-hand side.

So it has to first resolve this x so it has to go retrieve the x.

So it pulls the x to the 12 out.

And makes it part of this expression.

But then, as a side effect, and the ++ is after the x, it turns this to 13.

Now the 12 still exists, so it's 15 + 12 and so we end up with 27.

But x has become 13 in this next statement because as

a side effect of reading and adding one, that's what we got.

You can also say, ++$x, and the only thing that does,

is it adds the 1 before it copies the value into the expression.

So in that case this would be 28, x would still be 13.

Now, most civilized people tend not to use that unless there'll

be places I'll tell you to use it and in general, I tend not to use it at all.

And so we tend to do things like add one to it, explicitly.

So we say x = 12, so we have this x.

There's 12 in here, we copy the 12 into this 15 + x so y is 27.

And then in this next statement we then add 1 to x.

And so we add a statement here, but

we're making it a little clearer as to what's going on.

Some people like to show off and

write as dense a code as possible, I tend to avoid that.

The period character like I said, is a string operator.

So it's not just the fact that it can concatenate strings,

it will also convert its operands to strings.

Now it does not add a space, it's just a pure concatenation.

So if I want Hello space World then I have to add that Hello.

And so I can echo, Hello World with no new line.

But then I concatenate that variable with concatenated with a new line, so

I get my new line here with Hello World.

So one of the things about PHP is the operators have attitude in that

they are expecting and or converting based on the types of strings are.

Dot is the string operator, plus is a mathematical operator.

So that actually is in some ways a little more predictable than some languages.

Like JavaScript has this kind of stuff, but you can't always predict

what it's doing, because a operators are more object oriented.

5:52

The ternary operator comes from the C programming language, and

it comes from a time when we wanted to make things very succinct.

And so the ternary operator is basically an if, then, else in a single line.

So we have an assignment statement here and there are three parts.

It's called ternary because there's three parts.

The first part is a question that evaluates to true or false.

The second part is the result of the expression.

because this is kind of an expression,

you could put parentheses around it right there.

This is the value of the expression if this is true and

this is the value of the expression if it's false.

So the way I think about this is I think about this as

you've got these two values kind of in flight.

That are going to go into this variable $msg.

And then we pick the one, so in this case $www > 100 so it's a true.

So we pick the one that's true and then that one goes in, okay?

So they kind of both end up in flight basically.

It's like this is an assignment statement and

one of these two things is going to get in.

And it turns out this is the one that gets in.

Okay, and so that's why Large comes out.

6:59

And so we can do things like take the divide $www by 2,

take the remainder and ask if it's 0.

That would meant that it's either an even number or an odd number.

In this case, this turns out to be false because it's 123.

And so Odd gets put over here, and so away we go.

This is sort of an example of type conversion.

We take $www, get the remainder after dividing by 2.

And in this case, that's going to be 1, because it's 123.

And that's going to turn into true.

So 1 becomes true, and so Odd gets copied into message.

I flipped the order of the Even and the Odd because if the result is a 1,

that's true which is odd.

And if the result is a 0 then that's false, and then you get even.

So that's kind of a ternary operator.

7:55

And so there's a bunch of syntax that we do, and so we use it over and over and

over again, so I have to teach it to you.

I'd rather not teach it to you, but we just kind of, if we didn't do that,

we'd have way too many if, then, else’s in certain very specific situations.

These are idiomatic situations, where you look at it, go, that's what he's doing.

I understand what that is.

It's really an idiom, and he used the ternary operator.

So I'll forgive him.

But you don't want to overuse any of these fancy operators.

Side-effect operators you can use these things, so

$count += 1 is the same as saying $count = $count + 1.

This is just a contraction of this expression.

And I would never use this.

I would not use that code right there.

But I do use it here in constructing strings, because there's code places where

you start a string add something to it, add something to it, add something to it.

So one way to do it here is you start a string and then you want to add

a blank to it so you say $out = $out. , that's adding a space to the end.

You're going to do this over and over, you're going to say that over and over so

we can track this to be $out .= World.

That concatenates World to the end of it and that concatenates a new line.

So that prints out this, including a new line to go down there.

So this is a way, usually, this is a silly example, but we use this to grow strings.

Start a string, add a little bit more to it, add a little bit more to it,

add a little bit more to it, add a little bit.

And we use .= to kind of add to the end of the string.

And, that, I tend to use.

I tend to like that, I tend to use that.

Conversion and casting, there's a lot of aggressive type conversion that happens,

as I mentioned.

You can also explicitly force it when you want to.

It's actually something used more rarely in PHP than other programming languages.

So let's take a look at some of the crazy things that are going to happen here.

So we have some numbers, division, this division, even those these two things

are integers, the division produces a floating point number.

Which is actually a sane idea.

10:11

Divisions that of integers that produce non-floating point numbers,

like Python 2 for example, are not so logical.

This one is kind of weird, this a plus operator is a numeric operator,

36.25 is a floating point number.

TRUE is True, but then it gets converted to one to be numeric, and

100 is a 100 string and that becomes a number 100.

So this is going to be 137.25 and indeed it is, and that is not a syntax error.

You're going to look at PHP and be like that's bad!

But you get used to it and you say, mm, I guess I don't mind that.

That's a powerful capability as long as you use it wisely.

This of course is not wise.

10:51

I'm just showing you what's possible, right, so that's a bad line of code.

But there you go.

So here we're going to do concatenation with D.

That's a string operation so

this is a number, but it forces it to string automatically.

You could put parentheses string.

That's a type.

It says convert that non-string variable into a string.

Away you go.

That's not too useful because the dots would have converted it to

a string anyways.

But you can take something like 9.9 and convert it to an integer.

So that turns this to 9- 1 and gives us 8.

So that's really a truncating conversion to integer.

11:29

And again, here we have a string added to a number.

But remember that this is a numeric operator which means that it's going to

force its operands to become numeric.

So it turns out that what it does, is it doesn't blow up.

Again, you might like it.

You might not like it.

But it turns out to 0.

So “sam” plus 25 is the number 25.

“sam” concatenated with 25, that's a string operator.

So that says, I can convert this 25 to a string, and so you end up with sam25.

It works out.

Plus is a number.

Dot are strings.

And so that part is pretty consistent.

It's like if there's pluses, we're going to force them to numbers one way or

the other, including craziness like the string “sam” turning into a 0.

And so but the dot is for strings.

And so I actually wish more programming languages thought that way.

12:42

So in Python we have to be explicit about this.

We have to say int 100 and then this converts it and

then adds it and we get our 125.

Or if want to concatenate,

so we're using the same operator in Python to do the concatenation.

But we do have to convert the 25 to a string so we can concatenate that.

And if we foolishly try to convert “sam” into an integer, we get a Traceback.

And so this kind of tells you the philosophy of PHP

in that it lets you do things in PHP that in Python are going to blow up.

And in a way, this is one of the reasons I like Python.

As a beginning language, because it sort of keeps you on a very tight leash.

You have to be very precise in what you're trying to do.

If you try to do something silly, it's going to stop you.

So you can say, wait a sec?

What did I do?

Why did I do that?

Maybe this is silly, but what if that was a variable and you're like,

that was a string.

I forgot.

I forgot that I put “sam” in there sometimes.

And so it's helpful because this, while it can be frustrating to get a trace back,

this can also inform you on what you've done wrong.

Whereas, PHP, [SOUND] right?

Again, responsible.

13:57

Casting, so TRUE becomes 1, I mentioned that.

One of the most frustrating things that happens to me as a programmer is FALSE

becomes nothing.

So if I take this dot, again as a string operation.

So it's converting, forcing false to become a string.

When it forces TRUE to become a string TRUE becomes 1, so X1Y, FALSE is nothing.

So if you try to print FALSE out with an echo statement it's going to not print

anything.

And that has driven me crazy on more than one occasion.

Because I'm like, echo this variable, x, and there's nothing.

I'm like it didn't get to that line.

Wait a sec, after 20 minutes.

That was a FALSE, and that's why I'm not seeing anything.

So you gotta, FALSE doesn't print out well.

14:43

So the equality operator, as I mentioned earlier, is

aggressive type conversion operator where it tries to match the type conversions.

So here are some silly things that in many languages would blow up,

but in PHP are totally cool.

So 123 is equal to the string "123".

Well, that's because it's trying to convert them.

And so it does, and it finds that they're equal.

Here, we've seen this.

That just terts into 123, so that's going to work.

Here, this little trick here is like, false can become an integer 0 and

this becomes an integer 0, so yay, they're completely cool.

And this one here is really crazy.

5 < 6 is true, which becomes a 1.

And then the string 2 minus the string 1.

That turns into an integer 2 because minus is an arithmetic operator.

So it's 2 - 1 which becomes 1.

And look at the, 1 and 1 are equal.

So that is like little, the craziest line of code, not the craziest line of code,

but it's a very crazy line of code to not make a syntax error.

I'm not saying you should do that, I'm just saying

that's the aggression that all these operators in PHP tend to have.

Sometimes this equal sign aggression we want to stop it, and so

we just say triple equals.

And so this basically becomes true with no conversion.

And it's the same as true.

And so they're still equal.

But if this was like a 1,

that would be false because triple equals suppresses type conversion.

Triple equals compared to Python is like, is and is not triple equals.

Pretty much identical concepts.

16:36

So anything in numeric, false becomes 0 and there's also null becomes 0, okay?

Automatically if it's in a numeric calculation.

And so there are times where a function in particular the string position.

So the way string position works is it's looking for one string.

So, if I'm looking for A B C, and then I have this needle and

it's a quote A, it returns the position, which is position 0.

If it's a B, it would return 1.

The problem is that if it returns 0, that means that it found it and

when it returns, is returns FALSE if it didn't find it.

So you have to be careful to know whether you found it at the beginning or

you didn't find it all.

And so you read documentation and it's warning you to use even in

the documentation it's warning you to use the triple equals operator.

So that's how important the triple equals operator is in PHP.

17:27

And here's some simple examples of this strpos function.

So, here's this Hello World.

We're looking for the position of Wo, which is 0, 1, 2, 3, 4, 5, 6.

So that'll give us the 6.

We're looking for the string He.

And that will be right there in position 0.

And then we ask where the position ZZ is.

18:02

And this is the mistake that you can make.

You can say if the position of the He is false, well this is going to find it.

You think you're asking whether you found it or not, and this is going to be

converted to 0 because the aggressive conversion of the equals sign.

And so this is going to print out incorrectly.

Now if we're looking for ZZ, this one's going to work okay because this is going

to give us a false and a false and there's,

when you do type conversion they still match which is what you'd expect.

But this is where you, on strpos you have to use triple equals because if this

returns to zero and it is not equal to false.

Zero is not equal to false because triple equals suppresses type conversion, right.

And the same is true here, so all that says is read the documentation.

Print doesn't show the FALSE print R which allows you to print even more detail.

They never show up.

They don't show up at all.

So there's lots of ways to try to print FALSE, and

there's only one way that you can actually show them.

And that's the var dump.

So next we're going to talk a little bit about control structures, if then else and

things like that.