PHP Structure and Syntax

John Elder
A free video tutorial from John Elder
Founder of Codemy
4.4 instructor rating • 58 courses • 150,251 students

Learn more from the full course

Top Ruby And PHP Programming Bundle

Learn Ruby for Software Engineering and PHP For Web Development Quickly and Easily! Become a Coder Fast!

07:39:13 of on-demand video • Updated November 2020

  • Ruby From Beginner To Advanced
  • PHP From Beginner To Advanced
  • Programming Concepts Applicable To Any Programming Language
  • Object Oriented Programming
  • Conditional If/Else & Comparisons
  • Variables, and Data Types
  • Conditional If/Else & Comparisons
  • While and For Loops
  • Functions, Modules, & Classes
  • And More!
English [Auto] OK. This video I want to talk about the structure and the format of HP code. So in order to use HP to get rid of this high thing here we need to have PSP brackets so all PSP code starts with this bracket. And then the words BHP and then a question mark and a closing bracket. So this is a PDP tag right and anything inside of this tag whatever Ri right. That's actual page code. So let's just write something real quick let's type an echo and then quotation marks and then type in hello world. And now after the quotation marks put a semicolon. So that's another characteristic of P P. Every line of P P code will end with a semicolon. And if you're like me you're gonna forget the semicolon I always forget it I program python I program Ruby page P tons of programming languages and they all have little weird quirks and a lot of programming languages like Ruby is one of my favorite at the end of a line of Ruby code you don't put anything right. Sometimes at the end of the line of a python code you put a colon. So you just kind of have to remember at the end of a peach pea line of code you always want to put your semicolon sometimes you can get away with not putting it and you'll probably see me throughout this course forget to put it in there and it'll still work. Sometimes it doesn't work and it throws an error just sort of the way it goes so echo this does exactly what it seems like it would do it echoes out whatever we put in quotation marks onto the screen so we can go ahead and pick that control asked her command s if you're on a Mac to save this file or come up here and click save and then head back over to our web browser and we're in this local host slash course slash index that HP directory and file if we hit reload. Now boom is this hello world. So congratulations you've just written your very first BHP file your first bit of Peach Pit code this is always the first program every coder creates in any programming language is the hello world program so congratulations it's a lot of fun I realize it's not that particularly interesting but we'll get more complicated very very quickly so that's the format of HP you have an opening tag this question mark HP and a closing tag Now you notice this is all on separate lines we can put all of this on one line if we wanted to. This is equally valid if I save this and head back over to our web browser and click reload nothing at all has changed so I just sort of a matter of what you're working on if I was going to write this actual code I would probably write it like this right. If you're just doing one line of code this is perfectly fine. But most times you know you're gonna be doing multiple lines of code. So you want to kind of format it like this. The next thing I want to talk about our comments whenever you're writing code. It's always important to comment your code because six months from now you're going to have to come back and tweak something and you're going to forget what your code does is very common. So you need a little comment to kind of refresh your memory and say oh that's what I'm doing in this little section of code or if you're working with a team of other coders right. If you're all working on the same file you may not know what Bob over there is doing. You might look at the code and go What is he. What is this code supposed to do. Sometimes it's not obvious. So Bob should write a comment saying you know whatever this bit of code is so to do a comment. There's a couple of different ways. The most common is to forward slashes and then you just type in whatever your comment is let's let's say print out to the screen. All right. That's that's a good comment right. So that's one way to write comments. Another way is using multiple lines so you can do one for Slash and the star and then another star and then another forward slash. And now inside of here this is a multiple line comment. So this is also equally valid if you've got a lot of stuff to write in your comment you add on multiple lines but you don't really want to do that. Collins should be short and they should be sort of to the point. Sometimes you just need if you're writing a major block of code you might need a few lines to comment and really explain what it does that you use multiple lines like this. Now you can also do this on the actual line of code itself right. So you know just at the end of your whatever you can put this and anything from here to the end of this line will be commented out. Go ahead delete that. Now if we save this file and head back over to our web browser and click reload and if you're right click and view the page source itself you see just as Hello World. All those comments aren't showing up on the screen because these are the comments aren't for the web page. This is for us kind of behind the scenes. So that's cool. So they get rid of this comment and we'll leave that one. Now the last thing I want to talk about in this video is how HP interacts with each CML. So if you're familiar with each himself L stands for hypertext markup language each HTML is the computer language not really a computer language but we treat it like it is. But it's the computer language of web pages all web pages on the internet every single one is written in HD CML. So if you go to any Web site your favorite Web site go to go to become my Web site. If you're right click anywhere on the page and click View page source. You see this is the HD CML. All of this stuff is the HD Himmel it makes up this this file now p p works inside of h t AML so you should if you want to use HP for web development you really do need to know each team well at least a little bit. So when I learn HD him all too much in this course we're gonna use it but we're going to use something called bootstrap. So go to get bootstrap dot com and bootstrap is a CSX framework that allows us to do all kinds of cool styling stuff on our Web page and we're going to use this a little bit in this course a click on documentation and then just scroll down to the starter template. I'm going to click here to copy all of this code and head back over to our indexed HP BHP page just right click at the top and click paste paste in all of this code right. So now we can come over here and I'm going to just right click and copy this and I'm going to cut it and then come back here to where it says H 1 and delete what's in there and we can put this on multiple lines and I'm just going to right click and click paste or control V on my keyboard to paste in our HP code and you notice I tab this stuff over so that every time we write Peach Pit code it's sort of indented inside of our p p tag. You don't have to do that. That's just the convention to make it easier to read. So OK. So what we've got here is this is just a basic starter template file of each demo. And so if we save this this uses the bootstrap CSX framework which styles the thing a little bit. So if we come back to our our home page now our web page remember it said hello world like this. Now if we click this boom it says hello world in big text. And if we look at our code that's because we've wrapped this in this h1 tag each one means headline one it's a big headline. Right. So I'm also going to type a line break that should push it down. I'm also going to type in a center tag this will center everything in the middle of the screen. So then we need to come down here and in here close our center tag OK. So we save this come back to our web browser and click reload boom. It kind of you know puts center and moves it down a little bit. So that's how HP interacts with HDMI. It just goes inside your HMO code. So whatever your web page is wherever you want to do p you just add this little peach tag if you look at this tag it looks sort of like an HD e-mail tag right. These are all HD l tags and you can see they all have a sort of an opening bracket and a closing bracket. Right. Well that's just like our HP. Here we have an opening bracket and a closing bracket. The only difference is we wrap it in this question mark and P HP in this closing question mark. So it is kind of similar they kind of go together and it kind of looks the same you know. And so that's that's kind of cool. So I'm going to space this stuff down just to kind of get it out of the way and so throughout the rest of this course we'll be working right here in this file and and learning different p p and as we do we can then kind of toggle back and forth to this thing and hit reload and see the changes we've made. And that'll be cool. So that's all for basic PDP structure and syntax. Just remember PDP goes in HD e-mail files most of the time it has an opening tag and a closing tag all HP lines of code and with a colon of a semicolon. And remember to comment your code two slashes that's what you use most of the time or you could do the multi line one like we looked at earlier in the video. So that's all for this we do it in the next video we'll jump in and start learning more actual programming concepts and we'll look at variables.