Build Your Own Business Website header image

How to Use Opening and Closing PHP Tags

Difficulty Level -

Filed Under Topics -

Listed Under Lesson Subjects -

In this session we discuss how to use opening and closing PHP tags in functions and in statements within the function. We show that when using HTML intensive statements, we need to escape PHP to allow HTML to proceed and if it’s PHP intensive, we need to echo the HTML.

Video Transcript

Member: One of my things that’s just bugging the heck out of me, I can never figure out exactly what the rule is, the system is for the opening php tag you know, angle, bracket, question mark, a php… the closing php tag. And you know how you got to add… so when I say… because I know it’s all organized in your head and you know exactly what the rules are. But when you… like for instance, when I create a function or you create a function, I’m in the Responsive Child Theme course. I’m taking that. I’ve completed Lesson 5. And then sometimes you know, I don’t know where you start it and close it. And then the one thing, what I kind of figured I think I kind of figured out how you do it then there was the squiggly angle bracket. I forget what you call that. Not the you know, the squiggly angle one. But anyways…

Rick: This one down here? That one there?

Member: Yeah okay.

Rick: They call that curly braces so that’s…

Member: Curly braces, right. Yeah, I’m just a little nervous because I know there’s like 2 million people listening right now.

Rick: No there aren’t. They’re just a handful. Don’t worry about it.

Member: Okay, it’s not like a radio show. Okay so Rick, here’s the question. One time, just when I thought I had it figured out, you threw me off because those curly braces were enclosed inside of the opening and closing php tags. And then I’m like, “Okay now forget it. I have no idea why
you do what you do when you do with that.” So help me out.

Rick: Okay so it starts off with a function. When you’re creating a function, well, all the content of the functions begins and ends with a curly brace.

Member: Yes.

Rick: And so that’s one thing. Then you have stuff that happens inside of a function like an if statement or a while statement. Those also open and close with curly braces. So for example, let’s just look at functions. What do we have here? Let’s look at footer for a second. Okay so I don’t have any if. And by Lesson 5, I don’t have any if or while statements. So in Lesson 5, the curly braces are only used for opening and closing the content of the function.

Member: Right.

Rick: Okay? Inside of php, when you’re actually executing php which is what this file is, this file is .php and it starts with an opening php tag. When you’re inside of php, if you’re going to use HTML, you need to escape it. And so this escapes php and allows you to begin writing HTML. And then inside of my HTML, I want to go back to php so then I open up php. I do my little php bit and I close it. I could actually do it this way. I could actually get rid of that closing php and this opening php and it would open the php here. It does that php. It does this php. Now it wants to go back into HTML so we close out php again or escape php. Go back into HTML, do our HTML, escape or open up php again. So that’s the way this stuff works.

Now it’s semantically correct to have the HTML actually read as HTML. So for example, in Netbeans, because this reads as HTML, I can click on that div and it shows me the closing div. However, you’ll often see me inside of php, echoing HTML. So I don’t escape the php. I just echo the HTML instead. That’s another way of doing HTML but it doesn’t have this ability for you to track the HTML in the same way.

So usually if it is HTML intensive stuff then I escape php, go into HTML. If it’s php intensive stuff then I may very well echo the HTML. But that’s how I draw that distinction. But if this was gone, it would throw an error because I’m in php and then I have an HTML entity. So if I’m going to do an HTML entity, I can either echo it or I can escape php, allow HTML to proceed.

The ongoing assumption is that if it’s not wrapped in php tags, it’s HTML. So there’s our opening php, here’s our closing php, here’s our opening php, here’s our closing, here’s our opening, here’s our closing, here’s our opening, here’s our closing, back into HTML, so on and so forth. So because the content of it is primarily HTML and I called it HTML in my file, I escape the php and enter into HTML. However, in this case, I’ve chosen not to do it that way. And I’ve echoed the HTML and stayed in php the whole time.

Member: Gotcha.

Rick: So that’s the difference between these two. I could have rewritten this function by doing this and then coming back down here. So there’s my opening and closing php tags and then instead of an echo statement, I might actually want the tab anyway. But I don’t need to escape characters. Actually, that’s right. I don’t need the tab because I’m not using an escape character. So I’ll get rid of all my little escape characters and my closing thing. And then now here, I need to go back into php and leave php again. And then this, we’re just going to get rid of the… okay. And get rid of the escape characters and then we’ll just… okay, see these things are functioning the same, right? And I probably would do this. They’re going to work exactly the same.

Member: Yeah.

Rick: So that is the whole opening and closing php tag. Was that clear enough or…

Member: It’s very clear. Now I can… I should be able to go in and figure out line by line why you close the php… is that when you call it when you…

Rick: Yeah, you close php.

Member: Yeah, escape the php, do the HTML then you go back into php because it’s a php file. That makes total sense. I have watched… I’ve completed the lessons through 5 of the… I’ve watched the videos through 8 and there was some… I should be able to figure out on my own, if you can remember which file I’m talking about, that would be really cool. There was the file where there was the little curly brace and you put an opening php tag before the curly brace and then one right after it. And you did that twice within a function. If you remember and you could show
me that’s great. Otherwise, when I come across it again, I’ll figure it out using the principles that you just taught me.

Rick: Well, I’m guessing that it’s here. Here is an example of these curly braces. So we start off to here, opening and closing curly brace for the function. And then you have your if statement and the if statement, starts with the curly… now the content of the if statement starts with the
curly brace and closes it. Although when the if statement only has one action in it, you don’t actually need a curly brace. You only need a curly brace if there is more than one action.

So here, for example, there is no curly brace in this if else statement. There could be, it could go like this if  and then come over to this else and close it off and then come down here and close it off. And now you have opening and closing for the function, opening and closing for the if,  opening and closing for the else. And I didn’t do it that way. You know, I don’t know why I didn’t do it that way. I normally do it that way. Maybe I intended to explain that in the case of an if else statement like this, when there’s only one action inside the if or the else then it doesn’t actually need a closing… opening… it doesn’t need the curly braces. But… so that’s an instance where curly braces aren’t absolutely required.

And then if we come down to here, if is page and I’m escaping php… I’m escaping php for the div and then I’m entering back into php. And then escaping php and entering back in and escaping and entering back in… and then theoretically, I could be doing the same thing here but instead, I’m just echoing. That’s probably not the one you were thinking of.

Member: Okay so… if you just… yeah, if you can stop scrolling. Okay, let’s just say… okay, right there on line 40… no, let’s just say… yeah, line 53. You see the curly brace there?

Rick: Yup.

Member: I cannot remember the… I didn’t realize you had these question and answer sessions. I would have wrote it down. I can’t remember the exact file. But in that file, for instance, on line 33, there’s just one curly brace and just to the left of the curly brace is the opening php tag. And just
to the right of the curly brace, there’s a closing php tag and that’s what kind of threw me. It was… I can’t remember the file but I distinctly remember that because it was so unusual for me.

Rick: Well no, that does actually happen all the time. Here’s an example of it right here where you’ve got an opening php tag…

Member: There you go. That’s exactly what I’m talking about.

Rick: And you got your curly brace and then your closing php. It’s because…

Member: Walk me through that.

Rick: It’s got an if else statement that’s surrounded by HTML. So here we are in HTML and then we’re going to go to our if statement. And so our if statement is php so we go on to php. Although if this case is… if it’s a single then we’re going to write some HTML. So since we’re writing some HTML, I escaped php, go back into the HTML, do the HTML. Now that I’m finished with that if statement, I have to go back into php, close out the if statement with a curly brace. But I’ve got HTML here again so I’ve got to close out the php and go back into HTML.

Member: Okay now I understand.

0 Comments… add one

Save $200 on Membership Now!

Start learning today for as little as
$0.82 PER DAY!
Subscription Options
0 comments… add one