1. 5
    Closing tags, semicolons & line breaks in PHP
    2m 58s

Closing tags, semicolons & line breaks in PHP

Mark Shust
InstructorMark Shust
Share this video with your friends

Social Share Links

Send Tweet
Published 2 years ago
Updated 2 years ago

Closing tags and semicolons play an important part in the execution of PHP code at the time of execution. We'll learn all about them as well as line breaks in this lesson.

Instructor: [0:00] Note that if we look in the corner of this PhpStorm, we'll see there is a weak warning. If we auth select this closing tag, we'll see there is a green squiggly that says there is a redundant closing tag. This is because our PHP script only contains PHP code.

[0:19] It's best to remove the closing PHP tag in this situation. We do this because any white space or new lines added after the closing PHP tag can cause some unwanted side effects and errors in certain situations.

[0:34] Once we've removed that closing bracket, we'll see that PhpStorm gives us a hard warning now, denoted by this red squiggly. It says, expected semicolon. This is an error that will cause our script to fail. We can confirm this by saving this script and refreshing our page.

[0:55] This is because when we use a closing bracket, it implies that our PHP script has ended.

[1:01] Now that we've taken this closing tag away, we now need something to tell PHP that we are done writing PHP code. All we need to do here is add a semicolon to the end of this line. Now, if we save and refresh the page, everything's working again.

[1:19] This error may seem a bit silly, but semicolons really do play an important role in PHP in something called instruction separation. This is just a fancy way for us needing to tell PHP where one line of code ends and another begins.

[1:37] Before we add some more lines to our script, let's talk about line breaks. It's great to have everything on its own line, because it makes code reviews a lot simpler when you are comparing code from one version to a new version.

[1:51] Also, if you have some developers editing in command line and some not in command line, sometimes these terminal apps automatically add a new line to the end of scripts. It's just a good idea to always suffix every one of your PHP files with an empty new line.

[2:10] Back to our semicolons. Let's go ahead and duplicate out this line and add in a couple exclamation marks.

[2:18] Before PHP actually executes its code, it compiles it down and strips out all of the white space and line breaks. This means that this code will look more like this.

[2:30] If we didn't have semicolons, let's take them out, this is how it would appear to the PHP compiler. Again, PHP would be unable to determine where one line of code ends and another begins.

[2:43] If we go ahead and save this file and refresh, we can see a demonstration of that.

[2:49] By adding back in those semicolons, PHP can determine all of this at runtime and execute our script successfully.

egghead
egghead
~ 7 minutes ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today