Coders Republic

Home / Journal

PHP (Part 2): Variables and Constants

PHP (Part 2): Variables and Constants

In Part 1 we covered PHP's basic syntax — the PHP tags, the echo statement, and comments. As promised, this time let's talk about variables and constants: the two ways PHP stores information so you can use it later.

What is a Variable?

Think of a variable as a labelled box where you can keep a value — some text, a number, anything — and grab it again later by its name.

In PHP, every variable name starts with a dollar sign $, followed by a name you choose:

<?php
    $name = "Acronix";
    $age = 30;
?>

Here we created two variables: $name holds the text "Acronix", and $age holds the number 30. The equals sign (=) is the assignment operator — it puts the value on the right into the variable on the left.

Rules for Naming Variables

  • A variable name must start with the $ sign.
  • After the $, it must begin with a letter or an underscore (_) — never a number.
  • It can only contain letters, numbers, and underscores.
  • Variable names are case-sensitive$name and $Name are two different variables.
$user_name = "ok";   // valid
$_token    = "ok";   // valid
$2cool     = "no";   // invalid — starts with a number

Displaying Variables

Remember echo from Part 1? You can use it to print variables too:

<?php
    $name = "Acronix";
    echo $name;        // outputs: Acronix
?>

You can join (concatenate) text and variables using a dot (.):

<?php
    $name = "Acronix";
    echo "Hello, " . $name . "!";   // outputs: Hello, Acronix!
?>

There's also a shortcut: place a variable directly inside double quotes and PHP will swap in its value. This is called variable interpolation:

<?php
    $name = "Acronix";
    echo "Hello, $name!";   // outputs: Hello, Acronix!
?>

But be careful — this only works with double quotes. Inside single quotes, the variable name is printed literally:

<?php
    $name = "Acronix";
    echo 'Hello, $name!';   // outputs: Hello, $name!
?>

Common Data Types

The value you store can be of different types. The ones you'll meet first are:

  • String — text wrapped in quotes: $name = "Acronix";
  • Integer — whole numbers: $age = 30;
  • Float — decimal numbers: $price = 9.99;
  • Booleantrue or false: $isLoggedIn = true;

The good news: PHP figures out the type automatically from the value you assign — you don't declare it yourself. If you ever want to check, use var_dump():

<?php
    $age = 30;
    var_dump($age);   // outputs: int(30)
?>

What is a Constant?

Sometimes a value should never change while your script runs — like the name of your website or a fixed tax rate. For that, we use a constant.

Unlike variables, constants:

  • do not use the $ sign,
  • cannot be changed once defined,
  • are written in UPPERCASE by convention, so they're easy to spot.

There are two ways to define one. The classic way is the define() function:

<?php
    define("SITE_NAME", "Coders Republic");
    echo SITE_NAME;   // outputs: Coders Republic
?>

Or the const keyword:

<?php
    const SITE_NAME = "Coders Republic";
    echo SITE_NAME;
?>

Once it's set, trying to change a constant will cause an error — and that's exactly the point. It protects values that must stay the same.

Variables vs Constants — Quick Recap

  • Use a variable ($) for values that can change as your script runs.
  • Use a constant (UPPERCASE, no $) for values that must stay fixed.

To Be Continued…

Now that you can store data, the next step is doing something with it. In Part 3 we'll cover PHP Operators — how to do math, compare values, and combine conditions. Stay tuned!

← All articles