Variables and constants

What a variable is

In PHP a variable is a named box for a value. The dollar sign marks the name, and the single equals sign stores the value:

<?php $name = "Ada"; $age = 36; var_dump($name); // string(3) "Ada"

php vars.php

string(3) "Ada"

var_dump() prints the type and the length beside the value, so the output proves that the box holds a string of three characters. Nothing declares the type of a PHP variable: the value it holds decides the type, and the next assignment may give it another one.

Variable naming rules

Names start with a letter or an underscore after the dollar sign, never with a digit. Names are case sensitive, so $name and $Name are two different boxes.

Digits may follow the first letter, but spaces and dashes never belong in a name. Pick short words that say what the value means:

<?php $_total = 0; $userName = "ada"; echo $userName;

php names.php

ada

$_total and $userName are both legal: the first starts with an underscore, the second mixes camel case. $2fast is not legal, because a name may not start with a digit, and the parser stops the file before it runs a single line.

Interpolation in strings

Double quoted strings replace variables with their values. This replacement is called interpolation:

<?php $name = "Ada"; echo "Hello, $name!"; // Hello, Ada!

php interp.php

Hello, Ada!

Single quoted strings never interpolate: they print the dollar sign and the name exactly as written:

<?php $name = "Ada"; echo 'Hello, $name!'; // Hello, $name!

php single.php

Hello, $name!

When a variable touches a letter, braces keep the name apart from the text: "{$name}s" reads the variable and then adds the letter s, while "$names" looks for a variable called $names.

Constants with const

Constants hold values that never change. Define them once with const and write their names in capitals:

<?php const SITE_NAME = "Aredel"; echo SITE_NAME; // Aredel

php const.php

Aredel

Unlike variables, constants need no dollar sign when you read them. Use them for settings that stay fixed, like titles and limits.

A second const with the same name does not replace the first one: PHP 8.5 prints Warning: Constant SITE_NAME already defined, this will be an error in PHP 9 and keeps the original value. defined() answers whether a constant exists before the code defines it again.

Exercises

  1. Add the age from vars.php to the script and print both values with one var_dump() call. Which type does the age get?
  2. Rename $userName to $user_name and run names.php again. What changes in the output?
  3. Print Hello, Ada! with a single quoted string and with a double quoted one, then compare the two outputs.
  4. Define a constant LIMIT = 3 and print LIMIT + 1. Then try to change it with a second const and read the warning.

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. Which variable name does PHP accept?

    Answer

    $_total

    A name starts with a letter or an underscore; a digit, a dash or a space in that place is not allowed.

  2. Are $name and $Name the same variable?

    Answer

    No, they are two different variables

    PHP compares variable names byte by byte and keeps case, so a capital letter makes a different box.

  3. Which string form replaces $name with the value of the variable?

    Answer

    A double quoted string

    Only a double quoted string (and a heredoc) interpolates; a single quoted string and a nowdoc print the dollar sign.

  4. How do you read a constant named SITE_NAME?

    Answer

    SITE_NAME

    A constant is read by its bare name; the dollar sign belongs to variables only.

  5. What does this program print? <?php $name = "Ada"; $name = "Grace"; echo $name;

    Answer

    Grace

    The second assignment replaces the value in the same box, so the echo prints the last value.

  6. What does this program print? <?php $x = "Ada"; echo "Hi, $x!";

    Answer

    Hi, Ada!

    A double quoted string interpolates the variable, so the value replaces $x in the text.

  7. What does this program print? <?php $x = "Ada"; echo 'Hi, $x!';

    Answer

    Hi, $x!

    A single quoted string does not interpolate, so the dollar sign and the name print as written.

  8. What does this program print? <?php $a = 1; $A = 2; echo $a + $A;

    Answer

    3

    The two names differ by case, so the two boxes hold 1 and 2, and their sum is 3.

  9. What does this program print? <?php const LIMIT = 3; echo LIMIT + 1;

    Answer

    4

    LIMIT reads as 3, and 3 + 1 is 4; the bare name is how a constant is read.

  10. A file has const LIMIT = 3; and later const LIMIT = 5;. What does PHP 8.5 do?

    Answer

    It prints a warning that LIMIT is already defined and keeps 3

    A constant cannot be redefined: PHP 8.5 keeps the first value and prints Warning: Constant LIMIT already defined, this will be an error in PHP 9.

The code on this page was run with PHP 8.5.13 (php -v) in Alpine Linux, with the commands shown above.

Article author: Arthur Isaev