Current Location: Home> Latest Articles> settype() performance in boolean conversion

settype() performance in boolean conversion

gitbox 2025-05-27

In PHP, the settype() function is used to convert variables to specified data types. It not only changes the type of the variable, but also directly modifies the variable itself. This article will focus on the specific performance of settype() when converting variables to bool values ​​and things to note.


1. Introduction to the settype() function

The basic usage of settype() is as follows:

 settype(mixed &$var, string $type): bool
  • $var is a variable of the type that needs to be converted, and the function will directly modify this variable.

  • $type is the target type, such as "bool" , "int" , "string" , etc.

  • The return value is a Boolean value indicating whether the conversion is successful.


2. Performance when converted to a boolean

When settype() converts a variable to a Boolean value, the conversion rules are consistent with PHP's built-in Boolean conversion rules:

  • "False" value is converted to false , including:

    • Boolean value false

    • Integer value 0

    • Floating point value 0.0

    • Empty string "" and string "0"

    • Empty array []

    • NULL

  • All other values ​​are converted to true

Sample code:

 $values = [0, 1, -1, "", "0", "hello", [], [1], null, false, true];

foreach ($values as $val) {
    settype($val, "bool");
    var_dump($val);
}

Output result:

 bool(false)
bool(true)
bool(true)
bool(false)
bool(false)
bool(true)
bool(false)
bool(true)
bool(false)
bool(false)
bool(true)

3. Specific precautions

3.1 The variable itself is modified

settype() will directly modify the variable passed in. Note that if you want to retain the original value, you should copy the variable first.

 $original = "0";
$copy = $original;

settype($copy, "bool");

var_dump($original); // string(1) "0"
var_dump($copy);     // bool(false)

3.2 Performance when entering complex types

If the object or resource is passed in, the normal conversion of PHP is also followed when converting to a Boolean:

  • Resources and objects are generally converted to true unless they are null or empty objects.

  • Note that settype() cannot directly convert the object to a boolean, and the result may not be as expected.

3.3 Return value if conversion failed

Although settype() will succeed in most cases, some unsupported type conversions will return false .

 $obj = new stdClass();
$result = settype($obj, "bool"); // return false,Unable to convert
var_dump($result);

4. Summary

Features illustrate
Directly modify the change amount Settype() will change the variable itself passed in
Convert boolean values ​​according to PHP rules Only specific values ​​(such as 0, empty string, NULL, etc.) will become false , and the others are true
Cannot convert complex objects Cannot convert the object directly to a boolean, the conversion will fail and return false
Return to the successful conversion status Returns a boolean value to indicate whether the conversion is successful

Understanding these behaviors and limitations can help you more accurately control variable types and program logic when converting booleans using settype() .