1. 7
    Write short echo tags in PHP
    1m 53s

Write short echo tags in PHP

Mark Shust
InstructorMark Shust
Share this video with your friends

Social Share Links

Send Tweet
Published 3 years ago
Updated 2 years ago

PHP's echo statement is so common, it comes with an alternate "short echo tag" syntax. Let's learn how to use this syntax to clean up our view code.

Instructor: [0:00] Before we go any further, did you know that you can mix and match HTML and PHP?

[0:06] Let's consolidate this PHP tag to one line, and go ahead and close up this PHP tag, and then we will wrap this within an h1 header tag.

[0:18] If we save it, and go ahead and refresh the page, we will see our echo is now wrapped with an h1 tag. That's pretty cool.

[0:25] Since using this open PHP tag along with an echo statement is so common, PHP created a shorthand syntax for doing this, called a short echo tag.

[0:37] Rather than typing a normal PHP tag, we can actually replace this PHP in the echo with an equal sign. When we use this open bracket question mark equal sign syntax, we are essentially saying PHP echo.

[0:52] Finally, since these short echo tags are always closed up right away with a closing PHP tag, this tells PHP that the execution is complete.

[1:02] Because of this, we also don't need the semicolon. Let's go ahead and remove that. I never use semicolons with short echo tags, because it just makes things a lot cleaner and easier to read.

[1:13] Wasn't this update pretty neat? Let's go ahead and save this, and refresh the page and we will see everything still outputs exactly the same.

[1:21] This short echo tag is exactly equivalent to the same PHP echo statement that we used before. When you have the option to use two different pieces of code, always opt for the version that is the shortest and doesn't sacrifice readability.

[1:38] Less code almost always leads to fewer bugs, because it makes your code easier to reason about when it comes time to debug code that isn't working. When presented with these two options, I always opt for the short PHP echo tag.

egghead
egghead
~ 19 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