Variables and data types

Store values in variables and understand PHP's types, including how and when PHP converts them.

  • Beginner
  • 4 min read
  • Updated 17 Oct 2026

What you will learn

  • Create and reassign variables
  • Name variables clearly and correctly
  • Identify PHP's scalar and compound types
  • Check a value's type with var_dump and get_debug_type
  • Use constants for values that never change

A variable is a named container for a value. You store something in it, then use the name wherever you need that value.

Creating variables

Variables in PHP start with a dollar sign. Assign a value with =:

PHP
<?php

$name = 'Asha';
$age = 29;
$isMember = true;

echo $name;

You do not declare a type in advance. PHP works out the type from the value.

Assigning again replaces the value:

PHP
<?php

$score = 10;
$score = 15;

echo $score; // 15

Naming rules

  • Start with a letter or underscore: $total, $_count.
  • Then use letters, digits and underscores: $item2, $first_name.
  • No spaces, hyphens or leading digits: $first-name and $2nd are invalid.
  • Names are case-sensitive: $user and $User are different.

Pick names that describe the content. $unpaidInvoiceTotal is longer than $x, but you will never wonder what it holds. PHP projects usually use camelCase ($firstName); WordPress uses snake_case ($first_name). Follow the project you are in.

The main data types

TypeExampleUsed for
int42, -7Whole numbers
float3.14, 0.5Numbers with decimals
string'hello'Text
booltrue, falseYes or no values
array['a', 'b']Lists and maps of values
nullnull"No value"
objectnew DateTime()Instances of classes

Check a value and its type with var_dump(), or get just the type name with get_debug_type():

PHP
<?php

$price = 9.99;

var_dump($price);                 // float(9.99)
echo get_debug_type($price);      // float
echo get_debug_type('9.99');      // string
echo get_debug_type(null);        // null

null

null means a variable has no value. It is common for "not set yet" or "not found":

PHP
<?php

$middleName = null;

if ($middleName === null) {
    echo 'No middle name given';
}

Type juggling

PHP converts types automatically when an operation needs it. This is called type juggling:

PHP
<?php

$result = '5' + 3;
var_dump($result); // int(8)

The string '5' became the number 5. Convenient, but it can hide bugs:

PHP
<?php

var_dump('10 apples' + 5);
Plain text
Warning: A non-numeric value encountered...
int(15)

PHP 8 warns about strings like this instead of silently guessing, which is a big improvement over older versions.

Strict types

At the very top of a file you can turn on strict mode for function arguments. With it, PHP refuses to convert types when calling functions that declare parameter types:

PHP
<?php

declare(strict_types=1);

function double(int $n): int
{
    return $n * 2;
}

echo double(4);    // 8
echo double('4');  // TypeError

Many teams put declare(strict_types=1); at the top of every file. It turns silent conversions into clear errors that are easy to fix.

Converting types yourself

Cast a value when you need a specific type, for example when reading form input, which always arrives as strings:

PHP
<?php

$quantity = (int) '3';      // int(3)
$price = (float) '4.50';    // float(4.5)
$label = (string) 125;      // string(3) "125"
$flag = (bool) 0;           // bool(false)

Constants

A constant holds a value that never changes. By convention constant names are UPPERCASE:

PHP
<?php

const VAT_RATE = 0.2;

$net = 100;
echo $net * (1 + VAT_RATE); // 120

Constants have no dollar sign and cannot be reassigned. PHP also defines useful built-in constants, such as PHP_VERSION, PHP_EOL (the newline for the current system) and PHP_INT_MAX.

Summary

  • Variables start with $ and take their type from their value.
  • The main types are int, float, string, bool, array, null and object.
  • var_dump() and get_debug_type() show what a value really is.
  • PHP converts types automatically; declare(strict_types=1); makes function calls strict.
  • Use constants (const NAME = value;) for fixed values.

Further reading