Introduction to PHP
PHP is a server-side scripting language that is widely used for web development. It stands for Hypertext Preprocessor and is an open-source programming language
PHP (a recursive acronym for PHP: Hypertext Preprocessor) is an open-source, server-side scripting language designed for web development. "Server-side" means the code runs on the web server before the page reaches the browser: the server executes your PHP, and the visitor only ever sees the resulting HTML — never the PHP source itself. This makes PHP a natural fit for building dynamic pages whose content changes based on the user, a database, or a form submission.
This chapter explains what PHP is, why it is so widely used, and how to write and run your first script. By the end you will be able to read a basic PHP file and understand how it produces output.
Why PHP is still everywhere
PHP powers a large share of the web — including WordPress, Drupal, Laravel applications, and much of the e-commerce ecosystem. Its staying power comes from a few practical strengths:
- Gentle learning curve. The syntax is forgiving and reads a lot like C and Perl, so beginners can produce working pages quickly.
- Open-source and free. There are no license fees, and an active community continuously ships new versions and security patches.
- Runs almost anywhere. PHP works on Windows, macOS, and Linux, alongside servers such as Apache and Nginx, and it ships with nearly every shared-hosting plan.
- First-class database support. PHP talks to MySQL, PostgreSQL, SQLite, and most other databases out of the box, which is why it dominates database-driven sites.
- Huge ecosystem. Frameworks, libraries (via Composer), tutorials, and answered questions are abundant, so help is rarely far away.
How PHP runs
A PHP file is a plain text file saved with a .php extension. When a browser requests that file, the web server hands it to the PHP engine, which executes the PHP and returns plain HTML. The flow looks like this:
Browser ──request──▶ Web server ──▶ PHP engine (runs the code)
Browser ◀──HTML──── Web server ◀── generated outputTo run PHP locally you need PHP installed and a web server. The simplest setup is a bundled stack like XAMPP or MAMP, which installs Apache, MySQL, and PHP together. PHP also ships with a built-in development server you can start with php -S localhost:8000. See the PHP installation guide for step-by-step instructions, then open this page in your browser at http://localhost:8000/.
Writing your first PHP file
PHP code lives inside <?php ... ?> tags. Anything between those tags is executed as PHP; anything outside them is sent to the browser unchanged as HTML. Create a file named hello.php:
Simple hello world with PHP echo
The echo statement outputs text. When the server runs this file, the browser receives just the words Hello World!. Statements in PHP end with a semicolon (;) — forgetting it is one of the most common beginner errors. For the full rules on tags, statements, and whitespace, see PHP syntax.
Storing data in variables
A variable is a named container for a value. In PHP, variable names always start with a dollar sign ($), and you assign a value with the = operator:
Store text in a variable and echo it
Here $text holds the string "Hello World!", and echo $text; prints it. The browser sees the same Hello World! as before — but now the value is reusable. Variables can hold strings, numbers, booleans, arrays, and more; PHP figures out the type for you. Learn more in PHP variables and PHP data types.
Tip: Use comments (
//for a single line,/* ... */for a block) to leave notes in your code. The PHP engine ignores them.
Conclusion
PHP is a powerful, approachable language for building dynamic websites. Its low barrier to entry, cross-platform reach, and tight database integration explain why it remains one of the most widely used languages on the web. Now that you can write and run a basic script, the next steps are to learn PHP syntax in depth and start working with variables.