2. PHP Setup
What you will learn
- What you need to run PHP on your own computer
- How to set up a practice folder with LocalWP
- How to create, run, and troubleshoot a PHP file
- Why you should never practise on a live website
What you need
PHP needs a web server with PHP installed. You do not have to set this up by hand. The easiest route for a beginner on Windows or Mac is LocalWP, a free tool that creates a ready-made local site in a few clicks. Other all-in-one packages, such as XAMPP, work too, and PHP also has a built-in test server that you can start from the command line.
You also need a code editor. Visual Studio Code is free and popular.
Practise only on your own computer. Never test code on a live website. A mistake could break the site, and a careless script can expose private data.
Method 1: LocalWP (recommended)
- Install LocalWP from its official website and open it.
- Click Create a new site and name it, for example
php-practice. Choose the preferred setup, and let it finish. - Right-click the site and choose Go to site folder. Open
app, thenpublic. - Create a new folder here called
practice. - Inside
practice, create a file namedhello.php. - Make sure the site shows Running.
- In your browser, open the site address with
/practice/hello.phpon the end. If your site address ishttp://php-practice.local, openhttp://php-practice.local/practice/hello.php.
PHP code: practice/hello.php
<?php
echo "Hello from PHP!";
What you should see in the browser
Hello from PHP!
If you see that sentence, PHP works. Edit the file, save it, and refresh the browser to see changes.
Method 2: PHP’s built-in server
If PHP is already installed on your computer, you can start a tiny test server from a terminal opened in your project folder:
Terminal command
php -S localhost:8000
Then open http://localhost:8000/hello.php. Press Ctrl+C in the terminal to stop it. This server is only for learning and testing, never for a live site.
Check your PHP version
Create version.php and open it the same way:
PHP code: practice/version.php
<?php
echo "PHP version: " . PHP_VERSION;
What you should see (your number will differ)
PHP version: 8.x.x
This course uses modern PHP (version 8). If yours is much older, change the version in LocalWP’s site settings, or install a current one. Delete version.php when you finish. Pages that show server details can help attackers if left on a public site.
Showing errors while you learn
On a local practice site, you want to see mistakes. Add these two lines at the very top of a practice file if a page is blank:
PHP code
<?php
ini_set("display_errors", "1");
error_reporting(E_ALL);
Never leave error display switched on for a live website. Error messages can reveal file paths and other private details. On live sites, log errors to a file instead.
Where your files go
| Problem | Likely cause |
|---|---|
| The browser downloads the file, or shows the raw code | The page was opened as a file, or the server does not run PHP. Use the site address, not a folder path |
| 404 Not Found | Wrong folder or file name. Check the address matches the path inside public |
| A blank page | A PHP error with display switched off. Turn on error display (above) |
| The site will not start | Another program is using the same port. Close it, or restart LocalWP |
Good habits from day one
- Keep every exercise in its own file, with a clear name such as
variables.php. - Save the file, then refresh, after every change.
- Use lowercase file names without spaces.
- Keep a backup, or use Git, once your projects grow.
Common mistakes
- Double-clicking a
.phpfile to open it in the browser. - Saving files as
hello.php.txtbecause Windows hides file extensions. Turn on File name extensions in File Explorer. - Putting practice files outside the
publicfolder, so the server cannot find them. - Leaving the site stopped in LocalWP.
- Practising on a live website.
Exercise
Create a file called about-me.php in your practice folder that outputs one sentence about you. Open it through your local site address and check that the sentence appears.
Show solution
<?php
echo "My name is Sam and I am learning PHP.";
Open http://your-site.local/practice/about-me.php (use your own site address). You should see the sentence on the page.
Knowledge check
1. Why can’t you run a PHP file by double-clicking it?
PHP needs a web server to process it. Without one you only see the raw code.
2. Which free tool can create a local PHP environment for you?
LocalWP (or XAMPP, or PHP’s built-in server).
3. Why turn off error display on a live site?
Error messages can reveal private details such as file paths.
Summary
- PHP needs a server. LocalWP gives you one in minutes.
- Put practice files in the site’s
publicfolder and open them through the site address. - Show errors while learning, hide them on live sites, and never practise on a live website.