HexLibraryHexLibrary
</> HexCodeSign inSign up
PythonJavaScriptTypeScriptCC++JavaPHPGoRustRubyBashSQLLuaPerlCOBOLPascalDelphiFortranDartSwiftC#JuliaGroovyRElixirHaskellKotlinScalaClassic ASPClipperMS-DOS
LESSONS
Introduction to PHPVariablesData TypesOperatorsStringsArraysAssociative ArraysConditions (if / else)Loops (for / foreach / while)Functions
REFERENCE
📖 Overview
🖥️ Output
📦 Variables
🔢 Numbers & Math
✍️ String
📋 Array
🔁 Condition & Loop
🧩 Function
🔄 Type & Conversion
PHP›Data Types

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

Example code
1
<?php
Start.
DefinitionStart.
2
$text = "Alex";
string
Variablestring
3
$number = 36;
int
Variableint
4
$decimal = 1.72;
float
Variablefloat
5
echo gettype($number);
integer
Output / Inputinteger
DefinitionVariableOutput / Input

About this topic

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.

What you will learn

  • Recognizing the eight basic types
  • Inspecting types with gettype and var_dump
  • Explicit type casting
  • The difference between loose and strict comparison
  • The distinction between null and empty values
  • Using type declarations

When to use?

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.

Common mistakes

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";
}

Frequently asked questions

What is the difference between double equals and triple equals?

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.

What is the difference between null and an empty string?

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.

Why do I need to cast form data?

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.

Summary

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.

HexLibraryHexLibrary

Learn programming more clearly. Every command is explained line by line with colors, reinforced with runnable exercises, test your code instantly — all in one place, free.

Learn

  • Home
  • Languages & Lessons
  • HexCode

Company

  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

Other Projects

  • HexToolz
  • Free Service Manuals
© 2026 HexLibrary · The clearest way to learn code