Initializing & typing variables with settype()

These days, the way to develop is to have E_ALL and maybe even throw in E_STRICT if you are really hard core. That of course means having all your variables initialized before they are used. You could just do something like this:


$var1 = 0;
$var2 = "";
$var3 = 0.00;
$var4 = array();
$var5 = new stdClass;


That works fine and is pretty clear. However, I think a more elegant solution is to use settype() like this:


settype($var1, "int");
settype($var2, "string");
settype($var3, "float");
settype($var4, "array");
settype($var5, "object");


IMHO, this is much more clear. Its a standard way to set the type and default value of your variables. You see, settype() assigns a default value to the variables if they do not exist already.

Another handy use is in a function.


function myfunc( $someint, $somestring) {

settype($someint, "int");
    settype($somestring, "string");

        echo $somestring;


Now this example does not do much for the string, but it ensures the int variable is an integer. Without the settype() line, the if() statement would evaluate to true if some string value was passed in for $someint.

The one down side that some may not like is with arrays and objects. If the variable is a scalar type (int, string, float, boolean), it will be changed into an array/object with a member called scalar that contains the scalar value.

There is no note in the official docs (there is one in the comments) about settype initializing a variable. I think I will submit a documentation update request in the bug system. I am also considering writing a test to ensure this unknown, but IMHO very useful feature stays in PHP.


7 Responses to Initializing & typing variables with settype()

  1. Pure-PHP says:

    If you define a variable like this
    $var1 = 1;
    The type of variable is INT. There is no need to use settype for this variable.
    Every time you compare a variable with an int, php internally calls intval on this variable.
    I don’t no which version of php you use.
    My quick test with php5

    $someint = “a string”;
    if( $someint > 0 ){ // false
    echo $someint; // prints nothing

    $someint = “1 a string”;
    if( $someint > 0 ){ // true
    echo $someint; // prints 1, because intval( “1 a string” ) => 1

    $someint = “a string 1”;
    if( $someint > 0 ){ // false
    echo $someint; // prints nothing

  2. Brian Moon’s Blog: Initializing & typing variables with settype()

  3. Brian says:

    Elegant or not, the first set of code looks like the way any programmer in any language would declare variables. Nothing to figure out, it reads like english. Remeber, you’re probably not the only one who is ever going to have to read this code.

  4. Nate K says:

    I think this is a nice example. I use type casting in many different places, and this would keep things clean from the get go. Sometimes PHP has some quirky conversions that can stump some developers. Doing this from a start (making it strict) would be much more helpful.

  5. Sor says:

    Personally I dislike this method as you leave it up to PHP what the value of variable is going to be. Another disadvantage is that IDEs are unable to figure out the type of the variable, in fact I don’t think they would even recognise it as a variable declarement. It also leaves you completely vulnerable to register_globals weirdness.

  6. joel says:

    The problem with this is that you don’t actually initialize the variable, you’re only setting the type, assuming that it wasn’t already set.

    $var = “hello”;

    settype($var, “int”);

    woops. now $var is int(0). atleast with $var = 1; you know exactly what the value is at that point. settype doesn’t. I find that settype() is only useful if I need the return value that a cast wouldn’t be able to give me and is a nicer way of doing $var = (int) $var; for places where I really have to be 100% sure of the type before I use it.

  7. joel says:

    I mean 1, not 0. :S

%d bloggers like this: