A r t i c l e s
Navigation

Note: This Wiki is
outdated, personal views
may have changed.
L505 A.I. bot is dead
long live THX 1138

M a i n P a g e

D i r e c t o r y

Weak and Dynamic Typing Issues


PHP CODE:
  $height1 = 'p20';
  $height2 = 'p25';

  print $height1 + $height2."\n";
  print $height2 + $height1."\n";

  $height1 = '10px';
  $height2 = '16px';

  print $height1 + $height2."\n";
  print $hieght2 + $height1."\n";

EXECUTED OUTPUT OF ABOVE SCRIPT:

  0 
  0
  26
  10

This bothers me deeply. If I were to add 10px plus 16px together, I would expect that I cannot add those together, as they are strings. PHP takes 10px and turns it into a 10 integer, but it won't take p10 or p200 or p500 and turn it into an integer?

Worse yet, the next version of PHP might repair this lunacy and all your programs might be further broken since PHP no longer lies to you about what is going on, in some different implementation of PHP. Why I consider this lying? Because sometimes you are working with integers, and sometimes you aren't.. even if you feed it a string. PHP lies to you, without telling you.. that yeah that is a string. No, that isn't. Yeah that is an integer. No that isn't. Yes it is. No it isn't. Liar. Pants on fire pants on fire.


See also

The above script does not even display any errors at run time.

What happens if you are making an HTML wrapper and you decide you are going to add 10px to 16px in order to output some CSS styling? It would be wise to separate your px from 10. But if you forget to separate your px from your 10? PHP adds 10 with 16, but not 10px with 16px! This is ridiculous. But they are not integers, they are pixels! They are really strings. But it treats them as integers.. why? Then it treats n10 or a10 or y10 as zero (0)?. Why is a10 a zero, but but 10a is a ten? This is weak and dynamic typing for you. Lovely, isn't' it?

If these were strings you are adding together, you would think PHP would warn you that you should separate your pixels from your integers. i.e. cannot add two strings together, Johnny - you can't do that! Error. But no. There is no error. PHP does still allow you to add n10 with n16, but the result is zero! And PHP does still allow you to add 10n with 16n, but surprise surprise, the result is 26, not 26n and not 0.

Wouldn't you think that if 10px plus 16px is OKAY to do in PHP, then the result should be 26px?

The weakly and dynamically typed PHP language is inconsistent, confusing, and bizarre. At least, if I'm going to use a weakly/dynamically typed language - I want it to be reasonable. I want it to be consistent.

I'm surprised that there are even such things as "parse errors" in PHP. It is still checking your code for some sort of human error that you made. But all weak/dynamic type language zealots claim that programmers don't make many mistakes. Then why have parser errors? It's a contradictory and inconsistent programming methodology.

Weakly and Dynamically typed zealots will say that it doesn't matter if you are using a string or an integer - that you should be worrying more about the program code than worrying about the types you are using. Hmmm. Then tell my why I can add 10px and 16px if it is a string type, and not an integer type? A zealot would respond that this is convenient - that I can add 10px with 16px - but it's NOT convenient that 10px and 16px equals 26, rather than 26px. Yeah, truncate that px on me. And it is NOT consistent that p10 plus p16 equals zero, while 10p plus 16p equals 26.

Take a look at the above example again and you will see a spelling mistake which was made on purpose. Height is spelled as "hieght". Even though "hieght" was not initialized or defined anywhere, the program still runs fine. No errors.

Imagine a shopping cart you are programming late at night which is running live on a corporate server. Consider the next script, further down. You are the programmer who has been hired for this corporate website, and the website sells fencing which are measured in feet or meters. They sell large fencing, small fencing - any size.

PHP CODE:

  $sidefence_width = '10m';
  $frontfence_width = '16m';

  print $sidefence_width + $frontfence_width."\n";
  print $frontfence_width + $sidefence_width."\n";

  $sidefence_width = 'ft:30';
  $frontfence_width = 'ft:16';

  print $sidefence_width + $frontfence_width."\n";
  print $sidefence_width + $frontfence_width."\n";

  $sidefence_width = '3Oft';
  $frontfence_width = '5Oft';

  print $sidefence_width + $frontfence_width."\n";
  print $front_fencewidth + $sidefence_width."\n";

EXECUTED OUTPUT OF ABOVE SCRIPT:

  26
  26
  0
  0
  8
  5

You upload your PHP script to the fencing website after you've just done some work on their shopping cart system. The shopping cart allows people to pick their custom fence that they want built, using their measurements which they input for the webserver to receive after a few forms are submitted. The website is busy and people are ordering items throughout the day. Your program appears to run fine on the server after you upload it, so you leave the program. No visible errors after you upload your script, everything is running smoothly.

But little do you know, it's off by 10 feet, and your customers are ordering fences which will cost your company thousands of dollars. A fence that is 10 feet too short? No big deal - we're using PHP, so surely our customers will understand and find it perfectly acceptable that their fence is 10 feet short and maybe 16 feet taller than they ordered.

And not only did you make their fence 10 feet too short, but they also got a "0" when they checked to see how many fence posts you have in stock. They thought you only had zero fence posts in stock when maybe you had 100 in stock. But since you use a weakly and dynamically typed language to power your website, of course this happens every week. Isn't that great.

You as the programmer hope for your life that a program like this won't even run! Your web server shouldn't accept this program - even if it looks fine to the eye after a few days of coding. You're tired - you're human. You make mistakes, you are a programmer. I would like to be at least warned - if the program even decided to run - that PHP is not smart enough to add 16px plus 10px - because the measurement gets truncated on me and I only end up with 26, not 26px! And I would like to be warned that PHP is not smart enough to add ft10 with ft16. I don't want it to display 0 when it adds ft10 with ft16 - I want the program to stop with an error.

And why does $front_fencewidth + $sidefence_width only equal 5? Look at it closely.

In fact, even another issue pops up. What if I am doing byte and character manipulation? I want to add the byte 'T' with the byte 'R' and the result is 0? This makes no sense. But byte R is not 0. and Byte T is not 0.

So do you want to buy a weak fence from us that dynamically changes size depending on what hour of the day you visit our website?

About
Note: This Wiki is outdated, personal views may have changed.

This wiki contains info on life, health, humans, nature, programming, database, fads, paradigms, poems, principles, theories.

Articles may contain statements which some may find helpful and encouraging, or even discouraging.

Beware, I believe in the Grand Justice system.
_ _ _