PHP settype() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Variables

Definition

In PHP, settype() changes the type of a variable in place. You give it a variable and the name of the type you want, such as 'integer', 'string' or 'array'.

It does the same conversion as a cast like (int) or (string), but it changes the original variable directly instead of returning a new value. Because the variable is passed by reference, the type of $var is different after the call.

Syntax

Call settype() with the values you need. Start with this line.

PHP
settype($value, 'integer');
CallWhat it does
settype($v, 'integer')Changes $v into an int, so '42abc' becomes 42.
settype($v, 'string')Changes $v into a string, so 5 becomes '5'.
settype($v, 'array')Wraps a simple value inside an array with one item.

The valid type names are 'bool' or 'boolean', 'int' or 'integer', 'float' or 'double', 'string', 'array', 'object' and 'null'. Casting such as (int) $v is usually shorter when you do not need to change the variable itself.

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

Parameters

ParameterDescription
varRequired. The variable to convert. It is passed by reference, so the variable itself is changed.
typeRequired. The name of the new type: 'bool', 'boolean', 'int', 'integer', 'float', 'double', 'string', 'array', 'object' or 'null'.

Return Value

Return ValueDescription
boolReturns true when the conversion works. In PHP 8, an invalid type name throws a ValueError instead of returning false.

PHP Examples

Example 1 — Changing to Integer and String

Convert a messy string to an int and an int to a string.

PHP
<?php
$value = '42abc';
settype($value, 'integer');
var_dump($value);

$price = 5;
settype($price, 'string');
var_dump($price);

How It Works

  1. $value starts as the string '42abc'. It begins with digits followed by letters.
  2. settype($value, 'integer') changes it. PHP reads the leading digits and keeps 42.
  3. $price starts as the integer 5. The second call converts it to the string '5'.
  4. var_dump() confirms the new types. The same variable names now hold different types.

Example 2 — Return Value and Invalid Types

settype() returns true on success. It also truncates floats, and an unknown type name throws a ValueError in PHP 8.

PHP
<?php
$a = 'hello';
var_dump(settype($a, 'bool'));
var_dump($a);

$b = 3.9;
settype($b, 'int');
var_dump($b);

try {
    settype($b, 'banana');
} catch (ValueError $e) {
    echo 'ValueError caught', PHP_EOL;
}

How It Works

  1. settype() returns true on success. The first var_dump() shows that return value.
  2. A non-empty string becomes true. After the call, $a holds the boolean true.
  3. 3. 9 becomes 3 as an int. The decimal part is cut off, not rounded.
  4. 'banana' is not a valid type. PHP 8 throws a ValueError, which the catch block handles.

Example 3 — Array, Null and Boolean

Wrap a string in an array, empty a variable with null, and turn a string zero into false.

PHP
<?php
$a = 'text';
settype($a, 'array');
print_r($a);

$b = 'hello';
settype($b, 'null');
var_dump($b);

$c = '0';
settype($c, 'boolean');
var_dump($c);

How It Works

  1. Converting a string to an array wraps it. The text becomes the single item at index 0.
  2. Converting to null removes the value. The variable still exists, but it now holds null.
  3. The string '0' becomes false. PHP treats the string zero as a false value.
  4. Each variable changed type in place. No new variable was needed for any of them.

Frequently Asked Questions

It changes the type of a variable in place. For example, settype($x, 'integer') turns the value in $x into an integer and stores it back in $x.
A cast like (int) $x returns a new converted value and leaves $x alone. settype() converts the variable itself. Both follow the same conversion rules.
It accepts 'bool' or 'boolean', 'int' or 'integer', 'float' or 'double', 'string', 'array', 'object' and 'null'. Any other name throws a ValueError in PHP 8.
PHP does not warn you. A float like 3.9 becomes 3 and the string '12abc' becomes 12. Keep a copy of the original value if you may need it again.
It returns true when the type was changed. In PHP 8 and later, an invalid type name throws a ValueError instead, so you do not need to check for false.

Next: strval()

strval() returns the string value of a variable. It converts integers, floats, booleans an.

strval() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful