PHP Tutorial · PHP Basics · Lesson 2 of 38
PHP Get Started
Install PHP, run scripts from the terminal and start a local web server.
Install PHP
- Windows: download from windows.php.net or install a bundle like XAMPP or Laragon.
- macOS:
brew install php - Linux (Debian/Ubuntu):
sudo apt install php-cli
Check the installation:
php --version
Run a script
Save code in hello.php and run it from the terminal:
<?php
echo "PHP version: " . (PHP_MAJOR_VERSION >= 8 ? "8 or newer" : "old") . "\n";
echo "Hello from hello.php!\n";
Output
PHP version: 8 or newer Hello from hello.php!
php hello.php
The built-in web server
PHP ships with a development server. In your project folder run:
php -S localhost:8000
Then open http://localhost:8000/hello.php in your browser. For production you'd use Nginx or Apache with PHP-FPM.
PHP inside HTML
PHP can be mixed with HTML — everything outside <?php ... ?> is sent as-is:
<!DOCTYPE html>
<html>
<body>
<h1>Today is <?= date("l") ?></h1>
</body>
</html>
<?= ... ?> is short for <?php echo ... ?>.
Test yourself
Which command starts PHP's built-in development server?
php servephp -S localhost:8000php --serverphp run 8000
Show answer
Answer: php -S localhost:8000