WEBVTT

00:00.530 --> 00:02.090
Let's go back to strings.

00:02.090 --> 00:09.410
So we've used the strings throughout the course so far, but we haven't really went deep on them.

00:09.410 --> 00:14.450
So I think it's time that we really dive deeper into strings.

00:14.450 --> 00:20.630
So let's begin with the simplest thing, which is the single and double quote strings.

00:20.750 --> 00:25.910
So there are two types of strings in PHP the single and double quotes.

00:25.910 --> 00:27.740
Here you see the double quote strings.

00:27.740 --> 00:35.000
And what characterizes the double quote strings is that they can interpolate data.

00:35.000 --> 00:40.730
So interpolating essentially means that you can use a variable like name or any expression.

00:40.730 --> 00:44.420
And this would be outputted on the screen like in this example.

00:44.690 --> 00:51.200
So you can also add and use special characters like new line, tab etc..

00:51.260 --> 00:56.870
Now with single quote strings everything inside is treated literally.

00:56.870 --> 01:08.510
So there are no special characters except maybe an escape character and except a single quote ending

01:08.510 --> 01:09.260
character.

01:09.290 --> 01:15.050
Everything else is treated as you would mean a literal string, as in this example.

01:15.080 --> 01:17.210
That would be the output.

01:18.710 --> 01:21.470
So I've already created the strings php file.

01:21.470 --> 01:23.210
You can just grab it from GitHub.

01:23.210 --> 01:26.630
Let's run it so I can do strings.

01:26.630 --> 01:29.690
And we see exactly what I've described.

01:29.690 --> 01:35.450
So with the first example single quotes nothing is actually interpolated.

01:35.450 --> 01:39.860
Everything is treated literally with the second double quote example.

01:39.860 --> 01:46.520
We can clearly see that in place of the name variable here John is outputted as that's the value of

01:46.520 --> 01:47.390
the variable.

01:47.420 --> 01:51.320
Now let's see the different kinds of strings.

01:51.320 --> 01:55.700
So even if you have used PHP before you might not have heard about them.

01:55.700 --> 01:58.190
First is a heredoc syntax.

01:58.280 --> 02:00.020
This is pretty strange.

02:00.110 --> 02:02.180
So we've added those symbols.

02:02.180 --> 02:06.080
You write your code, then end and semicolon.

02:06.080 --> 02:08.690
Now everything between those characters.

02:08.690 --> 02:16.490
The starting and ending lines is actually a multi-line string, so you are free to write multi-line

02:16.490 --> 02:19.280
strings, which wouldn't be really easy.

02:19.280 --> 02:26.240
With single or double quotes, you would have to use a concatenation character, and it would really

02:26.240 --> 02:33.620
be odd to put, uh, those kind of strings into multi-line strings with this heredoc syntax.

02:33.620 --> 02:34.670
This is easy.

02:34.700 --> 02:37.340
Now there are two variants of this.

02:37.340 --> 02:43.760
There is Heredoc and there is another one called now doc.

02:43.760 --> 02:51.680
Now the difference between them is, uh, this actually can have single quotes here.

02:51.860 --> 02:54.860
The difference is pretty simple.

02:54.860 --> 03:02.450
With the here doc you've got interpolation and with now doc you don't.

03:02.480 --> 03:04.400
So those are like single quotes.

03:04.400 --> 03:07.430
And this syntax is like double quotes.

03:07.430 --> 03:08.810
So let's run it.

03:08.810 --> 03:13.370
This is string speech P.

03:13.850 --> 03:17.270
Um yeah I obviously forgot to echo those.

03:17.270 --> 03:24.780
So let me echo here dog and the now dog and let's see this.

03:24.780 --> 03:28.200
So first we've got here dog with interpolation.

03:28.200 --> 03:32.520
Next up we've got now dog without any interpolation.

03:32.520 --> 03:34.950
That's why we see dollar name in here.

03:35.070 --> 03:35.970
So what.

03:36.000 --> 03:37.110
What's the difference.

03:37.110 --> 03:40.350
Why would you have two types of strings.

03:40.350 --> 03:47.130
Well theoretically this single quote string is faster as it has to do less.

03:47.130 --> 03:53.520
It doesn't have to look for variables, for expressions, and for special character combinations like

03:53.520 --> 03:54.990
new lines and tabs.

03:54.990 --> 04:05.910
Just be wary with trying to optimize or do micro-optimizations by always using single quotes first.

04:05.910 --> 04:10.950
Before you do any optimization, just make sure that you profile your code first.

04:10.950 --> 04:19.770
And I think that actually it's rather not possible that the type of strings you use would significantly

04:19.770 --> 04:23.820
improve performance or slow down your application.
