help files
Z505 | PasWiki | FUQ | Search | Main Docs | API Guide



Notes

Migrating code and noting differences with php.

Differences between PHP and Powtils

How to migrate PHP code over to Powtils, and/or vice versa. This page will be updated with time - it is in no way complete yet.

Template engines like Smarty

The current WebTemplateOut function in powerful web utilities is a simple core function. It allows you to use $macrovariables. Currently the Powtils developers are working on a more complex templating system that will be an extra package you can download and use with Powtils.

PHP has MySQL access..What about Powerful web utils?

There are about 100 different ways of accessing a database with modern pascal, such as With Powtils you actually have more options than in PHP - this can be overwhelming. At some point the Powtils CORE may contain a clean mysql/firebird/postreg database access unit - however since there are so many external units to access databases we have currently left that choice up to you.

Extending PHP vs Extending Powerful Web Utils

Anyone can create add-ons and extensions around the core Powtils.

Even better, one can utilize existing source code from old turbo pascal programs, delphi programs, freepascal programs etc. Some units such as CRT.pas are not compatible with web programming since they make use of special console functions that aren't available in a web browser (one could go as far as making a webCRT.pas with this functionality if they wished).

In PHP, there is no way to access existing C libraries easily (importing from C DLL's). One can make extensions to the PHP language, but with Powtils your power is limitless. You can use existing C dll's or dso's, you can use existing Pascal code from desktop programs, you can use Pascal dll's, you can use operating system dll's, etc.

How to fake Sets in PHP

PHP isn't as elegant as Modern Pascal when it comes to sets. Often one will use a regular expression in PHP instead of using a set - but this calls an expensive regex interpretter library which in many cases is overkill, and generally bad habit (it is a bad habit to utilize regexes for everything and everything). Here is how you can fake a set of characters in PHP without using a Regex::
  // alpha numeric, like 'a'..'z', 'A'..'Z', '0'..'9' in Pascal
  $AlphNum = array('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','x','y','z',
                   'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','X','Y','Z',
                   '0','1','2','3','4','5','6','7','8','9','0' );
  if ( in_array('a', $AlphNum) ) { 
      echo 'a found <br>'; 
  } else { 
      echo 'a not found <br>'; } 
  if ( in_array('x', $AlphNum) ) { 
      echo 'x found <br>'; 
  } else { 
      echo 'x not found <br>'; } 
  if ( in_array('#', $AlphNum) ) { 
      echo '# found <br>'; 
  } else { 
      echo '# not found <br>'; 
  } 
  if ( in_array('ab', $AlphNum) ) { 
      echo 'ab found <br>'; 
  } else { 
      echo 'ab not found <br>'; 
  }
The above is kind of messy, but it is the way PHP language is designed. PHP was generally designed so that regular expressions could be used whenever something like the above situation was needed. That causes bad habits. It is just the syntax and style they offer, and nothing we can do about it.

In Pascal there is a more elegant XYZ in SET syntax.. for example:

Note: set<..> rather should contain square brackets, lufdoc parser needs to be updated.

const
  AlphNum = <'a'..'z', 'A'..'Z', '0'..'9'> ; // alpha numeric characters
begin
  if 'a' in AlphNum then 
    webwrite('a found <br>') 
  else 
    webwrite('a not found <br>');
  if 'x' in AlphNum then 
    webwrite('x found <br>') 
  else 
    webwrite('x not found <br>');
  if '#' in AlphNum then
    webwrite('# found <br>') 
  else 
    webwrite('# not found <br>');
  if 'ab' in AlphNum then 
    webwrite('ab found <br>') 
  else 
    webwrite('ab not found <br>');

For Loops in PHP vs For Loops in Powtils

PHP:
  $str1 = 'testing'.' testing';
  for ($i = 0; $i < strlen($str1) - 1; $i++ )
  {
      if ($str1<$i> == 't') { echo 't found'; } // str1<$i> should contain square brackets, lufdoc parser needs to be updated
  }
Not really elegant. Lots of symbols, lots of funny looking stuff just to do something simple. However, that's how they designed the language and that is how it works. Here is the difference in Powerful Web Utilities:
var
  str1: string;
  i: integer;
begin
 str1 = 'testing' + ' testing';
 for i:= 0 to length(str1) do
    if str1<i> = 't' then webwrite('t found');  // str1<i> should contain square brackets, lufdoc parser needs to be updated
end.
Strings in PHP act like zero based arrays of characters (starting at string<0>, so you have to subtract one when looping to the end of the string with strlen(). In Pascal strings are one based arrays (starting at string<1>).

Assigning procedures, passing procedures as parameters

In Pascal, one can assign a procedure to point to some other procedure by using a variable to do so. In Pascal it is also clear and elegant to create function which can then be passed in to another procedure or function as an argument.
  procedure (s: string; sort: procedure);
  begin
     s:= 'test';
     sort; // call custom sort procedure passed in as parameter
     sort:= @somethingelse; //assign the sort procedure to something else
     // do more stuff
  end;
In PHP, it is not as elegant or clean - one cannot assign a sort procedure to a variable. More awkwardly, a variable is assigned to a string, which acts like a function if brackets are used to launch the function. It is possible and do-able in a hackish sort of way.
  function foo($x) 
  {
      echo "x is $x <br>";
  }
  function test($func) 
  {
      $func(10);
  }
  // backwards.. we aren't assigning the procedure, we are assigning a variable
  $foo = "foo"; // foo is a string, but it can also reference the foo function
  $foo(5);  // Output: x is 5  we are launching the foo function
  test($foo); // Output: x is 10
  echo $foo; // outputs "foo" string, does not try to launch foo()
In PHP, AFAIK there is no direct/elegant way to:
  test = somefunction; //can't do it
  test = @someotherfunction // can't do it
The behavior of PHP is odd, and in my opinion much better design choices could have been made for that language. However, that's the way they designed it, and people put up with it. When migrating code or learning about the differences between Powtils and PHP, it is important to have knowledge about these subjects.

In PHP, one might consider it Voodoo or Weird or Very Advanced to pass in a function as a parameter, or assign a function to point to some other function. Modern Pascal programmers consider it an intermediate thing or even common knowledge. Another way to achieve similar behavior is to use an object and reference self.method inside the function - however sometimes object abuse occurs and it is better to pass in a simple function argument.

Other migration tips

This is not a complete migration guide yet, check back for more tips soon.

Basic rule of thumb

The less dollar signs there are in your code, the more money you will make in a programming career.






lufdoc, Powtils, fpc, freepascal, delphi, kylix, c/c++, mysql, cgi web framework docs, Z505