A r t i c l e s
Navigation

Note: This site is
a bit older, personal views
may have changed.

M a i n P a g e

D i r e c t o r y

Myths About PHP Syntax


There is a myth floating about that PHP is generally *better* for web programming than Powtils because PHP handles strings better and has nicer $variables that can be embedded in the code. Maybe it is implied that PHP is better, rather than explicitly agreed upon.

One could argue that Pascal is better than PHP because Pascal has strong typing and Pascal is not as limited as PHP. There are ways to extend Pascal using libraries and units to parse embedded variables in strings and templates.

Aren't there many factors to consider, and aren't there even some compromises that allow us to use $variables inside pascal code when we need them?

Some newsgroup postings about PHP vs Powtils:

And of course some false rumors:
  But you're sure they would support "PSP", then? Seems unlikely, if they
  don't even support something as standard as ASP.NET...

  -- 
  marc hoffman 
Fud, Fud, Fud. Listen to the dramatic music.

But wait a minute, Powtils runs on any typical web server or cheap hosting account. Powtils is more supported than Ruby or ASP since it runs on any server with cgi-bin available.

These discussions have a PHP advocate coming in and bragging about the advantages of PHP. Yes, we know, but that can also ironically end up being a disadvantage. I missed this conversation. However, no one brought up the fact that you can embed variables in Powtils/Pascal code with WebWriteF, WebFormat, or WebWriteA, WebTemplateOut, or Format().

The Arguments

One of the arguments is that PHP is better (cleaner) than Pascal for web programming because PHP offers more built in string abilities (such as embedded $variables) in the language. However, this also leads to language abuse, and ugly code.

Basically, the dominant argument is that PHP contains dollar sign $variables which can be embedded in strings. Pascal doesn't have them? Well, you can use embedded variables in pascal. There are workarounds and solutions. There are also functions like the WebWriteA which allows multiple types to be sent in without using the plus concat sign.

Busting The Myths

In powtils we have the WebWriteA(), WebWriteF(), and WebFormat() functions. We have many more than just those.

PHP allows you to do something like this:

< h t m l >
< b o d y >

< ? p h p  
  $somevar = 'another string';
  $s = "This string"." contains multiple strings "."and even $somevar";
  print $s;
p h p ? > 

< / b o d y >
< / h t m l >
Powtils allows you to do:
var
  s: string;
  somevar: string = 'another string';
begin
  setwebvar('somevar', somevar);
  s = 'This string' + ' contains multiple strings ' + 'and even a $somevar';
  webwritef(s);
end.

Advantages of WebWriteF

The webwritef function contains security too, whereas PHP is prone to hackers when magic $variables are embedded inside strings (especially if register_globals option is on, as in many older versions of PHP). Using functions like WebWriteFF or WebWriteF allow the programmer to go through security first. PHP just slaps the variable into the string and hackers can inject javascript into URL variables many times.

So I could argue that "PHP SUCKS BECAUSE IT ALLOWS HACKERS TO EXPLOIT $VARIABLES IN STRINGS". Even if register_globals is off, php programmers often forget to filter and check incoming variables since all programmers are lazy. Again, another advantage of Powtils that we can argue are the security issues. I'm sorry that I wasn't there in the newsgroup posting to bring up these "my language is better" arguments.

More Options

You could also make a Format function of your own // formating output function function WebFmt(s: string; vals: array of const); begin // handle the [val1, val2] array of const here. // ... for i:= low(vals) to high(vals) do... //... if String //... if Integer //... if Float //... etc. end;
var
  s: string;
  somevar1: string = 'another string 1';
  somevar2: string = 'another string 2';
begin
  s = 'This string' + ' contains multiple strings ' + 'and even a %1';
  webFmt(s, [somevar1]);
  s = 'This string' + ' contains multiple strings ' + 'and even a %1 and %2';
  webFmt(s, [somevar1, somevar2]);
end.
The percentage variables %1 and %2 would bind to somevar1 and somevar2.

All of these above solutions and compromises allow us to have the best of both worlds.. embedded variables while still having extra safety such as strong typing and a powerful fast modern pascal language.

Templates

There is also the webtemplateout() function and third party template engines with Powtils so that $variables can be used in templates.

WebWriteA function

Powtils also allows you to do this:
var
  s: string;
  number: integer;
begin
  s:= 'the number is: ';
  number:= 100;
  webwritea([s, number, ' and the float: ', 600.353]);
end.
The webwriteA function allows multiple types to be sent in to a single function without resorting to something like this:
var
  s: string;
  i: integer;
begin
  s:= 'the number is: ';
  i:= 100;
  webwrite(s + IntToStr(100) +' and the float: ' + FloatToStr(600.353));
end.

The arguments in the newsgroup postings claimed that IntToStr() and StrToInt() and StrToFloat() get in the way of the programmer. This is a minor issue in programming and can be solved. We just have to put our thinking caps on. Use the WebWriteA function and be a bit creative instead of claiming that Pascal is dead and done and that PHP rules.

All languages have their disadvantages. When I have to program in Ruby I find that this gets in my way all the time:

  s = 'some string';
  puts s[1];
In ruby, the above s[1] code does not print the character to the screen. It outputs the ordinal value of the character (a number)! How convenient. Who wants to know the ordinal value of the character? I want to see the character. So in Ruby you end up doing this:
  s = 'some string';
  puts s[1].chr;  // converts number to character

So is PHP better since PHP is geared toward handling $strings?

This is just one small factor to consider when choosing a web development environment.. especially since Pascal can handle $strings too, as proven above!

So is PHP better since it is terser and weakly typed?

Pascal more verbose? Slightly in some cases. In other cases it is more neat and PHP is actually more verbose. The WebWriteA function avoids using the dollar sign altogether and allows multiple strongly typed variables to be passed in to a print() style function.

In other words, there are ways to benefit from strongly typed languages by using array of const, or by using format functions and format wrappers.. so that you get the benefits of what weakly typed languages have built in to their language. Strongly typed languages can be verbose at times with casting(), which array of const and embedded variable or format functions can help solve.

Even if you were forced to do casts and you didn't have any options (which you do).. this still wouldn't be a reason to ditch pascal for web development. Even if Pascal is slightly more verbose in some situations, remember that PHP is more verbose in other situations.

Discussing Verbosity

In Powtils we have slightly more verbose syntax than PHP in some cases. But you'd be surprised. Many times PHP ends up being more verbose than pascal, while obfuscated at the same time. Obfuscated code does not mean terse code. Code can be obfuscated and verbose. Code can also be terse and readable.

A simple FOR loop in PHP is very verbose and obfuscated:

for ($i = 10; $i <= 100; $i++) 
{
	echo 'hello';
}

The above loop appears terse since it contains funny characters and symbols. However, it is actually not terse. The pascal loop is terser and more elegant:

for i:= 10 to 100 do
  webwrite('hello');
Count the characters and look at the loops yourself. Which one is more elegant? Which one is terse? Which one requires ridiculous shift-keys for the programmer to hammer in? Why should a prototyping language like PHP be hard for a programmer to use on the keyboard? Powtils loops are easy to type, on the other hand.

Is the Pascal loop terser and less obfuscated at the same time? How can this be so? Again, just because something (PHP) is obfuscated, does not automatically make it terse. Terse means 'short and to the point' which in many cases Pascal is! Terse does not mean obfuscated, since many times obfuscated code is very verbose.

But PHP For Loop is Powerful

The PHP for loop offers some more power, but FOR loops are generally used for doing simple loops. The WHILE loops offer all a person needs for power and flexibility. WHy cram junk into a FOR loop and cause simple FOR loops to be obfuscated? This the problem with PHP, Java, C++, and C loops.

Aside: Ruby loops are much cleaner than PHP loops.. but Ruby offers way too many different loops to use (language complexity).

Since the for loop is used often for simple '1 to abc' or '10 downto abc' operations, it makes sense to implement more complicated loops inside a While loop.. with the added benefit of the while loop being easier to read an understand than some obfuscated ()i++;$ hackery within the PHP/C++ style for loop.

Embedded Variables

The fact that PHP can embed variables into the code everywhere is both an advantage and disadvantage, since it can lead to unmaintainable hodgepodge. Some developers treat their PHP files as web templates instead of clearly thinking about their variables and separating some of their design from logic.

Two Sides To The Story

Many of the so called advantages of php are outweighed by its disadvantages. I'll name a few, but in no way is this a complete list.

Mistakes Not Caught

In PHP you can declare a variable like
  $somevariable = 'blah';
  print $somevariable;
  $somevarable = 'new text';
  print $somevariable
PHP doesn't notice that you made a spelling mistake. Varable or Variable? You may be thinking that programmers are smart enough to find these bugs.

But programmers should be spending time debugging important code instead of debugging simple errors. Almost all bugs in software are caused by stupid mistakes. Experienced programmers know this. Rarely are most bugs caused by an overly complex problem.

Think about the last error you had and how you fixed it. Wasn't it a stupid mistake? Just think back. Simple errors are easily caught in Powtils before you even deploy your program. In PHP, they are not.

Programmers are lazy, and having the compiler catch errors for us saves time so that we can work on more important code. I can hear the smalltalk extreme programmers saying that 'unit tests' would solve the problem. Maybe, maybe not. Well, you can write unit tests with compiled code too, making it even more robust.

(It is a waste of time to write tests that reinvent a compiler.. tests should be saved for areas that a compiler cannot check. Would a PHP programmer ever really be ambitious enough to write unit tests that go through his code and check for simple spelling mistakes and security vulnerabilities? I doubt it. We are lazy humans and I'm not even sure if that could be checked, without reinventing the compiler or Powtils!)

Concatenation

Why concatenate strings with dots in PHP when plus signs are more obvious?

Ugliness?

PHP syntax can quickly become ugly. Even a simple FOR loop contains all sorts of ugly characters such as ($ ; i++) {}.

In Powtils, a for loop is elegant and does not require ();i++{} obfuscation. And no, you are not forced to use IntToStr or FloatToStr functions. There are elegant solutions.

  for i:= 1 to 10 do
    webwrite('hello');

  for i:= 1 to 10 do
    WebwriteA(['loop number: ', i]);

  for i:= 1 to 10 do
  begin
    SetWebVarAsInt('i', i);
    WebwriteF('loop number: $i');
  end;
There are several options and above you see us using $embedded variable in our web program. So who was it that said $variables aren't available in modern Pascal again?

I estimate that the PHP advocate would come back and argue that SetWebVar is more verbose. Well, it causes you to think about what you are doing. You still have other options like WebWriteA, web templates, and many more. SetWebVar allows us to use strong typing, which PHP doesn't have - so I could argue that there is no way to enforce strong typing in PHP.

No language is perfect. Would we choose PHP just because it has $variables built in to the language? There are plenty more factors to consider, such as code maintainability.

More Ugliness

  $urlvariable = $_GET['urlvariable'];
Why does this have to be so ugly, just to get a URL variable? Underscores, dollar signs, capslock.

If we are going to make fun of Pascal for not having embedded $variables then I'm going to go ahead and make fun of PHP too.

Hackers Like Lower Level

Remember that Pascal programs offer fast string parsing abilities too - so for 'string handling' it depends on the need of the programmer. I like writing effecient pascal string parsing algorithms since Pascal can go lower level than PHP and get closer to stack/heap memory where needed. You have more control over the computer with modern Pascal programs. In PHP it is hidden behind the scenes (i.e. setlength, getmem, and custom memory saving tricks are basically all unavailable in PHP).

So in this sense, a hacker could argue Pascal is *better* for string handling if he likes writing wild and spicy efficient algorithms like I do.

Better or Worse

Each language has its disadvantages and advantages. The disadvantage of modern pascal is that it is not a weakly typed scripting language for kids to play with. Or is this an advantage?

I'm continually disgusted by the amount of horribly written PHP scripts I come across. Even popular PHP software such as phpBB, Smarty Template engine, and WordPress contain messes and hodgepodge code. Download take a look at the Smarty source code if you don't believe me.

Do the embedded $variables in php help us make more maintainable software? Sometimes.. at other times it causes bad habits, such as people utilizing PHP as a template engine instead of separating their logic from their design. But, in Pascal, we can use embedded variables too, and use ARRAY of CONST parameters to avoid plus signs in our code. The $macrovars are not the only factor to consider in web development.

With a strongly typed language, you can't always have it both ways.. but with Powtils we've made it so that basically you can have it both ways.. with extra safety and security.


See also Examples-Of-Strong-Typing-Advantages

About
This site is about programming and other things.
_ _ _