5. PHP Variables

What you will learn

  • How to create and use variables
  • The naming rules
  • How to change a value and join text
  • What constants are

What is a variable?

A variable is a named container for a value. In PHP every variable name starts with a dollar sign $. There is no keyword like let or const: you simply assign a value.

PHP code

<?php
$name = "Sam";
$age = 25;

echo $name . " is " . $age . " years old.";

Output

Sam is 25 years old.

The dot . joins text (PHP uses . where JavaScript uses +). PHP works out the type of a variable from the value you give it.

Naming rules

  • Start with $, then a letter or underscore.
  • After that, use letters, digits, and underscores.
  • No spaces or hyphens.
  • Names are case sensitive: $total and $Total are different.
Name Valid?
$first_name Yes
$_count Yes
$total2 Yes
$2total No: starts with a digit
$first-name No: hyphen

The common style in PHP is snake_case ($first_name), and many projects use camelCase ($firstName). Pick one and stay consistent. Use names that explain the value.

Changing values

PHP code

<?php
$score = 10;
$score = $score + 5;
$score += 2;
$score++;

echo "Score: " . $score;

Output

Score: 18

Variables inside double quotes

In double quotes, PHP replaces variable names with their values. This is called interpolation. Single quotes keep the text exactly as written.

PHP code

<?php
$city = "Lahore";

echo "Welcome to $city! ";
echo 'Welcome to $city!';

Output

Welcome to Lahore! Welcome to $city!

For array items or properties, wrap the variable in curly braces: "Hello {$user['name']}".

Variables are not always set

Using a variable that was never given a value produces a warning in modern PHP. Check first with isset(), or give a fallback with ??.

PHP code

<?php
$nickname = "Sam";

echo isset($nickname) ? "nickname is set. " : "nickname is missing. ";
echo "Other: " . ($other ?? "no value");

Output

nickname is set. Other: no value

Constants

A constant holds a value that never changes. It has no dollar sign, and the convention is capital letters. Create one with const or define().

PHP code

<?php
const SITE_NAME = "BeingXpert";
define("TAX_RATE", 0.1);

echo SITE_NAME . " tax: " . TAX_RATE;

Output

BeingXpert tax: 0.1

Variable scope: a first warning

A variable made outside a function is not visible inside it. This surprises people who know JavaScript. You will pass values in as parameters, which the Functions lesson explains.

PHP code

<?php
$greeting = "Hello";

function show() {
    // $greeting is NOT available here
    return isset($greeting) ? "visible" : "not visible";
}

echo show();

Output

not visible

Common mistakes

  • Forgetting the $ when using a variable.
  • Using different capital letters in two places.
  • Joining text with + instead of ..
  • Expecting a single-quoted string to insert variables.
  • Using a variable that was never set.

Exercise

Create variables $product (Notebook), $price (4.5), and $qty (3). Output: 3 x Notebook = 13.5, with the total calculated by PHP.

Show solution
<?php
$product = "Notebook";
$price = 4.5;
$qty = 3;

echo "$qty x $product = " . ($price * $qty);

Knowledge check

1. Which symbol starts every PHP variable name?

The dollar sign ($).

2. Which operator joins strings in PHP?

The dot (.).

3. Do single quotes replace variables inside the text?

No. Only double quotes do.

Summary

  • Variables start with $ and are case sensitive.
  • Double quotes insert variables, single quotes do not, and . joins text.
  • Use constants for fixed values, and check with isset() or ?? when a value may be missing.
Scroll to Top