juniorPHP

What are variables, data types, and constants in PHP?

Updated Feb 20, 2026

Short answer

Variables are named containers that store values during a PHP program's execution. PHP is dynamically typed, so a variable's data type is determined automatically from the value assigned to it. Constants store values that cannot be changed after they are defined.

Deep explanation

Variables in PHP

A variable is a name that refers to a value stored in memory. In PHP, every variable begins with the $ symbol, followed by its name.

variables.php
<?php
$name = "Aisha";
$age = 22;
$isStudent = true;
echo $name;
?>

PHP variables do not require a type declaration before use. The language infers the type from the assigned value. This makes PHP flexible for beginners but also means developers must be careful because values can change type during execution.

For example:

PHP
<?php
$value = 100;
$value = "one hundred";
?>

The same variable now holds an integer and then a string. PHP variables can change their data type dynamically at runtime.

Data Types in PHP

A data type defines what kind of value a variable stores and what operations can be performed on it.

Common PHP data types include:

TypeExamplePurpose
int$count = 10;Whole numbers
float$price = 19.99;Decimal numbers
string$name = "Sam";Text values
bool$active = true;True/false values
array$items = ["pen", "book"];Multiple values together

PHP also supports object, null, and resource types.

The following diagram shows how a value is assigned and interpreted by PHP:

Rendering diagram…
⚠️ A variable name points to a value, but the value's type comes from what is assigned, not from the variable declaration.

Constants in PHP

A constant is a named value that cannot be changed after it is created. Constants are useful for configuration values or fixed information that should remain the same throughout an application.

Constants are commonly created using define() or the const keyword.

constants.php
<?php
define("SITE_NAME", "My Store");
const TAX_RATE = 0.18;
echo SITE_NAME;
?>

Unlike variables, constants do not use the $ prefix.

FeatureVariableConstant
SyntaxStarts with $No $ prefix
Can change valueYesNo
Scope behaviorDepends on contextGenerally global when created with define()
Common useTemporary or changing dataFixed application values

Use constants for values that represent rules or settings, not temporary program data.

Why Interviewers Ask This

Interviewers want to know whether you understand PHP's flexible typing model. A good answer mentions that PHP is dynamically typed, variables can hold different types, and constants provide a safer way to store fixed values.

The key difference: variables can change, constants cannot.

Real-world example

Imagine building an online shop. A customer's name and shopping cart can change, so they should be stored in variables. The tax rate and company name are fixed settings, so constants are a better fit.

shop-config.php
<?php
$customerName = "Ravi";
$cartItems = 3;
const TAX_RATE = 0.18;
$totalTax = $cartItems * TAX_RATE;
echo $customerName;
?>

Here, $customerName and $cartItems represent changing application data. TAX_RATE represents a business rule that should stay consistent across calculations.

Common mistakes

  • * **Missing `$`** - Writing `name = "John"` instead of `$name = "John"` causes invalid PHP syntax. Always prefix variables with `$`.
  • * **Changing constants** - Trying to assign a new value to a constant creates errors. Use variables when the value needs to change.
  • * **Ignoring types** - Assuming a variable always contains one type can cause bugs. Check values carefully when handling user input.
  • * **Using constants for everything** - Constants are for fixed values, not normal temporary data storage.

Follow-up questions

  • What is the difference between dynamically typed and statically typed languages?
  • How can you check a variable's data type in PHP?
  • What happens if you use an undefined variable in PHP?
  • When should you use a constant instead of a variable?

More PHP interview questions

View all →