In this article · 9 sections
PHP runs on almost every shared hosting plan and powers WordPress and many custom business websites in India. This guide to PHP for beginners takes you from your first line to a small dynamic page, with safe habits from the start.
Setup
Install XAMPP (or similar) to run PHP on your computer. See XAMPP localhost setup. Save files with the .php extension inside the htdocs folder.
Your first PHP
<?php
$business = "Joboo Taxi";
$year = date("Y");
echo "<h1>Welcome to $business</h1>";
echo "<p>© $year</p>";
PHP runs on the server and sends plain HTML to the browser.
Variables and arrays
<?php
$km = 310;
$rate = 12;
$fare = $km * $rate;
$cars = ["Dzire", "Ertiga", "Innova Crysta"];
$route = ["from" => "Ludhiana", "to" => "Delhi", "km" => 310];
echo $route["from"] . " to " . $route["to"];
Loops and conditions
<?php foreach ($cars as $car): ?>
<li><?= htmlspecialchars($car) ?></li>
<?php endforeach; ?>
<?php if ($fare > 3000): ?>
<p>Long-distance fare applies.</p>
<?php endif; ?>
<?= ... ?> is a short way to echo. Always use htmlspecialchars() when printing data that could come from users or a database.
Functions
<?php
function calc_fare(int $km, int $rate = 12): int {
return $km * $rate;
}
echo calc_fare(310);
Includes: reuse header and footer
<?php include 'header.php'; ?>
<main>
<h1>About Us</h1>
</main>
<?php include 'footer.php'; ?>
Change the menu once in header.php and every page updates.
Handling a form
<?php
$name = trim($_POST['name'] ?? '');
$phone = preg_replace('/\D/', '', $_POST['phone'] ?? '');
$errors = [];
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
if ($name === '') $errors[] = 'Name is required';
if (strlen($phone) !== 10) $errors[] = 'Enter a 10-digit phone number';
}
?>
<form method="post">
<input name="name" value="<?= htmlspecialchars($name) ?>">
<input name="phone" value="<?= htmlspecialchars($phone) ?>">
<button>Send</button>
</form>
To email the enquiry, see PHP contact form with PHPMailer. To save it, see MySQL basics.
Security habits from day one
- Escape output with
htmlspecialchars() - Use prepared statements for database queries. See PHP security
- Hash passwords with
password_hash(). See secure login tutorial - Never trust
$_GET,$_POSTor cookies
FAQs
Is PHP still worth learning in 2026?
Yes, especially in India, where many business websites, WordPress projects and Laravel apps use it. See Python vs PHP.
Which PHP version should I use?
A currently supported PHP 8.x version.
Was this article helpful?
Your answer helps us improve our guides