PHP Tutorial (& MySQL) #3 - Your First PHP File
Skills:
Backend Performance70%
Key Takeaways
Creates a first PHP file and runs it on a local server
Full Transcript
all right 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 HT docs 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 xamp then into HT docs and I'm going to create a new folder and I'm going to call this folder TS tutorials right so now every time we create some code I'm going to place it inside this touch folder and when we preview this in a browser we're going to go to slouch 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 crl S to save this and we're already in the touch folder so I'm just going to call this index.php so our PHP files are going to have this PHP extension now if we want to start to write PHP code inside here it's not enough to call this 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 blind 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 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 so I'm just going to 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 going to error and I 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 slts which was the folder we created remember inside here we've just created an index.php file we don't need to write index.php because when we do slouch into the folder it's going to automatically look for that index.php file or an index.html file if it exists so if I click enter it's going to find that file run it and we can see this now this hello Ninjas okay so that is what this Echo statement did for us right here it takes this string and it essentially Echoes it to the browser but what's actually going on here behind the scenes if we look over here and we right click and 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.php and we're getting an HTML page back and hello is being output into the body so what's going on well what's happening is we're requesting the index.php page that is going to the server finding that page and it's running the code inside the PHP okay 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 HT ml 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 again 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 all right 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 type my HTML Tags I'm going to press Tab and that's going to create this little boiler plate 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 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 the H1 I could say hello Ninjas but instead I'm going to use PHP to Echo this okay 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 okay 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 ninja 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 it's resulting in that text being embedded between these H1 tags and then it's taking the resulting HTML and it's sending 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 refresh 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 hardcoded hello Ninjas instead of the PHP tags and it would have given us the same results but imagine if the stuff that we're outputting here inside the PHP tags it wasn't just hardcoded 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
Original Description
Hey gang, in this PHP tutorial we'll create our first PHP file and run it on our local server.
----------------------------------------
🐱💻 🐱💻 Course Links:
+ Course files - https://github.com/iamshaunjp/php-mysql-tutorial
+ VS Code editor - https://code.visualstudio.com/
+ Materialize Playlist - https://www.youtube.com/watch?v=gCZ3y6mQpW0&list=PL4cUxeGkcC9gGrbtvASEZSlFEYBnPkmff
🤑🤑 Donate
+ https://www.paypal.me/thenetninja
🎓🎓 Find me on Udemy
+ https://www.udemy.com/user/47fd83f6-5e4a-4e87-a0f0-519ac51f91b6/
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from Net Ninja · Net Ninja · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Regular Expressions (RegEx) Tutorial #14 - Matching a Username
Net Ninja
Regular Expressions (RegEx) Tutorial #15 - Email RegEx Pattern
Net Ninja
Regular Expressions (RegEx) Tutorial #16 - Finishing Touches
Net Ninja
GraphQL Tutorial #1 - Introduction to GraphQL
Net Ninja
GraphQL Tutorial #2 - A Birdseye View of GraphQL
Net Ninja
GraphQL Tutorial #3 - Project (stack) Overview
Net Ninja
GraphQL Tutorial #4 - Making Queries (front-end preview)
Net Ninja
GraphQL Tutorial #5 - Express App Setup
Net Ninja
GraphQL Tutorial #6 - Setting up GraphQL
Net Ninja
GraphQL Tutorial #7 - GraphQL Schema
Net Ninja
GraphQL Tutorial #8 - Root Query
Net Ninja
GraphQL Tutorial #9 - The Resolve Function
Net Ninja
GraphQL Tutorial #10 - Testing Queries in Graphiql
Net Ninja
GraphQL Tutorial #11 - GraphQL ID Type
Net Ninja
GraphQL Tutorial #12 - Author Type
Net Ninja
GraphQL Tutorial #13 - Type Relations
Net Ninja
GraphQL Tutorial #14 - GraphQL Lists
Net Ninja
GraphQL Tutorial #15 - More on Root Queries
Net Ninja
GraphQL Tutorial #16 - Connecting to mLab
Net Ninja
GraphQL Tutorial #17 - Mongoose Models
Net Ninja
GraphQL Tutorial #18 - Mutations
Net Ninja
GraphQL Tutorial #19 - More on Mutations
Net Ninja
GraphQL Tutorial #20 - Updating the Resolve Functions
Net Ninja
GraphQL Tutorial #21 - GraphQL NonNull
Net Ninja
GraphQL Tutorial #22 - Adding a Front-end
Net Ninja
GraphQL Tutorial #23 - Create React App
Net Ninja
GraphQL Tutorial #24 - Book List Component
Net Ninja
GraphQL Tutorial #25 - Apollo Client Setup
Net Ninja
GraphQL Tutorial #26 - Making Queries from React
Net Ninja
GraphQL Tutorial #27 - Rendering Data in a Component
Net Ninja
GraphQL Tutorial #28 - Add Book Component
Net Ninja
GraphQL Tutorial #29 - External Query File
Net Ninja
GraphQL Tutorial #30 - Updating Component State
Net Ninja
GraphQL Tutorial #31 - Composing Queries
Net Ninja
GraphQL Tutorial #32 - query variables
Net Ninja
GraphQL Tutorial #33 - Re-fetching Queries
Net Ninja
GraphQL Tutorial #34 - Book Details Component
Net Ninja
GraphQL Tutorial #36 - Styling the App
Net Ninja
GraphQL Tutorial #35 - Making a Single Query
Net Ninja
Build Apps with Vue & Firebase - Udemy Course
Net Ninja
Updated Vue & Firebase Course (Udemy)
Net Ninja
Vue & Firebase Real-time Chat (Preview) #1 - Intro
Net Ninja
Vue & Firebase Real-time Chat (Preview) #2 - Project Structure
Net Ninja
Vue & Firebase Real-time Chat (Preview) #3 - Firestore Setup
Net Ninja
Vue & Firebase Real-time Chat (Preview) #4 - Welcome Screen
Net Ninja
Vue & Firebase Real-time Chat (Preview) #5 - Props in Routes
Net Ninja
Vue & Firebase Real-time Chat (Preview) #6 - Route Guards
Net Ninja
Vue & Firebase Real-time Chat (Preview) #7 - Chat Window
Net Ninja
Vue & Firebase Real-time Chat (Preview) #8 - New Message Component
Net Ninja
Object Oriented JavaScript Tutorial #1 - Introduction
Net Ninja
Object Oriented JavaScript Tutorial #2 - Object Literals
Net Ninja
Object Oriented JavaScript Tutorial #3 - Updating Properties
Net Ninja
Object Oriented JavaScript Tutorial #4 - Classes
Net Ninja
Object Oriented JavaScript Tutorial #5 - Class Constructors
Net Ninja
Object Oriented JavaScript Tutorial #6 - Class Methods
Net Ninja
Object Oriented JavaScript Tutorial #7 - Method Chaining
Net Ninja
Object Oriented JavaScript Tutorial #8 - Class Inheritance
Net Ninja
Object Oriented JavaScript Tutorial #9 - Constructors (under the hood)
Net Ninja
Object Oriented JavaScript Tutorial #10 - Prototype
Net Ninja
Object Oriented JavaScript Tutorial #11 - Prototype Inheritance
Net Ninja
More on: Backend Performance
View skill →
🎓
Tutor Explanation
DeepCamp AI