Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
In this video, we talk about how PHP needs to be placed inside of a PHP block when coding.
-
0:00
In order to write PHP inside of a PHP file,
-
0:04
you have to do one more step then just saving the file with the extension .PHP.
-
0:08
You must also open and close what's called a PHP block.
-
0:14
You open and close a PHP block like this.
-
0:18
If you try to write PHP outside of a PHP block, it will display as plain text.
-
0:24
Likewise, if you try to write plain HTML inside of a PHP block,
-
0:29
it will cause an error.
-
0:31
In some files, like most core WordPress files,
-
0:34
you open the PHP block at the top of the file and close it at the end.
-
0:39
This means that you are writing exclusively PHP.
-
0:42
In other files like template files, you open and close
-
0:47
PHP blocks multiple times throughout the file whenever you want to write some PHP.
-
0:53
This means that you will likely have a combination of HTML and
-
0:57
PHP in the same file.
-
1:00
Both situations are perfectly acceptable in WordPress, and what you need to do in
-
1:04
any specific PHP file will determine what approach you take.
-
1:09
Let's take a look at a typical PHP only file.
-
1:14
We're going to go ahead and open the WP config file in our text editor.
-
1:19
We'll see that it opens with a PHP block, has a whole bunch of PHP.
-
1:23
But then, when we come down to the bottom, it does not close the PHP block.
-
1:30
This is an interesting thing in WordPress.
-
1:33
Technically, we could, and some say should, close this PHP block.
-
1:39
However, it will not break this to not close it.
-
1:42
And WordPress or the server running it will automatically close the block for us.
-
1:48
There are a couple of reasons that the WordPress developers do this.
-
1:52
One was they were concerned that if they didn't close the PHP block, and
-
1:56
someone tried to start coding PHP after this, that it could cause an error.
-
2:02
The other is that sometimes even just having extra white spaces
-
2:05
at the end of a PHP file outside of the closing block can cause problems.
-
2:11
For this reason, a lot of PHP
-
2:14
only files in WordPress do not have the closing end of the block added.
-
2:20
However, when you're coding your own files
-
2:22
generally you'll want to close the PHP block.
-
2:27
Let's take a look now at a file that contains quite a bit of HTML with only
-
2:31
a little bit of PHP inside of it.
-
2:35
We'll go ahead open up the 2015 default theme header file.
-
2:41
We could see that at the top of this header.php,
-
2:43
we have a PHP code block that opens here and ends here, with a comment inside,
-
2:49
a whole lot of HTML, and then little pieces of PHP that
-
2:55
are open and closed and only output a small amount of PHP.
-
3:01
This is fairly common.
-
3:03
We also see another type of PHP block, that we'll look at more later,
-
3:09
that's a conditional one that has some PHP, HTML inside of it,
-
3:15
some more PHP, more HTML, and back and forth like this.
-
3:22
We'll look at this shorthand of writing PHP a little bit later when we get to
-
3:25
the conditionals.
-
3:27
However, this template serves as a good example of a primarily
-
3:31
HTML file that simply has some PHP code blocks inside of it.
-
3:37
We'll look at one last file here, which is the functions.php.
-
3:40
This is one, again, that is primarily PHP, but
-
3:45
we may end up finding some HTML coded inside of it.
-
3:49
It depends on what that theme needs, and whether or
-
3:52
not they'll have HTML coded within it.
-
3:56
However, in this one, when we see our HTML coded, it's all appearing inside
-
4:01
of PHP itself, and they don't close and open the block to write it out.
-
4:07
Now that we've practiced opening and
-
4:09
closing some PHP files, let's take a look at what happens when we have an error,
-
4:14
specifically, one that involves the ever important semicolon.
You need to sign up for Treehouse in order to download course files.
Sign up