PHP.nl

Introduction

Introduction

Every single expression in PHP has one of the following built-in types depending on its value:

  • null

  • bool

  • int

  • (floating-point number)float

  • string

  • array

  • object

  • callable

  • resource

    PHP is a dynamically typed language, which means that by default there is no need to specify the type of a variable, as this will be determined at runtime. However, it is possible to statically type some aspect of the language via the use of . Different types that are supported by PHP's type system can be found at the page. type declarationstype system

    Types restrict the kind of operations that can be performed on them. However, if an expression/variable is used in an operation which its type does not support, PHP will attempt to

    the value into a type that supports the operation. This process depends on the context in which the value is used. For more information, see the section on . type juggleType Juggling

Tip: > may also be useful, as various examples of comparison between values of different types are present. The type comparison tables

Opmerking: > It is possible to force an expression to be evaluated to a certain type by using a . A variable can also be type cast in-place by using the function on it. type castsettype

To check the value and type of an , use the function. To retrieve the type of an , use the function. However, to check if an expression is of a certain type use the

functions instead.

expressionvar_dumpexpressionget_debug_type``is_typeVoorbeeld: Different Types

<?php
$a_bool = true;   // a bool
$a_str  = "foo";  // a string
$a_str2 = 'foo';  // a string
$an_int = 12;     // an int

echo get_debug_type($a_bool), "\n";
echo get_debug_type($a_str), "\n";

// If this is an integer, increment it by four
if (is_int($an_int)) {
    $an_int += 4;
}
var_dump($an_int);

// If $a_bool is a string, print it out
if (is_string($a_bool)) {
    echo "String: $a_bool";
}
?>
bool
string
int(16)

Opmerking: > Prior to PHP 8.0.0, where the is not available, the function can be used instead. However, it doesn't use the canonical type names. get_debug_type``gettype