Quicky 1: PHP is Loosely Typed – What does that Mean?


Target Audience:

Anyone using the PHP programming language.  Anyone wanting to learn more about the benefits and flaws of a loosely typed language.

Languages Used:

PHP 5+

Related Material:

PHP Manual – Class
Class Design in PHP
OOP in PHP
PHP Good Practices 1 – Naming Conventions
PHP Good Practices 2 – Self-Documenting Code

Introduction:

A loosely typed language such as PHP is a language that does not require you to declare a variable type when declaring a variable.  What does that mean?  In PHP when you declare a variable, you can with no need to worry about what kind of data will be stored in that variable.  I could make a new variable called $string and then assign it an integer value.

Languages such as C# and others are strictly typed, meaning that variables MUST declare type at the time the variable is declared, and that type is strictly enforced.  This means if I declare a new variable I have to specify a data type to enforce that variable.   Once I have declared a variable as being one type, the data in that variable must remain of that type unless the type is ‘Cast’ to a new type. Type Casting is the process of converting a variable of one type to another.  Seems like a lot of work huh?  It can be, but it has its benefits as well.

When you code in PHP, you don’t have to worry about what kind of data is coming in and out of your variables if you don’t want to.  This does not mean you shouldn’t though!  One of the biggest problems with PHP applications is that programmers don’t organize their data well, and applications run buggy.  Having variable be loosely typed can lead to all kids of unexpected run-time errors if you are not careful.

On the flip side, not being constrained to a specific type can add flexibility, and allow for far more dynamic capabilities.   PHP is one of the most flexible web programming languages I have worked in.  Perl is another one.  The flexibility provided with a loosely typed language can be golden to programmers who are aware of the challenges they also present.  If you can organize your data well, and write your own constraints to fit within your program, you can do amazing things with PHP.   If you don’t however, you can produce some very buggy code.

The Draw Back:

The biggest problem with loosely typed languages is the opportunity it gives programmers to write very bad code!  I myself wrote a lot of bad code as I learned PHP, for the very reason that I did not understand the purpose of data typing.   The problem is, when a variable allows you to assign it any form of data, it adds a lot of opportunity for error.  If I have a function that I know requires an integer input, and my variable got changed to a string somewhere along the line, my function will break!

This draw back can be overcome with a little bit of proper structure and organization.  In most cases you DON’T WANT TO CHANGE TYPES!  Doing so adds to much ambiguity to your code and creates avenues for problems to occur.   It can also create a lot of headaches when your program breaks because your bool value of ‘True’ has been converted to a string value of “True”, and the comparison is not what you expect.

When using PHP and declaring variables, I still recommend staying consistent and trying to enforce your own types.  You can do this in your classes through encapsulation by enforcing input to be of a certain type inside your setter functions, prior to assigning data to your class variables.  Outside of OOP you can enforce type by creating validate functions that check type prior to assigning the data to a variable.  Either way, the extra work is worth the trouble when the amount of errors you have to respond to shrinks.

The Benefits:

The benefits of a loosely typed language are in its flexibility, especially when creating highly dynamic applications.   Purely dynamic code can be difficult to work with due to its nature.   The application is responsible for determining course of action based on conditions and data that are often unknown to the programmer.  PHP and other loosely typed languages allow this kind of ‘complex’ programming to be possible by not enforcing data types on variables.   This means you can use a variable to hold data that could be ANYTHING!

Although this could lead to very bad things if done incorrectly, it can also lead to amazing application functionality when done properly.  I have written a lot of bad code.  I have learned a lot from that bad code as well, and in result have written some applications that surprised me.  PHP allows us to set the rules by being loosely typed, which allows us far more flexibility then strictly typed languages.  But you still NEED TO SET THE RULES!

Just because PHP does not set the rules, and does not enforce that you have to, does not mean you shouldn’t.  It does however mean a little more work for you as a programmer.  I was unique in my learning of data typing, in that I learned PHP and Pearl before any strictly typed languages.   This made me unaware of the benefits of data typing for a long time.  The advantage for that is it allowed me to think outside the box right away, and now I understand the advantages and possibilities within a loosely typed language.

Wrap It Up

Now that I understand the benefits of data typing, I also can apply those concepts to my loosely typed languages, giving me the best of both worlds.  I would encourage that you do the same.  If you have not studied a language such as C# or other strict languages, you should.  Doing so will teach you the benefits of working in such a strict environment, and allow you to apply those concepts to your loosely typed language while still having all the benefits it can provide!

— Live, Learn, Share —

Helpful Books:

5 thoughts on “Quicky 1: PHP is Loosely Typed – What does that Mean?

  1. A concrete example of an amazing things can be done with a loosely typed language and cannot be done (or at least not with the same amount of work) with a strongly typed language would be welcome….

  2. Very nice reading indeed, The first programming language I learned was Pascal, but then I met PHP and worked very much with it in web development. Right now I’m learning C# and Java, both of them are amazing languages, but I still use PHP because of its dynamic nature and because I can write web applications faster with it. I also would like to learn Ruby, just need more time.
    I think just one language could make me being infidel with PHP and that’s Javascript (although they are used for different purposes), it is so dynamic!!! I love closures!!!, (although PHP also has closures since 5.3 version, that’s awesome!!!)

    With PHP a programmer should be very tidy and disciplined just not to make a whole mess with it. But on the other side you can achieve amazing things!.

    The only thing I really miss from a typed language in IDEs is autocompletion, it’s great to have all the methods and variables right at hand!, and sometimes just to make: this.getArray()[x][y] instead of $p = $this->getArray(); $p[x][y];

    Regards!

Leave a reply to Jonathan Cancel reply