PHP Basics

PHP Tags

Maximiliano Firtman

Maximiliano Firtman

Independent Consultant
PHP Basics

Check out a free preview of the full PHP Basics course

The "PHP Tags" Lesson is part of the full, PHP Basics course featured in this preview video. Here's what you'd learn in this lesson:

Maximiliano creates a simple PHP file and demonstrates PHP tags. These tags contain the executed PHP code. Anything outside a PHP tag is considered string output. The closing tag can be omitted when a file only contains PHP code, and static string output is not wanted.

Preview
Close

Transcript from the "PHP Tags" Lesson

[00:00:00]
>> Maximiliano Firtman: So at this point, you should be able to open a terminal or a command line interpreter somewhere, and type php -v, and you should see something, okay? That means that you are ready to execute some PHP. Is that a server? No, there is no server here running, we're going to separate the steps here.

[00:00:21]
So now, in VS Code, when you go to Extensions and you search for PHP, they are, as I mentioned, docents or extensions. And all the extensions say this is the best one. This is the most complete extension. Actually, you just pick the one you prefer, or the most downloaded one.

[00:00:42]
Of course, there are some extensions that will let you debug PHP code, that by default, is not like a simple operation or a straightforward. So debug, I'm talking about using a break. So then always, step by step, line by line, executing your code as if it's a Java code.

[00:01:08]
Actually, that was impossible 15 years ago in PHP. So the ones that I'm using right now, but you can install anyone, I'm using PHP Debug, this one, okay? This one is the most, we have this one is 12 million downloads, okay, 3 million downloads. These are the ones that are good, but they will start all the time pushing you into buying some pro plan, okay?

[00:01:36]
Premium plan, okay? Which is okay, I mean, the good thing is that you can try, I can install, and if I don't like it, it's just one click to uninstall it. So it's really quick to try several until you're happy. What happens if you don't have an extension?

[00:01:52]
At least you need one for syntax highlighting. VS Code has the default syntax highlighting anyway, but nothing will happen. Because we're not going to use actually the IDE a lot, it's just code editor, and then we can use the terminal. So, something that I'm going to do here now is, I'm going to work with a playground folder, that actually you can chat there to open that.

[00:02:20]
It's just a folder, it empty, okay? I will just open that, so I will play there with some scripts okay, it's completely alone, it's not really a project yet, we are just going to start working with the syntax. So I'm going to create a new file. How would you call that file, anyone, next test.php.

[00:02:41]
PHP is using the PHP extension. There are other extensions as well that you can use for some special situations, but PHP is 99% of the time. So, you can see that now that I have .php, I have my little elephant as the icon, okay? So, you know that is recognizing the language also.

[00:03:01]
Look at the bottom of VS Code, you should see PHP as your language. That means that you can start typing PHP. Now, how do you run the script? Well, we have a run here, the run will be connected to one of the plugins. If you don't have the plugin, you won't be able to run it from there.

[00:03:22]
But anyway, forget about the run for a second, in the real world, you won't use the run a lot, to be honest. You can open a terminal, and you can say php space and the name of the script, like test.php, okay? And it's doing nothing because I'm doing nothing, okay?

[00:03:41]
But if you don't get any error, it means that, okay, it's there. Now, you can see that I'm running this like Python code, I'm not sure if you have experience with Python. That is also a scripting language, where to run a Python code, you go to the terminal, to the command line and say Python space and the name of your script.

[00:04:00]
Or with Node, node space and the script. The same happens here, is that a web server? No, it has nothing to do with the web yet, it's just an encrypted language. Now, let's go back to the PHP code here and let me write this. I'm going to refactor this, but I want you to get the surprise maybe.

[00:04:20]
Maybe I will just say hello world. I said, but Max, there is no print or console log or something like that, just a string, okay? Hello world, just that. I will run this, and I'm receiving hello world in the console, and it's kind of weird, right? So, it's just a text, so if I put some text, it will output the text.

[00:04:46]
So what's going on? It doesn't make any sense for most of you, I'm sure. [LAUGH] So what's going on is that, by default, the .php file, remember, PHP was created for helping HTML. Actually, the H in the middle is still hypertext, that is the same H as in HTML, okay?

[00:05:08]
And actually a .php file, by default, will treat every string that you have in the file as an output. So it's a literal output by default. That most of the time will be HTML, but there is no need to, it's just text, okay, it's just a string. So, is that PHP code?

[00:05:29]
Not really, not yet. Actually, PHP code will be within what is known a tag, a PHP tag, okay? What's a PHP tag? The normal tag is this one. You open a tag, question mark, php, that's a tag, that's a PHP tag. And you can close it like this.

[00:05:58]
So now if I move my Hello world here, now I'm getting an error. Okay, that looks better, we were expecting errors before. So if I try to execute this, I'm getting a syntax error, okay? Because yeah, what is this? This doesn't look like code, okay? Maybe I need to create a variable or maybe I wanna output this to the terminal.

[00:06:23]
And the output, the console log, the print here is called Echo. And then I can pass a string. And these other a function, it is a function. So if you want, you can add parenthesis, but Echo has, because again, this is PHP, it's mega, super flexible. Echo, because I'm using echo a lot, they say, you know what?

[00:06:46]
If you don't want to, don't add the parentheses. So you can call the echo function without parentheses. So now, if I execute this, I'm seeing the same Hello world, kind of the same Hello world, okay, than before, but now written by PHP, before it was just output. Can I have both?

[00:07:13]
Yes, why not? One is a static output, and the other one is a is a dynamic output, okay? So you can see that this is completely different from probably everything that you have done so far in other languages. It's not the same as Python, or Node.js, or Java, or .NET, so it's completely different.

[00:07:40]
Actually, at the time, we were talking about, again, the end of the 90s, late 90s, that was the common architecture. So I was doing ASP, that was a Microsoft version of PHP, and ASP was actually something like that called Fuchsia. Have you ever heard about that? It from originally macro media, and then Adobe, was also something like that.

[00:08:04]
So it was the common pattern at the time. Do you have any question? We will continue talking about that, but yeah.
>> Student: Derek says, his format is removing the closing question mark in the bracket.
>> Maximiliano Firtman: What is happening is that the person in the chat, he or she, installed one of the plugins, or one of the plugins has linter.

[00:08:32]
A linter is a tool that is checking your code and giving you some best practices. And probably, you have, and underline here, saying, hey, there is a warning here, you should remove this. Okay, let me give you a spoiler, because I'm thinking, should I wait to a couple, cuz I will explain that, but let's do it now.

[00:08:51]
So, what happens with this? And I will answer the question with time. Give me two minutes to answer the question. Why do you get that warning message? That may be a lot of you., it depends on the plugin that you have installed, if it's evaluating that or not.

[00:09:04]
I can continue, I have Hello world, Hello world from PHP, is this still alive? So what happens if I continue writing code outside of the PHP tag? Will it work? Well, actually, it works, you can see that new lines are not what you're expecting, so I don't have a new line here, okay?

[00:09:27]
So it seems like this new line is not taking, so what if I add another new line here? Okay, now it's better. I can continue, can I open another PHP tag? Yeah, sure, you can open as many as you want. So you can mix and match between string output and PHP execution at the same time.

[00:09:51]
In modern PHP, sometimes you don't want to mix and match output with PHP. And of course, some of you don't like this, because you have never used this paradigm, so it feels weird, I know, I know, it feels really weird. You just want to write code, not mix a string output with code in the middle, okay?

[00:10:17]
So sometimes they may have another test here, onlyphp, let's call this onlyphp.php, okay? Where, you know what? I just wanna open tab PHP and just write PHP, I don't wanna write anything else. So let's say here I can have a code that is doing something, but let me jump into creating a variable.

[00:10:36]
You create a variable like this, my name is Maximiliano, I will go into the details in a second, okay? And then I will just say, let me do this. Let's make it really simple, really, really simple, okay? So let's say this is my code, okay? By the way, can I try this?

[00:11:00]
If I try this, it says Maximilian, so you can see, depends on the plugin. If you have the plugin, you can just hit play. And play is just doing the same thing as executing php space, the name of your script, okay? So it's a Maximilian, which is okay.

[00:11:13]
So what's the problem or why if this premium linter shall ascertain to delete the closing tag? Because after the closing tag I can continue making outputs, okay? So maybe I can add spaces, maybe I can add a new line, or maybe I can add any string, okay? Any output will work.

[00:11:36]
And of course, it works. The thing is that, if you don't want to match and mix string output with PHP, you don't want. When you add a closing tag, you maybe, without wanting to, you are adding a space, you are adding a new line, and that will go to the output as well.

[00:11:58]
And maybe that's not your intention, so the space, the new line, will also be part of the output. So you can see that my output has two lines here. Can you see, I'm not sure if you see it? Yeah, there are two lines. Why there are two lines?

[00:12:11]
Because I have a new line here. So to avoid that, to actually say to the PHP interpreter, you know what, I'm not going to add more characters after the PHP script or the tag ended, just remove the ending tag. That would also, I know, will make a lot of noise to your brain, okay?

[00:12:31]
You open a tag and you don't close it, come on. But yeah, the reason is to avoid those extra characters that you may not want to be in the output, okay? So you can just open the PHP tag and not close it. Does it make sense? So that's the final answer to the question.

[00:12:53]
And it's a common scenario when you are not mixing string output with PHP code itself.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now