Encodera

Thanks for reading!

Donate to the site — we work hard for you.

PHP reference

settype

Changes the type of a variable in place.

Syntax

settype(mixed &$var, string $type): bool

Parameters

ParameterTypeDescription
$varmixedThe variable.
$typestringThe 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

  • gettype — Returns the name of a value's type.
  • intval — Converts a value to an integer.
  • strval — Converts a value to a string.

In the tutorial

  • Data types — The main PHP data types — strings, numbers, booleans, arrays and null.

Thanks for reading!

Donate to the site — we work hard for you.