PHP Basics

Starting a PHP Development Server

Maximiliano Firtman

Maximiliano Firtman

Independent Consultant
PHP Basics

Check out a free preview of the full PHP Basics course

The "Starting a PHP Development Server" Lesson is part of the full, PHP Basics course featured in this preview video. Here's what you'd learn in this lesson:

Maximiliano starts a development server from the terminal and specifies the host and port. The development server will serve static files like any other web server. PHP files are treated like HTML files, so any HTML content is rendered in the browser. Because of the ".php" extension, PHP code will be rendered on the server, and only the output will be returned to the browser.

Preview
Close
Get $100 Off
Get $100 Off!

Transcript from the "Starting a PHP Development Server" Lesson

[00:00:00]
>> Maximiliano Firtman: So it's time to talk about the web. And for the web, as we mentioned before, we need a server, okay? So php is not a server. Php shows this. We run a script and then we get an output, that's a php script. There is an exception to that is that php contains a server for development purposes only.

[00:00:25]
If you use php, if you say in the terminal php-S, the capital is important because it's not working, S, this from Serve, and then an IP address or a host, such as localhost: and a port number, any valid port number, will work, 4000, whatever, okay? It will create a server.

[00:00:52]
It's a PHP development server. It's not for production. In production, you should use Apache or other server, okay? So this is for us, for start playing with PHP without actually setting up another server and all that stuff. Now we can open the browser at localhost:4000 and see what happens.

[00:01:13]
If I do that, you can control click or command click from the terminal, this is what we found. Not found, by the way, this is a PHP color. That blue or purple, something like that one. So that's be a PHP server, okay? So it means that that's a PHP server responding that we don't have a default file in our project.

[00:01:39]
That means because it's expecting an index.php. So if I create a new file, index.php, and in HTML, let's start with HTML first. I'm going to create index.html. I'm not sure if you have used this, but you can use Emmet in VS Code. I'm not sure if you know what Emmet is, but you use exclamation mark tab and it writes for you the basic HTML template quickly.

[00:02:12]
Again, in case you haven't done that before, exclamation mark tab. For that to work, you need to be in an HTML so you should already save the file, okay? So I can just say ruphb something like that just to add something on the HTML. If I refresh now I'm seeing my HTML, okay?

[00:02:36]
So this server is also serving static files as well, okay? Make sense? So now I'm going to change the extension of this file. Instead of HTML, I will change the extension to PHP, index.php, okay? So if I keep it like this and refresh, I'm still seeing the same thing.

[00:02:58]
So that means that PHP by default, it's sending HTML. By default, it's sending HTML. What's the difference between bin.html and.php that now in the middle of my HTML, I can open a PHP tag. And I can do echo. For example, let's say, will this work?
>> Maximiliano Firtman: So if I write some PHP code now that echo will not go to the console, to the terminal in my operating system, it will go to the client.

[00:03:40]
Who is the client? The browser. And what we are doing here is what is known as server-side rendering. So if I look at my page source, I don't see PHP code in the browser. I just see the output that the final generated code, this is still server-side generated code, okay?

[00:04:01]
There is no client-side, everything is server-side. Make sense?
>> Maximiliano Firtman: So in the modern web development world, this is similar to some template engines that you have with Express.js or Django. We have templating, the PHP is templating by default. So by default, the servers in PHP are always sending HTML.

[00:04:28]
We will see later how to change that. So now we are on the web. So now we can start playing with the web, okay? That was our first idea. And okay, so something that I wanna show you is that if I inspect this, and I go to the network tab,
>> Maximiliano Firtman: We can see this is actually an HTTP request, a normal HTTP request, trying to see if I can, maybe not, I'm trying to make this part bigger, but yeah, so it has a lot of headers.

[00:05:07]
So PHP is not just sending this, it's sending headers as well. Lot of information, receiving and sending headers. And again something similar I mentioned before. So let's say that I'm going to change this right, so this is not my final code. If I have a new file and then say about PHP, and this about PHP will be another HTML that will contain an about section that will be playing something.

[00:05:42]
So actually, how can I get to that file? Probably you already guessed that we need to go to the URL, about.php. And I'm getting that file. So PHP works per request. Each file respond to each URL, that's how that works. So we have one file, one request. Maybe you're thinking, well, but I don't like that, I want a router, blah, blah, blah.

[00:06:12]
That's all PHP's responsibility, to be honest. It's a server responsibility, and sometimes you can mix a server with a framework. I mean, Laravel, CodeIgniter, they might be doing things differently on top of PHP, but PHP by itself, it's one URL, one script. That's all. So I know that these days, reading a .php sounds like weird, right?

[00:06:39]
In a URL. Means are still there. I mean, so, that's why, probably that was 2006 or something like that. The new the new pattern up here, known as friendly URLs, to remove the .PHP from the URL. But that's not a PHP problem, that's a server problem. So you need to go and set up your server.

[00:07:02]
So for example, when a request is coming, it will add the .php or it will forward internally that to a different file or things like that, the file in there. From a PHP point of view, it should be a .php file. So you can map that in your web server to a different URL later.

[00:07:24]
But by default, it's gonna be the name of your PHP file.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now