Quick answer
PHP is a server-side programming language that powers WordPress and a large share of websites. To start, install PHP 8 (via XAMPP, Laragon, or php from your OS package manager), create a file named index.php containing <?php echo "Hello"; ?>, and run php -S localhost:8000 in that folder. Then learn variables, arrays, conditions, loops, functions, forms, and connecting to MySQL safely with PDO prepared statements.
Key takeaways
- Learn modern PHP 8.x; avoid tutorials that use mysql_* functions, which were removed years ago.
- The built-in server (php -S localhost:8000) is enough to start; no Apache setup needed.
- Always use prepared statements for database queries and escape output with htmlspecialchars().
- Composer and a framework such as Laravel or Symfony are the next step after the basics.
- PHP skills lead directly to WordPress development, a big freelance and job market in India.
PHP runs on the server, builds web pages and talks to databases. It powers WordPress, WooCommerce, Laravel apps and much of the web, and it has improved a lot in recent years with types, enums, match expressions and better performance. This tutorial takes you from installing PHP to saving form data in a MySQL database, using current PHP 8 syntax.
What do I need to learn PHP?
- Basic HTML (see how to learn HTML step by step).
- A code editor: VS Code is free and has excellent PHP extensions (Intelephense).
- PHP 8.3 or newer installed locally.
How do I install PHP?
| System | Easiest option |
|---|---|
| Windows | Laragon or XAMPP (includes PHP, MySQL/MariaDB and Apache) |
| macOS | brew install php with Homebrew, or Laravel Herd |
| Ubuntu / Debian | sudo apt install php-cli php-mysql |
Check it works with php -v.
Step 1: Write your first PHP script
Create a folder, add a file called index.php:
<?php
$name = "Tamir";
echo "<h1>Hello, $name!</h1>";
echo "<p>Today is " . date('l, j F Y') . "</p>";In a terminal inside that folder, run php -S localhost:8000 and open http://localhost:8000.
Step 2: Variables and data types
<?php
declare(strict_types=1);
$price = 499; // int
$gst = 0.18; // float
$product = "Website audit"; // string
$inStock = true; // bool
$total = $price * (1 + $gst);
echo "$product costs ₹" . number_format($total, 2);Variables start with $. declare(strict_types=1); makes PHP stricter about types, which catches bugs early.
Step 3: Arrays and loops
<?php
$services = ['SEO', 'Google Ads', 'Web Development'];
foreach ($services as $i => $service) {
echo ($i + 1) . ". $service<br>";
}
$client = ['name' => 'Asha', 'city' => 'Mumbai'];
echo $client['name'] . ' from ' . $client['city'];Step 4: Conditions and match
<?php
$score = 82;
$grade = match (true) {
$score >= 90 => 'A',
$score >= 75 => 'B',
default => 'C',
};
echo "Grade: $grade";Step 5: Functions
<?php
function withGst(float $amount, float $rate = 0.18): float {
return round($amount * (1 + $rate), 2);
}
echo withGst(1000); // 1180Need a website that turns visitors into enquiries?
My team builds fast, secure, SEO-ready websites and web apps for growing businesses.
Step 6: Handle a form safely
<?php
$message = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$name = trim($_POST['name'] ?? '');
$email = filter_var($_POST['email'] ?? '', FILTER_VALIDATE_EMAIL);
$message = ($name && $email)
? 'Thanks, ' . htmlspecialchars($name) . '!'
: 'Please enter a name and valid email.';
}
?>
<form method="post">
<input name="name" placeholder="Name">
<input name="email" type="email" placeholder="Email">
<button>Send</button>
</form>
<p><?= $message ?></p>htmlspecialchars() stops users injecting HTML or scripts into your page (XSS).
Step 7: Connect to MySQL with PDO
<?php
$pdo = new PDO('mysql:host=localhost;dbname=leads;charset=utf8mb4', 'root', '', [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
]);
$stmt = $pdo->prepare('INSERT INTO enquiries (name, email) VALUES (?, ?)');
$stmt->execute([$name, $email]);
foreach ($pdo->query('SELECT name, email FROM enquiries ORDER BY id DESC') as $row) {
echo htmlspecialchars($row['name']) . '<br>';
}Prepared statements (the ? placeholders) prevent SQL injection. Never build queries by joining user input into the SQL string, and never use the old mysql_* functions, which were removed in PHP 7.
What PHP security basics should every beginner know?
- Validate input and escape output.
- Use prepared statements for every query.
- Hash passwords with
password_hash()and check them withpassword_verify(). - Keep database passwords out of your code, in environment variables.
- Run a supported PHP version; PHP 8.2 reaches end of life at the end of 2026.
What should I learn after the basics?
- Object-oriented PHP: classes, interfaces, namespaces, enums.
- Composer: PHP’s package manager.
- A framework: Laravel (most popular) or Symfony.
- WordPress development: themes, plugins, hooks and the REST API.
- Git, testing (PHPUnit or Pest) and deployment.
Pair PHP with back-end developer skills to build a career path.
Frequently asked questions
Is PHP still worth learning in 2026?
Yes. PHP powers WordPress and a large share of websites, Laravel is a popular modern framework, and there is steady freelance and agency demand, especially in India.
How long does it take to learn PHP?
With HTML basics, most beginners can build simple dynamic pages in two to four weeks of regular practice, and small database-backed projects in two to three months.
Which PHP version should I learn?
Learn PHP 8.x and install the newest stable version your tools support. Avoid tutorials written for PHP 5, which use removed functions and outdated practices.
Do I need Apache to run PHP?
No. For learning, PHP’s built-in server (php -S localhost:8000) is enough. Use Apache, Nginx or LiteSpeed when you deploy to a real server.