8

In the past few years, we've been using dynamically typed versions of PHP. However, in PHP7 we have an option to enable 'strict types':

(Note: Scalar Type Declarations repeatedly uses the phrase "weak type checking" or "strictly typed" versus static/dynamic.)

Is it right to say that PHP7 is a statically typed language? If so or not, why?

13
  • 3
    I don't think you can call a language strongly typed if variables don't have a type.
    – Darkhogg
    Jan 7, 2016 at 12:39
  • 2
    That is one possible definition. There are several others for each such phrase. You should use more exact terms, and avoid the strong/weak ambiguity.
    – Jules
    Jan 7, 2016 at 12:56
  • 1
    Not really, no. see the top answer at programmers.stackexchange.com/questions/38002/… or this excellent blog post by Eric Lippert for more detail.
    – Jules
    Jan 7, 2016 at 13:03
  • 6
    Without a definition of the terms "strong" and "weak" typing, this question is meaningless and un-answerable. Jan 7, 2016 at 13:47
  • 3
    Comments are ephemeral. They can (and will be) deleted for lots of different reasons. If it isn't in the question, it doesn't exist. I could probably name about 15 different, incompatible, conflicting definitions of those terms off the top of my head, all of which are in widespread use. Note that Scant Roger doesn't answer the question about strong/weak typing. He even explicitly says so. He answers a different question about static/dynamic typing. Jan 7, 2016 at 14:27

1 Answer 1

12

As mentioned, "strong" and "weak" type systems are ill-defined:

'Languages are often colloquially referred to as "strongly typed" or "weakly typed". In fact, there is no universally accepted definition of what these terms mean. In general, there are more precise terms to represent the differences between type systems that lead people to call them "strong" or "weak"'.

Static types can be verified at compile time. The compiler has enough information to verify static types before a program runs. (If a typed variable is used where the type is not expected, it's a compiler error.) Dynamic types cannot be perfectly determined at compile time. This doesn't mean variables in dynamic languages don't have types. It just means there's no way to be 100% certain of a variable's type until run-time.

According to the PHP RFCs for return type declaration and scalar type declaration, non-typed declarations and evaluation modes are still allowed. This means not all of PHP is statically typed. As such, it doesn't make sense to call PHP a statically typed language. Instead, it's a dynamically typed language with a couple static type features.

The fact that the scalar type declaration RFC actually uses the definitions "strict" and "weak" type checking is telling. The PHP folks pride themselves on not being language nerds. This is neither good nor bad. It just means the language designers are focusing on different things than, say, Haskell designers.

1
  • 10
    A dynamically typed language with optional static annotations is also frequently called gradually typed. It's a current trend in “scripting” languages that want to offer more static safety or better performance, but also in static languages that want to offer a more scripty feel (notably, C#).
    – amon
    Jan 7, 2016 at 14:29

Not the answer you're looking for? Browse other questions tagged or ask your own question.