What is PHP?
PHP (recursive acronym for PHP: Hypertext Preprocessor) is a widely-used, open-source server-side scripting language designed primarily for web development. Created by Rasmus Lerdorf in 1994, PHP has evolved from simple "Personal Home Page" scripts into a mature, general-purpose language powering over 77% of all websites that use a known server-side language.
WordPress, the platform behind 43% of all websites, is written entirely in PHP. Laravel, Symfony, and Drupal - some of the most popular web frameworks - are also PHP-based.
Unlike HTML (which is static), PHP generates HTML dynamically on the server before it reaches the browser. This means you can connect to databases, process form submissions, manage user sessions, and build full web applications - all with PHP.
How PHP Works
PHP follows a simple request-response cycle:
- A user visits
example.com/page.phpin their browser. - The browser sends an HTTP request to the web server (Apache/Nginx).
- The server recognizes
.phpfiles and passes them to the PHP engine. - PHP executes the script - reads databases, processes logic, builds HTML.
- The server sends the generated HTML response back to the browser.
- The browser renders the HTML. The user never sees PHP code.
PHP runs on the server before the page is sent. JavaScript runs in the browser after the page loads. Both are essential in modern web development - and frequently work together.
What Can PHP Do?
PHP can handle almost any server-side task:
- Dynamic websites - generate HTML based on user data or database content
- REST APIs - serve JSON to mobile apps and single-page applications
- Form processing - validate, sanitize, and store user input
- Authentication - login systems, sessions, password hashing
- File operations - upload, read, write, delete files on the server
- Email sending - via PHPMailer or built-in
mail() - Database CRUD - full MySQL/PostgreSQL/SQLite operations via PDO
- CLI scripting - cron jobs, data processing, automation scripts
PHP Versions
Always learn and use the latest stable PHP version:
| Version | Status | Key Additions |
|---|---|---|
PHP 8.3 | Current | Typed class constants, readonly improvements, json_validate() |
PHP 8.2 | Active | Readonly classes, null/false/true standalone types |
PHP 8.1 | Active | Enums, Fibers, readonly properties, intersection types |
PHP 8.0 | Security only | Named args, match, nullsafe operator, union types |
PHP 7.x | End of Life | Avoid for new projects |
Your First PHP Program
PHP code lives inside <?php ... ?> tags. You can mix PHP and HTML freely,
or write pure PHP files. Here is the classic Hello World:
<?php
echo "Hello, World!";
A slightly more practical example - showing the current date and server info:
<?php
$name = "RubanLearn";
$version = PHP_VERSION;
$today = date("Y-m-d");
echo "Welcome to $name!\n";
echo "PHP Version: $version\n";
echo "Today is: $today\n";
You can mix PHP inside HTML. Only the PHP parts execute on the server - the rest is sent as-is to the browser:
<!DOCTYPE html>
<html>
<body>
<h1><?php echo "Hello " . htmlspecialchars($name); ?></h1>
<p>Today: <?= date("D, d M Y") ?></p>
</body>
</html>PHP vs Other Server-Side Languages
| Language | Primary Use | Learning Curve | Performance |
|---|---|---|---|
| PHP | Web, APIs, CMS | Easy | Good (PHP-FPM) |
| Python | Web, AI/ML, Data | Easy | Moderate |
| Node.js | Web, APIs, Real-time | Moderate | Good (async) |
| Java | Enterprise, Android | Hard | Very Good |
| Go | APIs, Microservices | Moderate | Excellent |
PHP's biggest advantage for beginners: zero configuration to see results.
Save a .php file in your web server's folder, visit the URL - done.
Common Beginner Mistakes
- Forgetting the semicolon
;at the end of statements - Using
echooutside PHP tags without<?php - Mixing single-quoted strings (no variable interpolation) with double-quoted expectations
- Outputting user input directly without
htmlspecialchars()- XSS vulnerability - Not checking if a variable exists before using it (
isset()andempty()are your friends)
Next Steps
Now that you understand what PHP is and how it works, move on to: