Tell Numbers from Strings When Assigning PHP Variables
Learn how to inspect PHP values with var_dump() and see how reassignment can change a variable’s type without a type declaration.
To tell whether a PHP variable currently holds a number or text, inspect its value with var_dump(). PHP does not require you to declare a variable’s type first: the value has a type, and assigning a different kind of value can change the type you observe. That matters when a variable contains digits, because digits alone do not tell you whether the value was assigned as a number or as quoted text.
Assign a value
The basic pattern is $name = value;. The dollar sign identifies a PHP variable, = assigns the value, and the semicolon ends the statement. For example, $count = 12; assigns a whole-number value to $count, while $label = "12"; assigns quoted text containing the same two digits. The assignments look similar, but the quotes are an important difference when you inspect the values.
Names need a little care. A PHP variable name starts with a letter or underscore; subsequent characters can include letters, numbers and underscores. The dollar sign comes before that name, so $item2 follows the rule but $2item does not. Names are case-sensitive: $count and $Count refer to different variables. The PHP manual’s variable basics documents both the naming rule and case sensitivity.
Read the value’s type
The example below puts three assignments side by side: an unquoted whole number, a decimal value and quoted digits. PHP’s basic types include integers, floats and strings; a PHP variables and types guide describes integers as whole numbers and floats as real numbers. Rather than treating a variable name—or the digits it contains—as proof of its type, use the inspection call beside each assignment to see what PHP reports for that value.
PHP also has booleans, which represent true or false, and a variable can be assigned null to indicate that it contains no value. Those assignments are included briefly so you can recognize that numbers and strings are not the only possibilities.
The practical choice when writing these assignments is to use quotes for text and leave them off the number in the whole-number example. When reading unfamiliar code, look for the most recent assignment to the variable, not just its first one. PHP does not require an upfront type declaration and allows a variable’s type to change through reassignment, as the Laravel PHP fundamentals guide explains.
Check and change a type
var_dump() lets you inspect a value’s type and value. This self-contained PHP example includes an opening <?php tag for a PHP file. The final two calls inspect the same variable, $current, on either side of an assignment. That makes the change easier to spot than comparing two differently named variables.
<?php
$whole = 12;
$decimal = 12.5;
$digits = "12";
var_dump($whole);
var_dump($decimal);
var_dump($digits);
$enabled = true;
$missing = null;
var_dump($enabled);
var_dump($missing);
$current = 12;
var_dump($current);
$current = "12";
var_dump($current);
Read each dump together with the assignment immediately above it. Compare the reported types for $whole, $decimal and $digits, paying particular attention to the difference between unquoted 12 and quoted "12". The boolean and null calls show what inspection looks like for two other assigned values; they do not change the number-versus-text question.
Finally, compare the two dumps of $current. Its name stays the same, but its assigned value changes from unquoted digits to quoted digits. If the inspected type changes, reassignment explains it: there was no type declaration to edit. A beginner’s guide to PHP variables likewise uses var_dump() to inspect a variable’s type and value.
