PHP Installation
To run PHP on your own computer you need three things: a web server (like Apache), PHP and a database (like MySQL). Instead of installing them one by one, you can use a ready package. The most popular package for beginners is XAMPP. It is free and works on Windows, macOS and Linux.
Install PHP using XAMPP
- Open apachefriends.org and download XAMPP for your operating system. Choose the version with a current PHP 8 release.
- Run the installer and click Next. On Windows, if a security dialog appears, click Yes. Avoid the C drive path with spaces if you can, a simple folder like
C:\xampporD:\xamppis fine. - Keep Apache, MySQL and PHP selected in the components list, and finish the installation.
- Open the XAMPP Control Panel and click Start next to Apache and next to MySQL. They will show a green background when they are running.
- Open your browser and go to
http://localhost. If you see the XAMPP welcome page, the server is working.
Run your first PHP file
XAMPP serves files from its htdocs folder (for example C:\xampp\htdocs). Create a new folder inside it named mysite, and save a file hello.php in it with this code.
<?php
echo "Hello from PHP!";
?>
Now open http://localhost/mysite/hello.php in the browser.
Output:
Hello from PHP!
Do not open the file by double click, because then PHP is not used and the browser shows the code. A PHP file must always be opened with the http://localhost address.
Check the PHP version
<?php
echo "PHP version: " . PHP_VERSION;
?>
Output:
PHP version: 8.2.12
You can also see all settings with the phpinfo() function. Remove such a test file from a live server, because it shows private information.
Other ways to run PHP
| Option | Details |
|---|---|
| Built-in server | If PHP is installed, run php -S localhost:8000 inside your project folder. No Apache needed. See how to run PHP from the command line |
| Laragon, WAMP | Easy packages for Windows |
| MAMP | A similar package for macOS |
| Docker | Good if you want the same setup as your live server |
You also need a good code editor. Visual Studio Code is free and works very well for PHP.
Next chapter: PHP Syntax.
| Exercises & Assignments |
|---|
| Write a program to print “Hello World” using echo |
| Simple Tips for PHP Beginners |
| Experiment with Basic PHP Syntax Errors |
| Using phpinfo() – Display PHP Configuration & Modules |