WEBVTT

00:00.170 --> 00:06.410
So let's get started by creating a first PHP file inside which we're going to learn about the expressions

00:06.410 --> 00:08.660
and the basics of variables.

00:08.660 --> 00:10.490
You can just call it expressions.

00:10.820 --> 00:19.010
PHP and then as said previously, every PHP file it needs to have this opening PHP tag.

00:19.040 --> 00:25.550
This is a requirement for this file to be actually considered as PHP code.

00:26.690 --> 00:31.520
So the first thing you should learn is how do you output something on the screen.

00:31.520 --> 00:38.300
This is done by using the echo statement, which is followed by a string that should go on the screen.

00:38.300 --> 00:47.570
So I can say welcome to PHP and then make sure that I add a new line using the special character slash

00:47.570 --> 00:48.260
n.

00:48.470 --> 00:50.900
And remember about the semicolon.

00:50.900 --> 00:53.630
Now how do you run this PHP script?

00:53.660 --> 00:58.850
Well, we can just run it in the command line using PHP expression.

00:59.120 --> 00:59.870
PHP.

00:59.870 --> 01:01.940
And here is our output.

01:03.790 --> 01:11.650
So, as we already know in PHP, anything that will return a value is an expression, and in expression

01:11.650 --> 01:17.770
you can just mix raw values, variables, function calls and operators.

01:18.370 --> 01:20.770
So here you've got different examples.

01:20.770 --> 01:29.680
And this page is complex on purpose as that's actually a resource you can download under this video.

01:29.710 --> 01:33.790
Maybe you'd like to print it or just keep it as a reference.

01:33.790 --> 01:43.720
And then with expressions we will also use operators like arithmetic logical comparison and the string

01:43.720 --> 01:45.100
concatenation.

01:45.670 --> 01:52.180
So let's jump back to the code editor and see more examples of expressions in practice.

01:53.860 --> 01:58.990
So in this example here we've directly have put something on the screen.

01:58.990 --> 02:08.730
But sometimes you either have more complex expressions or you just need to do a little more work before

02:08.730 --> 02:11.220
you are ready to output the results.

02:11.220 --> 02:15.510
And in such cases, variables are very helpful.

02:15.540 --> 02:23.100
Let me begin by defining a variable that will be called name and it will contain a string.

02:23.130 --> 02:23.970
Alice.

02:24.000 --> 02:28.440
Now let's take a closer look at variables.

02:30.120 --> 02:38.100
So variables among other things, which we're going to talk about later, are used to store the results

02:38.100 --> 02:38.910
of running.

02:38.910 --> 02:40.320
Expressions.

02:40.830 --> 02:48.180
And variables are defined by using the dollar character followed by the variable name.

02:48.180 --> 02:52.920
Now there are rules for what's the valid variable name.

02:52.920 --> 02:56.160
So as I said it needs to start with a dollar.

02:56.190 --> 03:01.020
Then the second character needs to be a letter or underscore.

03:01.020 --> 03:08.250
And only the third and subsequent characters can be letters, numbers, or an underscore.

03:08.280 --> 03:16.310
You need to keep in mind that variable names are case sensitive, so lowercase variable is not the same

03:16.310 --> 03:19.370
as capitalized variable.

03:19.400 --> 03:23.840
Then there are some conventions on variable naming.

03:23.840 --> 03:28.640
You should either use camel case or snake case.

03:29.420 --> 03:35.120
So here are some examples of valid and invalid variable names.

03:35.120 --> 03:41.750
And also keep in mind not to use PHP reserved words for the variable names.

03:43.670 --> 03:50.840
So here we have assigned the value of Alice string to a name variable.

03:50.840 --> 03:54.290
So this is an assignment expression.

03:54.290 --> 04:00.710
Yes I said expression, which means that this together returns a value.

04:00.710 --> 04:05.240
And we can check that by running the echo statement.

04:05.450 --> 04:12.790
If I rerun this script now we see both welcome to PHP and the text Alice.

04:12.790 --> 04:20.800
So assignment is also an expression and it will return whatever was assigned to a variable.

04:23.110 --> 04:23.380
Okay.

04:23.380 --> 04:26.320
So let's create a more complex expression.

04:26.320 --> 04:34.270
So as we see on this board we have simple expressions like numbers just variables or arithmetic operations.

04:34.270 --> 04:40.480
But also we can create complex expressions mixing all of those together.

04:40.570 --> 04:42.760
So take a look at the operators.

04:42.760 --> 04:47.860
The one that's interesting for us in this case is the concatenation operator.

04:47.860 --> 04:49.330
That's just a dot.

04:49.330 --> 04:54.310
So it would let us put a couple of strings together.

04:54.940 --> 04:56.800
Let's see what can we do with this.

04:56.800 --> 05:01.510
So I can output the text saying hello.

05:01.780 --> 05:07.630
And then I use dot and I put the value of the name variable.

05:07.630 --> 05:13.120
And then also I add an exclamation mark and new line character.

05:13.840 --> 05:16.060
So let's rerun this script.

05:16.060 --> 05:17.050
And there we have it.

05:17.080 --> 05:18.580
Hello, Alice.

05:20.770 --> 05:28.810
So as you can see in this example, I can say that that's the first time when we can see that we are

05:28.810 --> 05:31.210
already doing something useful.

05:31.240 --> 05:39.790
So you can easily imagine that if we have assigned a value to this variable called name, we can also

05:39.790 --> 05:44.530
get an input from the user and then set the value of that variable.

05:44.530 --> 05:53.140
And then once someone enters his or hers name, we can welcome him with this let's say personalized

05:53.140 --> 05:54.250
message.

05:54.370 --> 05:54.820
Okay.

05:54.850 --> 05:57.220
So let's continue with expressions.

05:57.250 --> 06:01.420
This time let's do an arithmetic operation.

06:01.420 --> 06:04.060
So let's calculate something.

06:04.060 --> 06:07.420
First let me create a new variable called pizzas.

06:07.420 --> 06:09.280
And we will have three.

06:09.370 --> 06:13.720
Then let's create another one slices per pizza.

06:13.840 --> 06:15.850
That's typically eight.

06:15.880 --> 06:23.820
Now let's do an arithmetic operation so we can multiply, divide, add or subtract or calculate the

06:23.820 --> 06:24.810
modulo.

06:24.810 --> 06:30.000
So let's calculate the total amount of slices.

06:30.210 --> 06:39.150
This would be multiplying pizzas by the slices per pizza.

06:39.780 --> 06:41.940
And let's echo that.

06:42.720 --> 06:45.930
We can say total pizza slices.

06:45.930 --> 06:48.690
And now we concatenate.

06:49.800 --> 06:54.690
So we use the dot operator and we output the total slices.

06:54.690 --> 06:59.010
And again we concatenate that with the new line character.

07:00.510 --> 07:02.730
And now let me run this.

07:02.760 --> 07:03.780
There we have it.

07:03.810 --> 07:05.820
Total pizza slices 24.

07:05.850 --> 07:07.440
That was expected.

07:08.190 --> 07:11.340
Now at this point let me quickly mention the data types.

07:11.340 --> 07:16.110
So I'm going to go on data types in detail a little bit later.

07:16.110 --> 07:21.230
For now let me just explain This line right here.

07:21.230 --> 07:29.990
So we can see that we have total slices variable which is a result of multiplying three times eight.

07:30.020 --> 07:38.090
Yet we concatenate the string and a number and we get the proper output which is a string.

07:38.090 --> 07:47.450
So what happened here is called the type coercion, which is implicitly casting one of the data type

07:47.450 --> 07:48.320
to the other.

07:48.320 --> 07:50.630
So everything makes sense.

07:50.630 --> 07:59.060
So in case of a string concatenation the sensible output is to produce a string.

07:59.060 --> 08:05.240
That's why this number was implicitly casted to a string in PHP.

08:05.240 --> 08:13.370
But as I said we're gonna talk about data types and typecasting later on okay.

08:13.370 --> 08:14.840
So let's see another example.

08:14.840 --> 08:21.950
Let's define a variable and call that is hungry and initialize it with true.

08:21.950 --> 08:29.830
So this would be of data type boolean which basically can hold one of two values either true or false.

08:29.830 --> 08:33.850
So Boolean values are very commonly used.

08:34.210 --> 08:39.820
They are always the result of for example the comparison expressions.

08:40.870 --> 08:44.260
And in this case we will just set it to true.

08:44.260 --> 08:52.420
And what I would like to do is to output different text depending on whether someone is hungry or not.

08:53.110 --> 08:58.780
So let's always echo hungry with a question mark.

08:58.780 --> 09:03.340
And then we're going to use something called ternary operator.

09:03.340 --> 09:05.830
So it's super simple thing.

09:05.860 --> 09:13.150
First you add an expression on the left side, then a question mark and a colon.

09:13.900 --> 09:18.790
And this would return either something on the left side of the colon.

09:18.790 --> 09:26.520
If this expression is true, or the right side after the colon if this expression is false.

09:26.550 --> 09:37.230
Ternary operator is something you will commonly use, so I'd like to return the text yes, or alternatively

09:37.230 --> 09:38.130
the text.

09:38.160 --> 09:38.850
No.

09:38.850 --> 09:40.560
Let's run this.

09:40.590 --> 09:46.620
Or maybe let's also follow up with a new line character.

09:47.640 --> 09:51.180
Now let's run it and we have it.

09:51.210 --> 09:51.750
Hungary.

09:51.780 --> 09:52.740
Yes.

09:53.040 --> 09:57.450
Now let's change this value to false and rerun the script.

09:57.450 --> 10:00.660
And now the Hungary displays the text.

10:00.690 --> 10:01.560
No.

10:02.400 --> 10:03.660
Let's summarize.

10:03.660 --> 10:04.740
So you've learned.

10:04.740 --> 10:08.760
How can you output something on the screen, how to define a variable.

10:08.760 --> 10:17.340
And then couple of operators like concatenation arithmetic operators or an often used ternary operator

10:17.910 --> 10:18.960
Ethz.

10:19.170 --> 10:26.910
Then we've also heard a little bit about different data types like Boolean numbers strings, and the

10:26.910 --> 10:28.440
type coercion.
