PHP Zero to Advanced #1: Introduction & Setup
PHP powers a huge share of the web, from small personal blogs to large platforms such as WordPress, Wikipedia, and countless e-commerce sites. It is easy to start with, runs almost everywhere, and modern PHP (version 8 and later) is fast, typed, and pleasant to write. In this series we will go from zero to building a small REST API with authentication, tests, and secure database access.
In this first part you will learn what PHP is, install it, check your version, run PHP from the command line, start the built-in development server, and write your first scripts.
What Is PHP?
PHP (a recursive acronym for PHP: Hypertext Preprocessor) is a server-side scripting language. When a browser requests a .php page, the web server runs the PHP code and sends back the result, usually HTML or JSON. The browser never sees your PHP source code.
- Server-side: code runs on the server, not in the browser (unlike JavaScript in the browser).
- Interpreted: you do not compile anything manually; save the file and run it.
- Versatile: PHP can render web pages, build APIs, run CLI scripts, and process background jobs.
- Modern: PHP 8.x brings union types, enums, readonly properties, the
matchexpression, attributes, and a JIT compiler.
Throughout this series we target PHP 8.2 or newer.
Installing PHP
Pick the method that matches your operating system:
# macOS (Homebrew)
brew install php
# Ubuntu / Debian
sudo apt update
sudo apt install php-cli php-mbstring php-xml php-sqlite3
# Windows: download from windows.php.net or use Laragon / XAMPP
If you prefer an all-in-one bundle that includes a web server and database, tools like Laravel Herd, Laragon, or XAMPP work fine too. For this series, the PHP command-line interface (CLI) is all you need.
Once installed, verify the version:
php -v
Output:
PHP 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
with Zend OPcache v8.3.12, Copyright (c), by Zend Technologies
Your exact version and build date will differ. What matters is that the major version is 8 and the minor version is 2 or higher.
Your First Script
Create a file called hello.php. Every PHP file starts with the opening tag <?php. In files that contain only PHP, you should omit the closing tag ?> to avoid accidental whitespace being sent to the output.
<?php
echo "Hello, World!";
Output:
Hello, World!
Run it from your terminal with the PHP CLI:
php hello.php
Output:
Hello, World!
Congratulations, you just ran your first PHP program. Note the semicolon at the end of the statement: every PHP statement must end with ;.
The Built-in Development Server
PHP ships with a small web server for local development. You do not need Apache or Nginx to start learning. Inside a folder that contains your PHP files, run:
mkdir my-first-php && cd my-first-php
php -S localhost:8000
Output:
[Tue Oct 13 09:00:00 2026] PHP 8.3.12 Development Server (http://localhost:8000) started
Now create index.php in the same folder. PHP can be mixed with HTML: anything outside <?php ... ?> is sent to the browser as-is.
<!DOCTYPE html>
<html>
<head>
<title>My First PHP Page</title>
</head>
<body>
<h1>Welcome!</h1>
<p>Today is <?php echo date('Y-m-d'); ?></p>
<p>PHP version: <?= PHP_VERSION ?></p>
</body>
</html>
Open http://localhost:8000 in your browser and you will see the heading, today's date, and your PHP version. The short tag <?= ... ?> is a shortcut for <?php echo ... ?>. Stop the server with Ctrl+C.
The built-in server is for development only. In production, use a proper web server such as Nginx or Apache with PHP-FPM.
echo, print, and Comments
echo is the most common way to send output. print works similarly but accepts only one argument. Note the difference between double quotes and single quotes:
<?php
echo "Hello from echo\n";
print "Hello from print\n";
echo "One", " ", "Two", "\n"; // echo accepts multiple arguments
$name = "Budi";
echo "Hi, $name!\n"; // variables inside double quotes are interpolated
echo 'Hi, $name!' . "\n"; // single quotes are literal
Output:
Hello from echo
Hello from print
One Two
Hi, Budi!
Hi, $name!
The . operator concatenates (joins) strings, and "\n" is a newline character, which only works inside double quotes. We will cover strings in depth in the next part.
Comments help explain your code and are ignored when the script runs:
<?php
// A single-line comment
# Also a single-line comment
/*
* A multi-line comment.
* Useful for longer explanations.
*/
echo "Comments are ignored by PHP";
Output:
Comments are ignored by PHP
You can also inspect your environment with a few built-in constants and functions:
<?php
echo "PHP version: " . PHP_VERSION . "\n";
echo "Major version: " . PHP_MAJOR_VERSION . "\n";
echo "Max int: " . PHP_INT_MAX . "\n";
echo "Running from: " . php_sapi_name() . "\n";
Output:
PHP version: 8.3.12
Major version: 8
Max int: 9223372036854775807
Running from: cli
Common Mistakes
- Forgetting the semicolon. This is the classic beginner error:
<?php
echo "First line"
echo "Second line";
Output:
Parse error: syntax error, unexpected token "echo", expecting "," or ";" in /path/to/script.php on line 4
- Opening the file directly in the browser (
file:///.../index.php). The browser will show raw code or download it. PHP must be executed by a server, so usephp -S. - Using single quotes when you want interpolation.
'Hi, $name'prints the literal text$name. - Adding a closing
?>in pure PHP files. Trailing spaces after it can cause "headers already sent" errors later. - Running an old PHP version. Many examples in this series use PHP 8.2+ features. Always check
php -v.
Exercise
Create a file about.php that stores your name, city, and the year you started learning PHP in variables, then prints three sentences. Here is one possible solution:
<?php
$name = "Siti";
$city = "Bandung";
$year = 2026;
echo "My name is $name.\n";
echo "I live in $city.\n";
echo "I started learning PHP in $year.\n";
Output:
My name is Siti.
I live in Bandung.
I started learning PHP in 2026.
As a bonus, serve it with php -S localhost:8000 and open http://localhost:8000/about.php. Notice that the browser joins the lines together, because \n is not an HTML line break. Try replacing it with <br>.
Summary
- PHP is a server-side language that generates HTML, JSON, or any text output.
- Check your installation with
php -vand run scripts withphp file.php. - Use
php -S localhost:8000for a quick local development server. - PHP files start with
<?php, statements end with;, andechoprints output. - Double quotes interpolate variables, single quotes do not.
In Part 2 we will dive into variables, data types, operators, and working with strings.
Related articles
PostgreSQL Zero to Advanced #1: Introduction & Setup
Start the series by understanding what a relational database is, installing PostgreSQL locally, and getting comfortable with psql commands like \l, \c and \dt.
Ruby on Rails Zero to Advanced #1: Ruby Basics
Start the Rails journey with the language underneath it: irb, variables, strings, puts vs p, and symbols explained with runnable examples.
Python Zero to Advanced #1: Introduction & Setup
Start your Python journey: what Python is, how to install it, how to use the REPL, print your first output, and run .py files from the terminal.