/home/josephspurrier

Strong, Weak, Dynamic, and Static Typed Programming Languages Explained

After doing a little searching, I’ve found it’s a little difficult to understand the differences between strongly typed, weakly typed, dynamically typed, and statically typed programming languages. I’d like to clear that up.

First, let’s define what typing is:

In programming languages, a type system is a collection of rules that assign a property called a type to the various constructs—such as variables, expressions, functions or modules—a computer program is composed of. The main purpose of a type system is to reduce bugs in computer programs by defining interfaces between different parts of a computer program, and then checking that the parts have been connected in a consistent way. This checking can happen statically (at compile time), dynamically (at run time), or it can happen as a combination of static and dynamic checking. Wikipedia

There are two important pieces to pull from the definition. The first is that a type system is a collection of rules. The second is that checking for the rules can apply at different times.

Here’s what we can deduce so far:

There is an extremely helpful post by Naresh Jain that includes this diagram:

strongweakstaticdynamic_type

Naresh’s image shows that there are different degrees of typing. There are languages that have more rules (more strongly typed) than others.

Here’s another definition from Wikipedia that explains a strongly typed language:

In 1974, Liskov and Zilles described a strong-typed language as one in which “whenever an object is passed from a calling function to a called function, its type must be compatible with the type declared in the called function.” Jackson wrote, “In a strongly typed language each data area will have a distinct type and each process will state its communication requirements in terms of these types.” Wikipedia

According to this definition, PHP is NOT a strongly typed language because you can pass multiple variable types to the same function. Let’s take a look at this example of PHP:

// Pass the integer 4
// Outputs: int(9)
var_dump(foo(4));

// Pass the string 4
// Outputs: int(9)
var_dump(foo('4'));

// Pass the string 4
// Outputs: Fatal error: Unsupported operand types
var_dump(foo(array(4)));

// Argument does not have a distinct type
function foo($bar)
{
    // Return the value + 5
    return $bar + 5;
}

In the example above, the function, foo(), does not specify a distinct type for argument $bar so a programmer can pass any type of variable. The first time the function is called, the integer of 4 is passed and returns an integer of 9. The second time the function is called, the string of 4 is passed and again returns an integer of 9. The third and final time the function is called, an array containing an integer of 4 is passed and results in a Fatal Error.

The example above shows PHP is weakly typed and dynamically typed. In terms of the number of rules, PHP uses strings and integers interchangeably so there are “less rules”. Therefore, PHP is considered a weakly typed language. PHP is also dynamically typed because the rules are applied at run time instead of at compile time since PHP is not compiled and instead interpreted at run time.

Note: PHP does support type hinting which makes it less weakly typed/more strongly typed, yet it is still considered a weakly typed language because you cannot use type hinting for integers or strings.

In summary, here are how I see the different types defined:

Hopefully, this clears up a few things.

#code