PHP settype() Function
Quick summary: The PHP settype() function sets the type of a variable.
PHP settype() Syntax
settype(mixed &$value, string $type): bool
PHP
PHP settype() Basic examples
$value = "123";
settype($value, "int");
var_dump($value);
PHP
Output:
int(123)
Converts a variable to a new type.
PHP settype() Real-world usage
$flag = "1";
settype($flag, "bool");
var_dump($flag);
PHP
Output:
bool(true)
Normalizes user input.
PHP settype() Edge cases
$value = "abc";
settype($value, "int");
var_dump($value);
PHP
Output:
int(0)
Invalid numeric strings convert to 0.
PHP settype() Common mistakes
Expecting settype to return a value
settype modifies the variable by reference.
Incorrect
$new = settype($v, 'int');
Correct
settype($v, 'int');
Do not assign the return value.
PHP settype() Frequently Asked Questions
What does settype() do?
Changes the type of a variable.
Does it modify variable?
Yes, in place.
Return type?
Boolean.
Supported types?
int, float, string, array, object, bool.
Use case?
Type casting.
Difference from casting?
settype modifies variable directly.
Performance?
Fast.
Handles invalid types?
Returns false.
Can convert to array?
Yes.
Can convert to object?
Yes.
Strict?
No.
Best practice?
Prefer casting syntax.