Beginner Fundamentals
PHP Data Types
Data types describe the kind of value a variable holds. PHP has several built-in types for different needs.
Main types
- string: text, like
"hello" - int: whole numbers, like
42 - float: decimal numbers, like
3.14 - bool:
trueorfalse - array: a collection of values
- null: no value
<?php
$text = "hello"; // string
$count = 10; // int
$price = 19.95; // float
$active = true; // bool
$items = [1, 2, 3]; // array
$empty = null; // null
Checking a type
Use gettype() to get the type name as a string.
<?php
$value = 42;
echo gettype($value); // integer
Inspecting with var_dump
var_dump() shows the type and value, which is great for debugging.
<?php
var_dump(42); // int(42)
var_dump("hi"); // string(2) "hi"
var_dump(true); // bool(true)
var_dump([1, 2]); // array(2) { ... }
Use var_dump whenever you need to know exactly what a variable contains.