Basic PHP Syntax - PHP 8 Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] let's write some php we need to open the project directory from the htdocs within our code editor so we'll need to click on the open here and then open the xm folder and then htdocs and this is the folder we'll be using for this video so we click on open and now we need to create index.php file and a quick note here is that if you want your file to be interpreted as php then your file must end with that php and not with that html the php files can also have html css and even javascript in them so it would work pretty much the same way the way php is parsed is that it looks for the opening and closing tags and interprets everything in between as php so the opening tag is less than sign question mark and then php and closing tag is question mark and greater than sign so basically anything in this code block here will be interpreted as php in some editors you might notice this squiggly line here and it tells you that it's redundant closing tag which means that if your file entirely contains php code meaning it's 100 php then you do not need the closing tag and in fact we should not have the closing tag in those files and that is to make sure that no accidental white space or new lines are added after the php closing tag which could mess up your website so for example if i were to put hello right here by mistake and then open the browser we'll say hello would be printed even though we might be processing some php here and then rendering some page in here you would just add that unwanted space and unwanted text right after it so the general rule is that if your php file is only php code then you should not have the closing tag if you're embedding your php within your html then you need to enclose your php code with the opening and closing tags so we'll remove that for now and let's print hello world to the screen so we're going to do that by using echo and end it with a semicolon so if we go and refresh the page we see hello world in the browser and if we inspect element here we only see the hello world and we don't see any of the actual php code that's because php is a server side language even though this seems simple it actually introduced us to three new things echo is what you would use to print something then we enclose the text within either a single or double quotes which indicates that it's a string and don't worry about the string or data types right now we'll talk more about the types later and finally the statement ends with the semicolon if you're coming from a python or a javascript background get used to writing semicolons because if you miss it this is where you're gonna get a parser so make sure to always have a semicolon there there are a couple of exceptions where a semicolon is not needed for example if you're closing the php tag it will automatically assume the semicolon on the last line before the closing time so if we close the tag right here and refresh the page it will work so basically you don't need the semicolon on the last line this is useful when you're embedding php within html and it's just a single line in those cases you don't need the semicolon if you have multiple lines though it's a good idea to stay consistent and just use the semicolons you could also execute this php script within your terminal so if we open uh xm control panel and click on shell here this will bring up the terminal we need to cd into our project directory which is htdocs program with geo and then you could run php files using the php command so that's php index.php and that prints hello world right here so you could basically execute your php scripts in command line if you want to another way to print something is you could use print which essentially is the same thing as echo it just has couple of differences and i'll mention those differences now for example print has a return value of 1 which means that this expression right here actually returns value and what i mean by that is that if i do echo and then print what this will do is that it will print out hello world and then append one at the end because this expression itself returns one so if we refresh the page here we see that one right here this means that print could be used within expressions while echo can't so for example if we did this the other way print echo this would not work and we would get the syntax error also you could call these like this with parentheses and it would still work and same goes with echo the other difference is that if you don't use parentheses and you're just echoing out text you could actually comma separate multiple words so for example you could do hello and then comma space and another comma and world and this would basically concatenate all these three into one and print that to the screen so if we refresh we get exactly same thing you cannot do this with print also echo is marginally faster than print so i would just suggest to stick with echo unless you have a specific reason to use print now before we move on to something else what if you wanted to print something like joe's invoice and as you can see editor is already warning us that there is something wrong here if we refresh the page we get syntax error and that means that we just need to escape this quote in here or we need to enclose it within double quotes so one way is we could use backslash and that will work the other way is we could enclose these in double quotes and that would still work so what if you wanted to assign these to a variable the variable should start with a dollar sign so we could define something like name and call it geo and then echo that variable out and if we refresh the page the name is printed correctly there are a few rules when creating variables in php they must either start with a letter or an underscore the letter can be either uppercase or lowercase but it cannot be a number or a special character for example you cannot start a variable with the number you'll get a syntax error but you could start with the underscore and then numbers and that would work so if we put it here that would work the other rule is that you cannot have special characters like these for example also you cannot assign a value to a variable called this and that's because this refers to the object and we'll talk about objects in later videos so don't worry about it now just know that you cannot set this to hello and as you see some editors will underline this and tell you that you cannot reassign this and if you refresh here we'll get a fatal error the variables in php by default are assigned by value let me show you what i mean so if we have a variable called x which equals to 1 and then we have a variable y which equals to x and then we change the value of x to 3 and then we print y what will be printed is 1 and not 3. that's because variables are assigned by a value on the other hand if you actually wanted y to change whenever x changes then we need to assign variables by reference instead of by value and this might sound confusing but it's actually pretty simple also don't worry about it too much we'll talk about this more in later videos and you'll see how we're going to use these but to assign a variable by reference you need to add the ampersand right here so now y is equal to the reference of variable x so anytime x changes the y will also change so right now if we refresh the page the y is equal to 3. so now that you know what variables are let's create a variable first name here and call that geo and let's echo hello first name let's see what we get and it's not what you would expect the first name here is just being printed as is and that's because we're enclosing it within single quotes with single quotes the text gets printed as is meaning that you cannot use variables inside if you want to get the value of the variable then you need to change the single quotes to double quotes and now if we refresh this page we see hello geo to me however this is not that readable it is entirely up to you but you will see some developers and close the variables with the curly braces like this which adds more clarity that it's a variable and if you refresh it still works another way is that you could concatenate text with a variable and we'll talk more about operators later so just bear with me here we can replace this with hello and then we use a dot and then first name and that here is a concatenation operator and if we refresh the page it still works so we have covered how to run php and command line how to run php in browser but how do we actually embed php within html so let's replace everything here with the basic html and refresh and we have the html so let's say we wanted to print a hello world instead of this heading here using php and as mentioned before if you want to execute php code you have to enclose it uh within the opening and closing tags so we're going to open php here and then echo hello world and close if you refresh that works now there is a shorter version to this which is less than sign question mark and then equal sign and that pretty much is the same thing as php echo and then whatever you want to echo and as you noticed i'm omitting the semicolon here because it's just a one-liner and there is no reason to have a semicolon here and that would still work so if you just need to print something you should use this shorthand version if you need to process some php then you need to enclose your php code block within the php opening and closing tags so for example you could assign a variable here equals 10 y equals 5 and then you could echo x comma y and if you refresh it gets printed correctly now you could also echo out html directly from the php so for example let's move this right below and here we could print the content within a paragraph for example and this would still work if we refresh the page this is printed within paragraph because we can inspect element and we see that it's within the paragraph this can be useful for things like dynamically generated htmls for example but in general i would say that it's not good idea to mix html directly in your php we'll talk more about separating your presentation logic from your business logic in later videos so let's talk about comments how would you comment something out say that three months later you come back and you have no idea what this means for such cases you would probably leave comments on your code so one way to leave a comment is using double slash and then just type your comment and this is just a single line of a comment the other way of writing a single line of comment is using the hashtag if you want multi-line comments then you would write it like this so it's pretty much the same as other languages and you could have multiple lines here and if you've seen something like this before this is something called duck blocks which you would use to write the documentation for your source code and we'll cover those more once we move on to the object oriented php for now you don't need to worry about it now there is a couple of things you need to know about the comments if you put the comment on the same line as your closing php tag right here so if we were to put a comment right here it's not going to comment out this closing php tag so if you put something after it like hello you cannot expect that this would comment out the hello here it would still print hello another thing to note is that you should not nest multi-line comments so for example if you have a multi-line comment here you should not add another comment right here because this will result in error if we refresh we'll get a syntax error so this is it for this video we've covered the basic syntax you know how to echo something out to the browser you know how to run php in the command line you know how to embed php within the html you know about variables how to write comments and so on in the next video we'll talk about constants and variable variables and if variable variables sounds confusing to you it's not so thank you for watching please hit like and subscribe and i'll see you on the next one
Info
Channel: Program With Gio
Views: 10,697
Rating: undefined out of 5
Keywords: php, php8, learn php, php tutorial, php course, learn php the right way, beginner friendly php course, full php course, php in 2021, advanced php course, beginner to expert php, php syntax, php embedded in html, php in html
Id: HrtS-FkPBqk
Channel Id: undefined
Length: 12min 32sec (752 seconds)
Published: Thu Nov 26 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.