What PHP is
PHP is a server-side scripting language made for producing web pages: a PHP file can mix ordinary HTML with blocks of code, and the server runs that code each time someone requests the page. Variable names start with a dollar sign, every statement ends with a semicolon, and the language is relaxed about types until you ask it to be strict. Modern PHP (version 8 and later) is a much tidier language than older tutorials suggest, with classes, type declarations, enums and a package manager, but the everyday feel is still direct: fetch some data, shape it, send back a page.
Where PHP is used
- Content management and online shops
- Several long-established content management systems and e-commerce platforms are written in PHP, so a great deal of day-to-day PHP work is building themes, plugins and extensions for sites that already exist.
- Web application backends
- Frameworks such as Laravel and Symfony provide routing, database access, forms and authentication for larger applications, while plain PHP is still enough for a small site.
- APIs for front ends and mobile apps
- A PHP backend often returns JSON to a JavaScript front end or a phone app, using the same request-and-response model as an ordinary page.
- Small business and hobby sites
- Inexpensive shared hosting usually comes with PHP already set up, which is why a brochure site, a booking form or a club website is so often written in it.
- Command-line scripts and internal tools
- The same interpreter runs from a terminal, so teams that use PHP on the web tend to write their scheduled jobs, data imports and maintenance scripts in it too.
Your first PHP program
Saved as hello.php. You can paste it straight into the playground to see it run.
<?php
$name = "Priya";
$year = 2026;
$items = ["tea", "bread", "rice"];
echo "Hello, " . $name . ".\n";
echo "In five years it will be " . ($year + 5) . ".\n";
echo "You have " . count($items) . " items on your list.\n";What it prints
Hello, Priya.
In five years it will be 2031.
You have 3 items on your list.- Line 1,
<?php, tells the interpreter that code follows. Without it PHP treats the whole file as plain text and sends it to the screen unchanged. - Lines 2 to 4 create three variables. Every variable name begins with
$, and each statement ends with a semicolon. The square brackets on line 4 make an array, an ordered list of values. - Line 6 prints text with
echo. The dot joins pieces of text end to end, and"\n"at the end is a newline; PHP does not add one for you. - Line 7 does arithmetic in the middle of the sentence. The round brackets around
$year + 5make sure the addition happens before the result is joined to the text. - Line 8 calls
count(), one of PHP's built-in functions, to find out how many items the array holds.
Run PHP on your own computer
PHP was built for web servers, but the same interpreter runs from a terminal, and that is the simplest way to learn it: no web server and no browser, just a file and a command. A page in a browser comes later, and PHP ships with a small development server for exactly that.
Install PHP
On Windows, download the current release from php.net, unzip it to a folder such as C:\php and add that folder to your PATH. On macOS and Linux, install the php package with your package manager, or download a release from php.net. Any 8.x version is suitable; php.net lists which versions are still supported.
Check it works
Open a terminal and ask for the version. If the command is not found, the folder containing php has not been added to your PATH yet.
Shellphp --versionRun a file
Save the first program above as hello.php and run it from the folder it is in. Whatever the script echoes appears in the terminal.
Shellphp hello.phpSee a page in a browser
When you want HTML rather than terminal output, start PHP's built-in development server in a folder that contains an index.php, then open http://localhost:8000. It is meant for learning and development only, not for hosting a real site.
Shellphp -S localhost:8000Use an editor that understands PHP
Any text editor works. One with PHP support will underline a missing semicolon or an undefined variable as you type, which saves a lot of round trips to the terminal.
A learning order for PHP
Stages, not a timetable. Each one exists because the next would not make sense without it, and how long each takes depends on how much you write.
Stage 1. Output, variables and types
- <?php tags and echo
- variables and the $ sign
- strings and the . operator
- integers, floats and arithmetic
- booleans and null
PHP's basic values and the habit of ending every statement with a semicolon are the foundation of everything else, and getting comfortable with echo from the terminal keeps the first steps free of web complications.
Stage 2. Conditions and loops
- if / elseif / else
- comparison and === versus ==
- while and for
- foreach
- match and switch
Choosing and repeating is what makes a script do work. PHP has two ways to compare values, and learning the strict one early avoids a family of bugs that are hard to see later.
Stage 3. Arrays and functions
- indexed arrays
- associative arrays (keys and values)
- foreach over key => value
- built-in array functions
- writing your own functions
- parameter and return types
The array is PHP's one general-purpose container: it is a list, a lookup table and a record all at once. Functions with type declarations are how you keep a growing script readable.
Stage 4. PHP on the web
- how a request becomes a response
- $_GET and $_POST
- HTML forms
- escaping output with htmlspecialchars
- sessions and cookies
This is what PHP is for, and it is where the language's habits matter most: reading user input, keeping state between pages, and never printing user-supplied text without escaping it.
Stage 5. Classes, Composer and databases
- classes, properties and methods
- constructors and visibility
- namespaces and autoloading
- installing packages with Composer
- PDO and prepared statements
Real applications are organised into classes and lean on libraries, and Composer is how PHP code is shared. Prepared statements are the only safe way to put user input into a database query.
Stage 6. A framework and a real project
- Laravel or Symfony basics
- routing, controllers and templates
- environment configuration
- testing with PHPUnit
- deploying to a host
A framework packages the decisions the previous stages taught you to make by hand. Once you can build, test and deploy a small application, you can learn the rest from the work itself.
Mistakes beginners make in PHP
- Leaving out the <?php tag
- PHP only runs what is inside <?php ... ?> blocks; everything else is sent to the output as-is. A file without the opening tag shows its own source code in the browser or terminal, with no error at all, which is baffling the first time it happens.
- Joining text with + instead of .
- In PHP the dot joins strings and + is only for numbers. "Hello, " + $name stops with a TypeError in PHP 8 (Unsupported operand types: string + string). Write "Hello, " . $name, or put the variable inside a double-quoted string: "Hello, $name".
- Comparing with == when === was meant
- Two equals signs convert the values before comparing, so "1" == "01" is true and "0" == false is true. Three equals signs compare value and type without converting, which is almost always what you mean. Use === and !== by default.
- Misspelling a variable name and not noticing
- PHP does not require variables to be declared, so $nmae is simply a new, empty variable. PHP 8 prints Warning: Undefined variable $nmae and carries on with null, so the script keeps running and produces wrong output rather than stopping. Treat warnings as errors while you learn.
- Forgetting a semicolon
- The parser only notices on the next statement, so the message (syntax error, unexpected token "echo") points one line below the real problem. When a parse error makes no sense on the line it names, look at the line above.
Strengths and trade-offs
Where it is strong
- Built for the web from the start: reading a form, setting a cookie, sending a header and returning HTML are one-liners rather than library calls.
- Runs on nearly every web host, so a beginner's first site can go live cheaply without configuring a server.
- A gentle path from a single file to a large application: you can begin with HTML plus a few lines of code and add structure only when you need it.
- Modern PHP has caught up with other languages: typed properties, enums, match expressions, readonly properties and a mature package ecosystem through Composer.
Where it is not
- A lot of the PHP on the internet is old. Tutorials and forum answers often show practices that are now discouraged or removed, and a beginner cannot always tell which is which.
- The standard library is inconsistent: function names and argument orders vary (strlen, str_replace, array_key_exists), so you look things up more often than you would like.
- It is a web language. For mobile apps, desktop software, games or data science, PHP is rarely the right tool and its ecosystem there is thin.
- Loose typing and silent conversions can hide bugs until they reach a user. Type declarations and declare(strict_types=1) fix this, but you have to opt in.
Who PHP is for
PHP is a good choice if what you want to build is websites and web applications, particularly if you will work on existing sites, plugins and themes, or want the cheapest route from a first script to a page that is live on the internet. If you already know another language and need to maintain a PHP site, the syntax will feel familiar and most of the learning is in the web parts. Choose something else first if your interest is data analysis, machine learning, mobile apps or games; PHP does not reach those, and its web-only shape would get in your way.
Questions about learning PHP
- Do I need to know HTML before learning PHP?
- You can learn the language itself from the terminal without any HTML, and the first program on this page does exactly that. But the point of PHP is producing web pages, so as soon as you build something real you will be writing HTML, and probably some CSS, around your code. Learning a little HTML alongside PHP is the natural order; you do not need to master it first.
- Do I need a web server to learn PHP?
- No. The php command runs a file directly, and PHP includes a development web server (php -S) for when you want to see a page in a browser. A production web server only matters when you host a site for other people, and most hosting plans have one set up already.
- Which version of PHP should I learn?
- The current 8.x release. Each version has a published support window on php.net, and older lines stop receiving security fixes. Code written for PHP 5 or early 7 will often still run, but tutorials from that era teach habits, such as the removed mysql_ functions and unescaped output, that you should not pick up.
- Is PHP still worth learning?
- It depends on what you want to do. Many existing websites, content systems and online shops are written in PHP and are actively maintained and extended, and new applications are still started in it with modern frameworks. If your goal is web work, especially freelance or agency work on existing sites, it remains a practical choice. If your goal is data, AI or mobile, pick a language built for those instead.
The primary source
When this guide and the official documentation disagree, the documentation is right and we would like to hear about it.