LEARN TO CODE | PHP | 01 SERVER-SIDE FOUNDATIONS

01.2 Variables, types and output

Store values in variables, recognise common types and insert clear output into an HTML response.

01 | STORE A VALUE YOU CAN REUSE

Use the local development server from 01.1 . Save each complete example in its own .php file inside myWebsite, keep the server running and open its URL through http://127.0.0.1:8000/. Save changes and refresh the browser.

Short PHP-only examples can also be served as files: their output appears as the response body. Each example is independent unless stated otherwise. Browser HTML view, not the editor’s source view, shows the result.

A variable gives a value a name. Instead of repeating a member’s name in several messages, store it once and use that variable wherever needed.

<?php
$name = 'Maya';
echo $name;

EXPECTED OUTPUT

Maya

PHP variable names begin with $. The = assigns the right-hand value to the variable. It is not an equality test.

02 | NAMES AND CASE MATTER

Use descriptive names such as $clubName, $capacity and $isOpen. A variable name starts with a letter or underscore after $, then can contain digits. Our examples use ordinary ASCII names.

$name and $Name are different variables. Use one naming style consistently and assign a value before reading it.

A comment helps explain a choice. Use // for a line comment or /* ... */ for a block. A comment is not displayed unless you deliberately output its text.

<?php
// A capacity is a whole number of places.
$capacity = 12;
echo $capacity;

03 | COMMON VALUE TYPES

Types describe values
PHP type Example Meaning
string 'Robotics' Text
int 12 Whole number
float 2.5 Floating-point number
bool true or false Logical value
null null No value assigned for this purpose
array ['Art', 'Chess'] Collection, introduced in 01.4

PHP is dynamically typed: a variable can hold values of different types as assignments occur. That flexibility does not mean you should change a variable’s meaning unexpectedly.

'12' is text; 12 is an integer. PHP can convert types in some operations, but explicit, predictable code is easier to understand.

04 | SEE THE TYPE WHILE DEBUGGING

<?php
$places = 12;
$label = '12';
var_dump($places);
var_dump($label);

EXPECTED OUTPUT

int(12)
string(2) "12"

var_dump is useful for inspecting a value and its type. In a browser, normal HTML whitespace can hide line breaks; use View Page Source to inspect this output or place the debug output inside a temporary <pre> element.

Debug displays are not a finished user interface. Remove them when replacing them with meaningful page output.

05 | CONCATENATE WITH A DOT

<?php
$name = 'Maya';
$club = 'Robotics';
echo 'Hello, ' . $name . '! Welcome to ' . $club . '.';

EXPECTED OUTPUT

Hello, Maya! Welcome to Robotics.

The dot joins strings. Spaces inside quoted strings matter: 'Hello,' without a following space would touch the next value.

PHP uses + for numeric addition. Use . for text concatenation rather than importing habits from another language.

06 | SINGLE AND DOUBLE QUOTES

<?php
$name = 'Maya';
echo "<p>Hello, $name.</p>";
echo '<p>Hello, $name.</p>';

EXPECTED OUTPUT

Hello, Maya.
Hello, $name.

Double-quoted strings can interpolate simple variables; single-quoted strings keep $name as literal text. Both are useful when chosen deliberately.

To avoid quote confusion, separate larger HTML structures from PHP expressions. We will use that approach in the complete page below.

07 | BOOLEANS NEED MEANINGFUL DISPLAY

<?php
$isOpen = false;
if ($isOpen) {
    echo 'Open';
} else {
    echo 'Closed';
}

EXPECTED OUTPUT

Closed

Directly echoing true produces 1, while false produces an empty string. Neither automatically prints the words true or false.

This example uses selection, explained next lesson, to turn a logical value into a useful message. NULL is also not a ready-made label for users.

08 | HTML OUTPUT AND ESCAPING

<?php
$club = 'Art & Design';
echo htmlspecialchars($club, ENT_QUOTES, 'UTF-8');

When inserting a text value into HTML text, htmlspecialchars converts special characters to their HTML-safe representations. The browser displays Art & Design as text; the source contains an escaped ampersand.

ENT_QUOTES also handles quotation marks. This is HTML output escaping, not validation or a universal solution for JavaScript, CSS or URL contexts. The later validation-and-escaping guide explores context properly.

Start the habit even with fixed example data so the code is ready for the distinction when forms are introduced.

09 | BUILD A COMPLETE WELCOME PAGE

<?php
$name = 'Maya';
$club = 'Robotics';
$places = 12;
?>
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>Club welcome</title></head>
<body>
    <h1>Club welcome</h1>
    <p>Hello, <?= htmlspecialchars($name, ENT_QUOTES, 'UTF-8') ?>.</p>
    <p>Club: <?= htmlspecialchars($club, ENT_QUOTES, 'UTF-8') ?></p>
    <p>Places: <?= $places ?></p>
</body>
</html>

Save as welcome.php. The initial PHP block prepares values, then HTML presents them. The short <?= expression ?> form echoes the expression; it does not require a special short_open_tag setting.

Known integer places is displayed directly. Names and club text are escaped. PHP-only files usually omit the final closing tag to avoid accidental trailing output; mixed HTML templates need closing tags to return to HTML.

10 | REQUESTS DO NOT AUTOMATICALLY REMEMBER

Change $places to $places - 1 in your page and refresh twice. If each request begins by assigning 12, each response still shows 11 rather than counting down to 10.

Each request runs the initialisation again. Persistent storage and sessions need explicit mechanisms; ordinary local variables alone are not a saved database.

TRY IT YOURSELF

Create a page showing a student name, a club name and a known integer year level. Include a club name containing & and inspect the response source.

Reveal guidance

Prepare variables in a PHP block, output the text through htmlspecialchars and display the integer in a paragraph. The browser should show an ordinary ampersand while the response source contains &amp;.

11 | PREDICT BEFORE RUNNING

TRY IT YOURSELF

Predict the difference between $total = 12 + 3 and $label = '12' . '3'.

Reveal guidance

The first is integer 15; the second is string 123. Confirm using var_dump, then explain how the operators determine the operation.

Next, we will make decisions and repeat output using conditions and loops.

KEEP LEARNING

Previous page 01.3 Operators, selection and loops

References: PHP development server · Types · Arrays · HTML text escaping