Example 1 — Hello, World!
<?php
echo "Hello, World!";
?>
This page is a self-contained introduction to PHP—a server-side language for dynamic websites. You will understand what PHP does, how it runs on a web server, and write your first programs that output HTML.
Language overview.
Rasmus Lerdorf.
Server runtime.
First program.
Free to use.
Hands-on code.
PHP (Hypertext Preprocessor) is a server-side programming language widely used to develop dynamic websites and web applications. Unlike JavaScript running in the browser, PHP executes on the web server and sends the finished HTML to the visitor.
PHP handles form submissions, user login, database queries, file uploads, session management, and API backends. It powers popular CMS platforms like WordPress, frameworks like Laravel and Symfony, and countless custom business sites.
PHP files usually end in .php. The server processes PHP tags and replaces them with output before the browser ever sees your source code.
PHP was created by Rasmus Lerdorf in 1994 as a set of Common Gateway Interface (CGI) scripts for tracking visits to his online resume. It evolved into a full language and is now maintained by The PHP Group and a large open-source community.
| Event | Year | Highlight |
|---|---|---|
| PHP created | 1994 | Rasmus Lerdorf personal home page tools |
| PHP/FI 2.0 | 1997 | Form interpreter release |
| PHP 3 | 1998 | Renamed Hypertext Preprocessor |
| PHP 5 | 2004 | Improved OOP support |
| PHP 7 | 2015 | Major speed and memory improvements |
| PHP 8+ | 2020+ | JIT, attributes, named arguments, union types |
PHP code is executed on the server before the web page is sent to the client’s browser. That allows dynamic content based on user input, database results, the time of day, or any logic you write in PHP.
index.php)Visitors never see your raw PHP source—only the generated result.
PHP can be embedded directly into HTML, which makes it a natural fit for web development. Use any text editor or IDE (VS Code, PhpStorm, Notepad++), save files with a .php extension, and place them in your web server’s document root (e.g. htdocs in XAMPP).
<!DOCTYPE html>
<html lang="en">
<head>
<title>My PHP Page</title>
</head>
<body>
<h1>Today is <?php echo date('Y-m-d'); ?></h1>
</body>
</html>Anything outside <?php ?> tags is sent to the browser as normal HTML.
PHP is considered relatively easy to learn, especially for beginners new to programming. Its syntax resembles C, Java, and JavaScript, and the language is designed to be practical for web tasks like printing output and reading form data.
PHP has a large, active community with tutorials, forums, and packages on Packagist. The official PHP documentation is comprehensive and beginner-friendly.
As with any language, difficulty depends on your prior experience and project complexity. Start with variables, echo, and simple forms before tackling databases and frameworks.
Yes. PHP is an open-source language released under the PHP License. Developers can use it freely in personal and commercial projects and contribute to its development on GitHub.
The runtime, standard library, and documentation are maintained collaboratively. Hosting providers worldwide ship PHP preinstalled, so deploying PHP apps is straightforward and inexpensive.
Pick one of these beginner-friendly setups:
brew install phpphp -v
cd my-project
php -S localhost:8000Open http://localhost:8000/demo.php in your browser after starting the built-in server.
Here is a simple PHP program that prints Hello, World! to the browser:
<?php
echo "Hello, World!";
?>The echo statement outputs text. Wrap PHP in <?php and ?> tags inside .php files. Learn more in the dedicated PHP echo tutorial.
PHP runs behind many global websites and platforms. Here are seven well-known examples:
Millions of smaller sites, agencies, and hosting plans still rely on PHP for its simplicity and ecosystem.
| Concept | What it does |
|---|---|
| Variables | $name = "Alex"; store data |
| echo / print | Output text and HTML to the browser |
| Superglobals | $_GET, $_POST, $_SESSION for web input |
| Control flow | if, for, foreach, while |
| Functions | Reusable blocks: function greet() { } |
| Arrays | Lists and associative maps of data |
| Include files | require / include for shared layout code |
| Task | PHP code |
|---|---|
| Open PHP block | <?php |
| Print text | echo "Hello"; |
| Variable | $count = 10; |
| Read GET param | $_GET['name'] ?? '' |
| String concat | $full = $first . ' ' . $last; |
| Comment | // line or /* block */ |
Five starter programs. Save each as a .php file and open it through your local server or php -S localhost:8000.
<?php
echo "Hello, World!";
?><?php
$name = "Alex";
$age = 20;
echo "Name: $name\n";
echo "Age: " . $age;
?>Variables start with $. Double-quoted strings expand variables; use . to concatenate.
<!DOCTYPE html>
<html lang="en">
<body>
<h1><?php echo "Welcome to PHP!"; ?></h1>
<p>Server time: <?php echo date("H:i:s"); ?></p>
</body>
</html><?php
$n = 7;
if ($n % 2 === 0) {
echo "$n is even";
} else {
echo "$n is odd";
}
?><?php
function add(int $a, int $b): int {
return $a + $b;
}
echo "Sum = " . add(3, 5);
?>Modern PHP supports type hints (int) and return types for clearer, safer code.
The user visits https://example.com/page.php or submits a form.
Apache or Nginx passes the file to the PHP engine with request data in superglobals.
Your script queries databases, reads files, and echoes HTML fragments.
The browser renders the final page; PHP source stays on the server.
.php files using <?php ?> tags and echo.$_GET and $_POSTphpinfo() or debug details on public serverspassword_hash()PHP originally stood for Personal Home Page, but it was later renamed to Hypertext Preprocessor to reflect its broader role as a general-purpose web language. It is one of the few recursive acronyms in computing—the “P” in PHP stands for PHP itself.
Install PHP or XAMPP, save the Hello World example, and open it in your browser.
10 people found this page helpful