Learning outcome: Store information in PHP variables, recognize common value types, and display those values on a web page.
In the previous lesson, you displayed a fixed message with echo. A variable lets you store a value and use it again. PHP variable names begin with $; for example, $studentName.
A variable’s type is determined by the value assigned to it.
Create a file named variables.php in your php-practice folder:
<?php
$studentName = "Aman";
$completedLessons = 1;
echo $studentName;
echo "<br>";
echo $completedLessons;
Start your local server from that folder with php -S localhost:8000, then open http://localhost:8000/variables.php. You should see:
Aman
1
The = sign assigns a value to a variable. It does not display the value; echo does that.
Your course project might need to store a learner’s name, lesson count, progress, and enrollment status:
| Type | Example | Used for |
|---|---|---|
| String | "Aman" |
Text |
| Integer | 1 |
Whole numbers |
| Float | 12.5 |
Numbers with a decimal part |
| Boolean | true |
Yes/no states |
| Null | null |
No value assigned yet |
The four scalar types are string, integer, float, and boolean. null is a separate type. Manual
Try assigning each one:
<?php
$studentName = "Aman";
$completedLessons = 1;
$progressPercent = 12.5;
$isEnrolled = true;
$nextProject = null;
To inspect a value while developing, use var_dump():
var_dump($isEnrolled);
It displays the value and its type, such as bool(true). Use this for debugging rather than as the final text students see on a page. Manual
Replace the contents of variables.php with this complete example:
<?php
$studentName = "Aman";
$courseName = "Practical PHP";
$completedLessons = 1;
$progressPercent = 12.5;
$isEnrolled = true;
$status = $isEnrolled ? "Enrolled" : "Not enrolled";
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>PHP Learning Progress</title>
</head>
<body>
<h1>Learning Progress</h1>
<p>Student: <?= htmlspecialchars($studentName, ENT_QUOTES, 'UTF-8') ?></p>
<p>Course: <?= htmlspecialchars($courseName, ENT_QUOTES, 'UTF-8') ?></p>
<p>Lessons completed: <?= $completedLessons ?></p>
<p>Progress: <?= $progressPercent ?>%</p>
<p>Status: <?= $status ?></p>
</body>
</html>
Refresh http://localhost:8000/variables.php. The page should show Aman’s course name, 1 completed lesson, 12.5% progress, and Enrolled status.
<?= ... ?> is a short way to echo a value inside HTML. The $status line chooses text based on the boolean value; we will explore conditions more fully later. htmlspecialchars() prepares text for display in HTML. It is a useful habit when values may eventually come from a form or database. Manual
Turn the example into your own learning progress card:
2 and $progressPercent to 25."PHP Variables".false. Check what happens to the status.Check your work: The page should display your name, 2 completed lessons, 25% progress, your current topic, and Not enrolled.
<?php
$studentName = "Simran";
$courseName = "Practical PHP";
$completedLessons = 2;
$progressPercent = 25;
$isEnrolled = false;
$currentTopic = "PHP Variables";
$status = $isEnrolled ? "Enrolled" : "Not enrolled";
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>PHP Learning Progress</title>
</head>
<body>
<h1>Learning Progress</h1>
<p>Student: <?= htmlspecialchars($studentName, ENT_QUOTES, 'UTF-8') ?></p>
<p>Course: <?= htmlspecialchars($courseName, ENT_QUOTES, 'UTF-8') ?></p>
<p>Lessons completed: <?= $completedLessons ?></p>
<p>Progress: <?= $progressPercent ?>%</p>
<p>Current topic: <?= htmlspecialchars($currentTopic, ENT_QUOTES, 'UTF-8') ?></p>
<p>Status: <?= $status ?></p>
</body>
</html>
$: Write $studentName, not studentName.$currentTopic = "PHP Variables";. Without quotes, PHP will not treat it as a text string.;.Checkpoint: You are ready to continue when you can change the variables, refresh the browser, and explain why the displayed values change.