agyanadda@gmail.com

PHP Data Types

PHP Basics 1 min read Updated Dec 1, 2026 Topic 6 of 36
Type Example
string 'Hello'
int 42, -7
float 3.14, 99.5
bool true, false
array ['red', 'green']
object new Student()
null null — no value

Check the type

  • gettype($x) returns the type name
  • is_int(), is_string(), is_array(), is_numeric(), is_null() return true/false

Type casting

Convert a value: (int) '25' → 25, (string) 99 → '99', (bool) 0 → false, (float) '3.5' → 3.5.

Form values are strings

Everything from $_POST and $_GET arrives as a string. Convert or validate before doing maths.

Type declarations (PHP 8)

Functions can declare types: function add(int $a, int $b): int. Add declare(strict_types=1); at the top of a file to make PHP strict about them.

Example

Example — Types and casting PHP
<?php
$values = ['Asha', 21, 75.5, true, null, [1, 2]];

foreach ($values as $v) {
    echo gettype($v) . '<br>';
}

$age = '25';              // from a form: string
var_dump($age);           // string(2) "25"
$age = (int) $age;
var_dump($age);           // int(25)

var_dump(is_numeric('12.5'), is_numeric('abc'));

Output: string, integer, double, boolean, NULL, array — then string(2) "25", int(25), bool(true), bool(false). (gettype returns "double" for floats.)

Test Yourself With Exercises

Exercise 1 Multiple Choice

What is the type of a value from $_POST?

Exercise 2 True / False

(int) '42' converts the string to the integer 42.

Exercise 3 Fill in the Blank

The value ___ means a variable has no value.

Frequently Asked Questions

PHP converts numeric strings to numbers in maths operations. To join strings, use the dot operator: '10' . '5' gives '105'.