LEARN TO CODE | PHP | 01 SERVER-SIDE FOUNDATIONS
01.1 Running PHP on a local development server
Set up a local PHP workflow and explain how PHP runs on the server to generate a response for the browser.
01 | WHAT DOES SERVER-SIDE MEAN?
An HTML file describes a page the browser can display. PHP can generate that HTML before it reaches the browser. It runs on the server side : the computer handling the request.
During local development, your own computer plays both roles. The browser is the client; a PHP development-server process is the server. They communicate even though they are on the same machine.
You need only basic HTML to begin. No database, hosting account or framework is required for these first lessons.
02 | FOLLOW ONE REQUEST
1 | REQUEST
The browser asks for index.php at a local HTTP address.
2 | EXECUTE
The server runs PHP and builds the response.
3 | DISPLAY
The browser receives HTML and displays the page.
With a correctly configured PHP server, the response contains PHP’s output rather than the PHP source. View Page Source in the browser to inspect that response.
JavaScript can run in the browser after a response arrives. PHP does not continuously watch a user’s clicks; another request is needed for server code to run again.
03 | INSTALL THE PHP RUNTIME
VS Code edits code; it does not install the PHP runtime for you. Use a current supported PHP 8 release from the official downloads and installation instructions or your school’s prepared installation.
Windows: follow the official Windows instructions for a compatible x64 command-line build, including its required runtime. Extract PHP to a folder such as C:\php and add that folder to your user Path if you want the php command to work everywhere. Reopen VS Code after changing Path.
macOS/Linux: use the installation route appropriate to your system or a teacher-managed package. Do not assume PHP is already installed. See the official installation guide for platform details.
You do not need to install Apache or MySQL for this section. PHP’s built-in development server is enough.
04 | CREATE THE PROJECT FOLDER
In VS Code, choose File → Open Folder, create a folder called
myWebsite
and open it. In the Explorer, click New File and name it
index.php
.
Open Terminal → New Terminal. Check that its working folder is myWebsite, then type:
php -v
You should see the installed PHP version. If php is not recognised, check installation and Path. In Windows PowerShell you can instead use the executable’s full path, for example:
& "C:\php\php.exe" -v
That path is an example; use the actual folder containing your php.exe.
05 | WRITE YOUR FIRST PAGE
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>My first PHP page</title>
</head>
<body>
<h1>Welcome to my website</h1>
<p><?php echo "PHP created this sentence."; ?></p>
</body>
</html>
Save index.php. Most of this is ordinary HTML. The opening <?php starts a PHP region and ?> returns to HTML. Inside it, echo sends text into the response; the semicolon ends the statement.
The PHP output appears inside the existing paragraph element. The browser should display the heading and “PHP created this sentence.”
06 | START THE SERVER ONCE
php -S 127.0.0.1:8000
Run this in the myWebsite terminal. Uppercase -S starts PHP’s built-in server. The terminal stays occupied because the server keeps running and listening for requests.
If using a full Windows PowerShell path, run
& "C:\php\php.exe" -S 127.0.0.1:8000
. Keep that terminal open.
This is a local development server, not the server to expose as a public website. Binding to 127.0.0.1 keeps this lesson’s server on the local machine.
07 | OPEN THE LOCAL ADDRESS
Hover over the terminal’s address and use Follow Link where VS Code offers it, or open your browser and enter
http://127.0.0.1:8000/index.php
. The root address
http://127.0.0.1:8000/
also finds index.php in this setup.
127.0.0.1 means this computer; 8000 is the port at which this server listens. index.php identifies the requested file relative to the project folder.
Do not double-click index.php to open a file:// address. An ordinary static Live Server extension also does not execute PHP. The workflow is save → request through the PHP server → view response.
08 | SAVE, REFRESH AND INSPECT
Change the echoed sentence, save and refresh the browser. You do not need to restart the server after every ordinary PHP edit.
Use View Page Source to find the generated sentence. You should see the response HTML, not the echo instruction. An unchanged response often means an unsaved file, wrong URL or a different project folder.
Refresh creates a new request and runs the page again. Plain variables in one request do not automatically remember values for the next request; sessions and storage come later.
TRY IT YOURSELF
Change the heading to My PHP Lab and make PHP output Welcome, Maya! inside the paragraph.
Reveal guidance
Edit the HTML heading and the string supplied to echo, save, then refresh. Confirm both changes using the same HTTP address.
09 | TROUBLESHOOT THE WORKFLOW
| Symptom | Check |
|---|---|
| php is not recognised | Runtime installed; correct Path or full executable path |
| Address already in use | Stop your earlier server or use a different port, such as 8001 |
| Browser cannot connect | Server still running; address and port match |
| Unexpected page | Terminal served from the intended project folder |
| PHP source displayed or downloaded | Correct PHP-capable server, .php extension and HTTP URL |
| Syntax error | Read the reported file and line; check quotes, braces and semicolons |
To stop the server, click its terminal and press Ctrl+C. Start it again with the same command when needed.
10 | ADD A SECOND PAGE
TRY IT YOURSELF
Create about.php with an HTML heading and a different PHP-generated sentence. Add an ordinary HTML link to it from index.php.
Reveal guidance
Use <a href="about.php">About</a>. The browser requests the second file from the same local server. Do not put an absolute filesystem path in the link.
Explain which part runs in the browser, which part runs on the server and what the browser receives. Next we will generate output from variables rather than only fixed text.
KEEP LEARNING
Previous page 01.2 Variables, types and output
References: PHP development server · Types · Arrays · HTML text escaping