🌙
Happy Karwa Chauth! Wishing all couples a blessed Karwa Chauth. May your bond grow stronger every year. From Team Job...
💬 Share
Web Development Tutorials

PHP for Beginners: Variables, Forms, Includes and Your First Dynamic Page

PHP still powers a huge share of the web, including most small business sites in India. Here's PHP for beginners with practical, safe examples.

In this article · 9 sections
  1. Setup
  2. Your first PHP
  3. Variables and arrays
  4. Loops and conditions
  5. Functions
  6. Includes: reuse header and footer
  7. Handling a form
  8. Security habits from day one
  9. FAQs
  10. Is PHP still worth learning in 2026?
  11. Which PHP version should I use?

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>&copy; $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);
<?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, $_POST or 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

Share this article

Joboo Web

Web design, SEO & digital marketing team · Ludhiana, Punjab

Joboo Web has helped Indian businesses grow online since 2019 — building fast websites and running SEO, Google Ads and social campaigns for clients in 80+ cities. About us →

Keep reading

💬