| 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 nameis_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.)