settype
Changes the type of a variable in place.
Syntax
settype(mixed &$var, string $type): boolParameters
| Parameter | Type | Description |
|---|---|---|
| $var | mixed | The variable. |
| $type | string | The new type: `int`, `float`, `string`, `bool`, `array`, `null`. |
Return value
bool`true` on success.
Examples
A string becomes a number:
<?php
$value = '42 items';
settype($value, 'int');
var_dump($value);Output:
int(42)Notes
A (int) $value cast is usually handier: it returns a new value and leaves the variable alone.
See also
In the tutorial
- Data types — The main PHP data types — strings, numbers, booleans, arrays and null.