PHP Installation and Setup
Before writing PHP you need PHP installation on your computer, or access to a server that runs PHP. Fortunately, setting up PHP locally takes only a few minutes, and PHP even ships with its own development web server.
Installing PHP
- Windows: download a ZIP from windows.php.net or install a bundle like XAMPP or Laragon, then add the PHP folder to your PATH
- macOS: install Homebrew, then run brew install php
- Ubuntu/Debian: sudo apt install php-cli php-mbstring php-xml php-mysql
- Any OS: Laravel Herd gives you PHP, Composer and a local server in one installer
Check the PHP Version
Open a terminal and ask PHP for its version. If you see a version number, PHP is installed correctly.
php -vPHP 8.3.12 (cli) (built: Sep 24 2024 18:08:04) (NTS) Copyright (c) The PHP Group Zend Engine v4.3.12, Copyright (c) Zend Technologies
Run a PHP File From the Command Line
Create a file named hello.php and run it with the php command. This is the fastest way to practise the examples in this tutorial.
<?php
// hello.php
echo "PHP is working!";$ php hello.php PHP is working!
Use the PHP Built-in Web Server
To see PHP pages in a browser, start the built-in server in your project folder. Then open http://localhost:8000 and PHP will serve index.php automatically. It is meant for development only, not production.
php -S localhost:8000[Fri Oct 31 10:00:00 2026] PHP 8.3.12 Development Server (http://localhost:8000) started
Install Composer
Composer is the package manager for PHP. You will need it for libraries and for Laravel. Install it from getcomposer.org and check it with composer --version. An editor such as VS Code with the PHP Intelephense extension makes writing PHP much nicer.
Frequently asked questions
How do I run a PHP file?
Run php filename.php in a terminal, or start the built-in server with php -S localhost:8000 and open the file in your browser.
Do I need XAMPP to run PHP?
No. XAMPP is convenient because it bundles Apache and MySQL, but PHP on its own plus the built-in server is enough for learning.
Why does my browser show PHP code instead of running it?
You probably opened the file directly from disk. PHP must be run through a web server, so open it via http://localhost instead of file://.