Skip to content

PHP Tutorial Getting Started with PHP

Your First PHP Program

Lesson 11 of 369 2 min read By Deepak Paswan Updated Oct 5, 2026

Let us write a small page that greets someone by name and uses the time of day.

Step 1: store a value

A variable holds a value. In PHP, a variable name starts with $.

index.php
PHP
<?php
$name = "Riya";
echo $name;
Output
OUTPUT
Riya

Step 2: build a message

Put the variable inside a string. Double quotes let you place a variable right in the text.

index.php
PHP
<?php
$name = "Riya";
echo "Hello, $name!";
Output
OUTPUT
Hello, Riya!

Step 3: make a decision

Use if to choose a different message depending on the hour.

index.php
PHP
<?php
$name = "Riya";
$hour = 9; // set by hand so the result is predictable

if ($hour < 12) {
    $greeting = "Good morning";
} else {
    $greeting = "Good afternoon";
}

echo "$greeting, $name!";
Output
OUTPUT
Good morning, Riya!

Step 4: put it in a page

Here is the whole thing as a web page. This time it uses the real hour from the built-in date() function.

index.php
PHP
<?php
$name = "Riya";
$hour = (int) date("G"); // 0 to 23

$greeting = $hour < 12 ? "Good morning" : "Good afternoon";
?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Greeting</title>
</head>
<body>
  <h1><?= $greeting ?>, <?= $name ?>!</h1>
</body>
</html>

You do not need to understand every symbol yet. You have used a variable, a string, a condition and a built-in function, and each one has its own chapter later in the course.

Exercise: add an evening greeting

Change the code so it says “Good evening” when the hour is 18 or more. You will need elseif.

Show a possible solution
index.php
PHP
<?php
$hour = 20;

if ($hour < 12) {
    $greeting = "Good morning";
} elseif ($hour < 18) {
    $greeting = "Good afternoon";
} else {
    $greeting = "Good evening";
}

echo $greeting;
Output
OUTPUT
Good evening