Skip to content
VJ Classes

One place to learn science, commerce, arts, computers and life skills

This lesson covers the PHP you need to build pages: variables, arrays, control flow, functions and including files.

Variables and types

<?php
declare(strict_types=1);   // optional: stricter type checks in this file

$courseName = "Physics for Class 11";
$fee = 1500;                // int
$rating = 4.6;              // float
$isOpen = true;             // bool
$teacher = null;            // no value

echo "Course: $courseName, fee ₹$fee";   // double quotes interpolate variables
echo 'Single quotes print $fee literally';

Strings join with .: "Hello " . $name.

Arrays

<?php
$subjects = ['Maths', 'Physics', 'Chemistry'];          // indexed array
echo $subjects[0];                                       // Maths
$subjects[] = 'Biology';                                 // append

$student = ['name' => 'Arjun', 'class' => 11, 'city' => 'Kota'];   // associative array
echo $student['name'];

$courses = [
  ['title' => 'Physics', 'fee' => 1500],
  ['title' => 'Maths', 'fee' => 1200],
];

Useful functions: count(), in_array(), array_keys(), array_map(), array_filter(), usort(), implode(), explode().

Control flow

<?php
foreach ($courses as $course) {
  $label = $course['fee'] > 1300 ? 'Premium' : 'Standard';
  echo "{$course['title']}: ₹{$course['fee']} ($label)<br>";
}

if ($fee >= 1000 && $isOpen) {
  echo 'Enrolment open';
} elseif (!$isOpen) {
  echo 'Enrolment closed';
}

$day = date('N');   // 1 (Monday) to 7 (Sunday)
echo match (true) {
  $day >= 6 => 'Weekend batch',
  default   => 'Weekday batch',
};

match (PHP 8) is a stricter, cleaner alternative to switch.

Functions

<?php
function inr(int $paise): string {
  return '₹' . number_format($paise / 100, 2);
}

function withGst(int $amount, int $ratePercent = 18): int {
  return intdiv($amount * (100 + $ratePercent), 100);
}

echo inr(149900);        // ₹1,499.00

Type declarations (int, string) and return types catch mistakes early.

Mixing PHP and HTML cleanly

For templates, use the alternative syntax, which reads well inside HTML:

<ul>
<?php foreach ($courses as $c): ?>
  <li><?= htmlspecialchars($c['title']) ?>: ₹<?= number_format($c['fee']) ?></li>
<?php endforeach; ?>
</ul>

Includes: don’t repeat yourself

<?php require __DIR__ . '/includes/header.php'; ?>
<main>
  <h1>Courses</h1>
</main>
<?php require __DIR__ . '/includes/footer.php'; ?>

require stops with an error if the file is missing (good for essential files); include only warns. __DIR__ makes paths reliable regardless of where the script is called from.

A sensible project structure

mysite/
  public_html/        <- files the web server can serve (index.php, css, images)
  app/                <- functions, database code, templates
  config.php          <- secrets: database password (outside the web root if possible)

Keeping configuration and library code out of the publicly served folder protects them from being downloaded.

Try this

Create includes/header.php and includes/footer.php with your site’s navigation, and a courses.php page that lists an array of five courses with their fees, marking those above ₹1,000 as “Premium” using a function you write.

Key points

  • Variables start with $; double quotes interpolate; strings join with a dot.
  • Indexed and associative arrays; foreach for lists; match for clean branching.
  • Typed functions catch errors early; use the alternative syntax in templates.
  • require shared headers and footers; keep config and code outside the web root.