PHP Tutorial (& MySQL) #3 - Your First PHP File

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
alright then gang so now we know how PHP files are being run on the server and how we're serving up these pages to the browser using the local development server what I'd like to do now is create a folder inside htdocs' where we're going to contain all of our different course files for this project or this series so I'm going to go into xampp then into HT docs and i'm going to create a new folder and I'm gonna call this folder torts tutorials right so now every time we create some code I'm gonna place it inside this twits folder and when we preview this in a browser we're gonna go to forward slash it's over here okay so then now we have that folder set up let's go to sublime editor and I've opened this folder already inside sublime as well and what I'm going to do is create our first PHP file so I'm going to right-click go to new file and I'm going to ctrl s to save this and we're already in the totes folder so I'm just going to call this index dot PHP so our PHP files are going to have this dot PHP extension now if we want to start to write PHP code inside here it's not enough to call this dot PHP we also have to embed our PHP code inside PHP tags a bit like when you create HTML you create HTML tags we have PHP tags for PHP code as well so to do that you do an Open bracket question mark PHP and then when we come to close the PHP tag it's question mark close bracket now in sublime there's a shortcut I can just type PHP and then tab and then it does it for me so now what we could do is write some PHP code inside this tag right here now we could keep it all on one line and we could do a PHP statement right here or if you're writing multiple lines of PHP you'll find that people just enter down a couple of lines and write the PHP inside the tags like that either way is fine so what I'm going to do now is show you a very simple PHP statement so I'm going to use a statement called echo and then I'm going to say a string after the echo statement now a string is just like a sentence or a collection of letters and numbers and symbols inside quotation marks and we'll learn more about Strings later on I'm just gonna say something like hello oops if I can spell it correctly that is hello ninjas okay now important at the end of every PHP statement you need to do a semicolon if you don't add that on then it's gonna error and I'll show you that in a second okay so let me save this now and let me run this inside the browser so if I go to the browser and I go to forward slash totes which was the father we created remember inside here we've just created an index dot PHP file we don't need to write index dot PHP because when we do forward slash talks into the folder it's going to automatically look for that index dot PHP file or an index dot HTML file if it exists so if I click enter it's gonna find that file run it and we can see this now this hello ninjas okay so that is what this echo statement did for is right here it takes this string and it essentially echoes it to in a browser but what's actually going on here behind the scenes if we look over here and we right click on inspect then we can see if I just zoom this in that this is actually an HTML page right now we requested a PHP page it was index dot PHP and we're getting an HTML page back and hello ninjas is being output into the body so what's going on well what's happening is we're requesting the index dot PHP page that is going to the server finding that page and it's running the code inside the PHP ok so inside here so it's running that and that is outputting a string now it takes that string this thing right here hello ninjas and it sends it to the browser and when it reaches the browser the browser interprets that string as HTML and it puts it inside this HTML document because a browser can't actually run a PHP file it knows HTML so it's rendering whatever is returned from the PHP inside an HTML document okay so that's what's going on right there now I want to show you what happens if we don't add on this semicolon if we don't do that and we try to run this again by refreshing it still works but if we then try to echo something else after it hello gain then this is not going to work so refresh that and now we get a pass error syntax error unexpected echo expecting a comma or a semicolon so we have to add on our semicolons at the end to say look this is the end of this particular statement then it can go on to the next one and this will work so if I save this now and refresh in the browser then we see this again okay cool alright then so let me just delete that second one and in fact I'm going to comment this out as well now the second thing I wanted to talk about is how to actually embed this PHP stuff inside HTML code so what I could do is I could set my HTML tags I'm going to press tab and that's going to create this little boilerplate for me I could create this HTML template inside this PHP file and inside this HTML template I could do some PHP tags so for example I could first of all let's give this a title I'll just say my first PHP file and then below that in the body I'm going to do an h1 now inside that h1 I could say hello ninjas but instead I'm going to use PHP to echo this ok so if I do PHP tab to create the tags and then say echo and the string I want to echo which is hello ninjas ok and then semicolon now what's going to happen here we're going to request this file in the browser it's going to go to the server and it's going to run this PHP file wherever it sees any PHP like this it's going to process that PHP and this is going to result in just this hello ninjas text being output in this position so it would be the same as this that's what it would result in okay so we're running that PHP is resulting in that text being embedded between these h1 tags and then is taking the resulting HTML and it's sending that to the browser the browser receives that interpret it as an HTML file and renders this the resulting HTML template to the Dom so if we save this now and preview this again reefer then we can see that right here and it's bigger because it's inside an h1 now so that's really cool that's how we can easily embed our PHP inside our HTML templates so this is another reason PHP has been so popular over the years the fact that we can easily mix our HTML and PHP together to return this dynamic HTML template to the browser now this might seem pointless at the minute because we could have easily just hard-coded hello ninjas instead of the PHP tags and it would have given us the same result but imagine if the stuff that we're outputting here inside the PHP tags it wasn't just hard-coded strings but instead it could be dynamic data such as user information on a user dashboard or maybe product information from a database that's ultimately the goal here to output dynamic content to the HTML templates and perform some kind of manipulation of them okay so now we know how to create these PHP files and now we know how to embed our PHP inside the HTML itself in the next video what we're going to do is talk a little bit about two things called variables and constants
Info
Channel: The Net Ninja
Views: 130,452
Rating: undefined out of 5
Keywords: php, tutorial, php tutorial, php tutorial for beginners, mysql, mysql tutorial, mysql tutorial for beginners, sql, sql tutorial, php for beginners, learn php, php mysql, php and mysql, php file, .php, php echo, echo, ?php, php tags
Id: ABcXbZLm5G8
Channel Id: undefined
Length: 7min 54sec (474 seconds)
Published: Tue Jan 29 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.