Data Types
string, int, float, bool types; learning types with gettype and type casting.
Simple enough for a 5-year-old to understand
Basic types
<?php$text = "Alex";$number = 36;$decimal = 1.72;echo gettype($number);string, int, float, bool types; learning types with gettype and type casting.
Simple enough for a 5-year-old to understand
<?php$text = "Alex";$number = 36;$decimal = 1.72;echo gettype($number);PHP's type system is flexible but hard to predict. There are eight types: int, float, string, bool, array, object, null, and resource. You don't have to declare types; PHP converts them based on context.
This automatic conversion is PHP's most criticized feature. PHP 8 significantly improved the situation: comparing text with numbers now behaves much more sensibly, and meaningless conversions throw errors. Still, you will encounter type-related surprises in legacy code.
The safe path is clear: declare types in functions, turn on strict typing mode, and explicitly cast incoming data. These three habits prevent almost all type-related bugs.
var_dump is the core debugging tool in PHP: it shows both the value and the TYPE together, making it much more useful than echo. The fastest way to understand why a variable isn't behaving as expected is var_dump.
Form data always comes in as text — even a number field. You must explicitly cast it before doing any calculations.
Wrong
$value = "0";
if ($value) {
echo "full";
} else {
echo "empty";
}The text "0" evaluates to FALSE in PHP — even though it is a non-empty string. The output is "empty". This is PHP's most famous pitfall; isset or length checks should be used for existence validation.
Correct
$value = "0";
if ($value !== "") {
echo "full";
} else {
echo "empty";
}Wrong
$a = "abc";
if ($a == 0) {
echo "equal";
}In PHP 7 and earlier, this condition would be TRUE — the string was converted to a number and counted as 0, which was the source of countless security vulnerabilities. PHP 8 fixed this, but loose comparison is still unpredictable; strict comparison should always be used.
Correct
$a = "abc";
if ($a === 0) {
echo "equal";
}Wrong
$sum = 0.1 + 0.2;
if ($sum == 0.3) {
echo "equal";
}The float type stores decimals approximately; the sum is not exactly 0.3 and the condition fails. For monetary calculations, integers representing cents or bcmath functions should be used instead of float.
Correct
$sum = 0.1 + 0.2;
if (abs($sum - 0.3) < 0.000001) {
echo "equal";
}Double equals compares by converting types, yielding unpredictable results. Triple equals compares both the value and the TYPE. Rule of thumb: always use triple equals; situations where you consciously want loose comparison are very rare.
Null means "no value"; an empty string is a value, it is just empty inside. This distinction matters in databases: an unpopulated field is null, while a deliberately left blank field is an empty string. Loose comparison treats them as equal, strict comparison does not.
The HTTP protocol transmits everything as text; number fields also arrive as text. If you plug them directly into calculations, PHP does automatic conversion, but when the user enters "abc", the behavior becomes unpredictable. Explicit casting and validation are mandatory.
PHP types are flexible and automatic conversion yields unpredictable results; the text "0" evaluating to false is a classic trap. Always use strict comparison, inspect the type with var_dump, and explicitly cast incoming data.