Introduction to PHP
PHPPHP is one of the most widely used programming languages on the web. It powers over 75% of all websites — including WordPress, Facebook (originally), and Wikipedia. In this lesson you'll learn what PHP is, how it works, and write your very first PHP script.
What is PHP?
PHP stands for PHP: Hypertext Preprocessor (a recursive acronym). It is a server-side scripting language — meaning the code runs on the web server, not in the user's browser.
Here's what happens when someone visits a PHP website:
User's Browser → Request → Web Server (PHP runs here)
↓
PHP reads .php file
↓
PHP generates HTML
↓
User's Browser ← HTML Response ← Web Server
The user never sees your PHP code — they only see the final HTML output. This makes PHP great for dynamic websites, user authentication, database interactions, and more.
PHP vs HTML
HTML is static — it always shows the same content. PHP is dynamic — it can show different content based on conditions, database data, user input, and more.
<!-- Always shows the same thing -->
<p>Hello, John!</p>
<?php
// Shows different name based on who is logged in
$name = "John";
echo "Hello, {$name}!";
?>
Your First PHP Script
PHP code lives inside <?php and ?> tags. You can mix PHP with HTML in the same file. PHP files use the .php extension.
<?php
echo "Hello, World!";
?>
// Output: Hello, World!
Mixing PHP with HTML
You can embed PHP inside HTML files to make them dynamic:
<!DOCTYPE html>
<html>
<body>
<?php
$name = "Suraj";
$day = date("l"); // Gets today's day name
?>
<h1>Hello, <?php echo $name; ?>!</h1>
<p>Today is <?php echo $day; ?>.</p>
</body>
</html>
// Output: Hello, Suraj!
// Today is Friday.
PHP Comments
Comments are lines that PHP ignores — used to explain your code:
<?php
// This is a single-line comment
/* This is a
multi-line comment */
echo "Comments are ignored by PHP";
?>
PHP is Case Sensitive (mostly)
<?php
// Variables ARE case sensitive
$color = "red";
echo $color; // red
echo $Color; // Error! $Color is not defined
// Keywords are NOT case sensitive
ECHO "hello"; // works
Echo "hello"; // works
echo "hello"; // works
?>
echo, if, while etc. It's the standard convention in PHP and makes your code more readable.
What Can PHP Do?
PHP is extremely versatile. Here's what you can build with it:
| Use Case | Example |
|---|---|
| Dynamic websites | Blog, news site, e-commerce |
| User authentication | Login, register, sessions |
| Database interaction | MySQL, PostgreSQL |
| File handling | Upload, read, write files |
| REST APIs | JSON endpoints for mobile apps |
| Email sending | Contact forms, notifications |
| Web frameworks | Laravel, Symfony, CodeIgniter |
Test your knowledge!
Take the PHP Basics quiz to reinforce what you learned.