settype

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

Changes a variable's type in place (bool/int/float/string/array/object/null...) - the by-reference conversion is the odd one out: every other language returns a new value with types fixed.

C C23

C23
#include <stdio.h>

int main(void) {
    /* C variables cannot change type - ever. A cast makes a NEW value:
       x stays int, d is a separate double */
    int x = 42;
    double d = (double) x;
    printf("%d %g\n", x, d);
    /* "changing type in place" in C means overwriting the bytes:
       a union, and the type tag lives in your head */
    union { int i; float f; } u = { .i = 1 };
    u.f = 1.5f; /* same bytes, new meaning - unchecked by the compiler */
    printf("%g\n", u.f);
    return 0;
}
Output
42 42
1.5

C++ C++26

C++26
#include <iostream>
#include <variant>

int main() {
    int x = 42;                        // types are compile-time fixed
    double d = static_cast<double>(x); // new value; x unchanged
    std::cout << x << ' ' << d << "\n";
    // the C++ container for "a variable that changes type": std::variant,
    // an explicit tagged union - settype's job, done with type safety
    std::variant<int, double> v = 42;
    v = 1.5; // the active alternative switches in place
    std::cout << std::get<double>(v) << "\n";
    return 0;
}
Output
42 42
1.5

C# v11.0.100

v11.0.100
string s = "42";
int n = int.Parse(s); // a NEW value; s is still a string
Console.WriteLine($"{s} {n}");
// C# has no in-place retyping: variable types are fixed at declaration.
// The closest container is object boxing plus pattern matching:
object box = 42;
if (box is int i) {
    Console.WriteLine(i + 1); // 43
}
Output
42 42
43

Go v1.27.4

v1.27.4
package main

import (
	"fmt"
	"strconv"
)

func main() {
	s := "42"
	n, _ := strconv.Atoi(s) // new value; s unchanged
	fmt.Println(s, n)
	// Go types are static: a variable's type never changes.
	// "in-place retyping" does not exist; interface{} boxing
	// changes the DYNAMIC type of the box, not any variable's declared type
	var box interface{} = 42
	box = "now a string"
	fmt.Printf("%T\n", box)
}
Output
42 42
string

Java v27.0.1

v27.0.1
public class Main {
    public static void main(String[] args) {
        String s = "42";
        int n = Integer.parseInt(s); // a NEW value; s unchanged
        System.out.println(s + " " + n);
        // Java variable types are final at declaration - in-place retyping
        // does not exist; Object references only change what they point to
        Object box = 42;
        box = "now a string";
        System.out.println(box.getClass().getSimpleName());
    }
}
Output
42 42
String

JavaScript v26.13.1

v26.13.1
let x = "42";
const n = Number(x); // a NEW value; x unchanged
console.log(x, n, typeof x);
// JS variables are untyped bindings: x may be ASSIGNED anything,
// which looks like settype - but it is rebinding, not conversion
x = 42;
console.log(typeof x); // number
Output
42 42 string
number

PHP v8.6.0

v8.6.0
// settype changes the variable ITSELF - the by-reference oddball
$x = '42';
settype($x, 'int'); // $x is now int(42), in place
var_dump($x, get_debug_type($x));
$y = 0;
settype($y, 'bool');
var_dump($y); // false: 0 converts falsy
$z = '1.5';
settype($z, 'float');
var_dump($z);
settype($z, 'string');
var_dump($z); // round trip: "1.5"
settype($z, 'array');
var_dump($z); // scalar -> array wraps it: [0 => '1.5']
var_dump(settype($x, 'int')); // returns true on success
Output
int(42)
string(3) "int"
bool(false)
float(1.5)
string(3) "1.5"
array(1) {
  [0]=>
  string(3) "1.5"
}
bool(true)

Python v3.15.2

v3.15.2
x = 42
d = float(x) # a NEW object; the name x still points to the int
print(x, d)
s = str(x)
print(s, type(x).__name__) # x never changed
# Python names are untyped bindings: rebinding x = "42" looks like
# settype but is rebinding - the int object itself never converted
Output
42 42.0
42 int

Rust v1.99.1

v1.99.1
fn main() {
    let s = "42";
    let n: i32 = s.parse().unwrap(); // a NEW value; s unchanged
    println!("{} {}", s, n);
    let f = n as f64; // as-cast: new value
    println!("{}", f);
    // Rust bindings are typed at declaration: in-place retyping does not
    // exist; shadowing (let n = "42";) creates a NEW binding, the old
    // value keeps its type forever
}
Output
42 42
42

TypeScript v26.13.1

v26.13.1
let x = "42";
const n = Number(x); // a NEW value; x unchanged
console.log(x, n, typeof x);
// TS adds static types at compile time: assigning 42 to x later makes
// the compiler complain - the type layer forbids settype-style changes
Output
42 42 string

Language differences

C: variable types are fixed forever and casts yield new values; in-place retyping is only possible by overwriting bytes in a union - the type tag lives in your head, which is the root of settype's dangerous side (type punning, and under strict aliasing rules a UB minefield).

C++: static_cast returns a new value and variable types are compile-time fixed; std::variant is the explicit tagged union - the correct container for "a variable that changes type", switching its active alternative in place with a type check on every access (get throws bad_variant_access on mismatch).

C#: every conversion returns a new value (Parse/Convert/ToXxx); variable types are fixed at declaration; object boxing only changes what a reference points to. ref parameters pass variables but the type stays a compile-time fact - no settype counterpart.

Go: types are fully static - conversion T(v) always returns a new value; interface{} boxing changes the box's dynamic type, not any variable's declared type; no counterpart.

Java: same as Go - conversion methods and casts all return new values; Object references merely redirect; variable types are final at declaration; no counterpart.

JavaScript: Number()/String()/Boolean() all return new values; variables are untyped bindings, and reassignment changes the binding rather than converting the value - it looks like settype but is rebinding.

PHP: settype IS the odd by-reference conversion - the first argument is taken by reference and the variable retypes in place, returning true on success; target types: bool/boolean, int/integer, float/double, string, array, object, null (paired aliases are another historical burden). Scalar-to-array wraps the value as [0 => value]; scalar-to-object yields a stdClass. Modern PHP style leans on explicit cast operators (new values), and settype mostly appears in older code.

Python: int()/float()/str()/bool() all return new objects; names are untyped bindings - rebinding is not conversion and the original object's type never changes; no counterpart.

Rust: types are fixed at declaration; as/parse/From all return new values; shadowing creates a new binding while the old value keeps its type - "a variable changing type" simply does not exist in the language model.

TypeScript: at runtime the same as JavaScript; at compile time types narrow or error on assignment - settype-style in-place retyping is explicitly rejected by the type system (unless any).