When you switched from pedaling a bicycle to driving a car, what was the
hardest thing to learn?
Changing programming languages is exactly the same. This article focuses
on the switch from Visual Basic to PHP. First we look at the bicycle to car
changeover then specific help for VB programmers starting out with PHP.
On my first time in a car, I fount it hard to remember to use the
indicator. There was no equivalent on my bicycle. Braking was even harder.
My bicycle had brake levers on the handlebar. In the car during an
emergency, I had a real urge to squeeze the steering wheel instead of
pushing my foot on the brake pedal. Conclusion: Learning something new is
hard, relearning something old is harder.
Changing language involves the same weird problems. I learned Assembler
then COBOL. I could easily switch between the two because they were so
different. I find Microsoft's Visual Basic harder. There are at least 20
versions of VB. Some versions have sophisticated functions of immense value
but I never use them because I only remember things that work across all
versions of VB.
If changing cars were the same as changing versions of VB, some cars
would be missing brake pedals.
Switching from VB to PHP is half way between the bicycle-to-car switch
and the car-to-car switch. Most things look different so I can remember
which language I am using. One really tricky part is remembering the weird
options on substr(). While substr() is powerful, VBs left(), right() and
mid() are easier to learn. If a programmer has to regularly swap between VB
and PHP, he/she will spend a lot of time looking up the parameters for
substr(). The tip outlined below simply removes the problem.
Here are my favorite examples of instant help:
<?php
Function len($lenstring) {
return(strlen($lenstring));
}
// Neat and simple. It lets me grab the length of
// a string exactly how I would in VB:
$x = len($y);
Function left($leftstring, $leftlength) {
return(substr($leftstring, 0, $leftlength));
}
// It lets me grab 1 or a few characters from
// the front of a string exactly how I would in VB.
Function mid($midstring, $midstart, $midlength="") {
if (len($midlength)) {
return(substr($midstring, $midstring-1, $midlength));
} else {
return(substr($midstring, $midstring-1));
}
}
/*
A little more programming to emulate VB. Mid() is a function I often
use to emulate the sophisticated VB functions that are, sadly, restricted
to only a few versions of VB.
*/
function right($rightstring, $rightlength) {
return(substr($rightstring, -$rightlength));
}
// This is the left function translated for right wingers.
?>
I think you have the idea now. Where something is similar but confusing,
write a function in the new language to emulate the old. In some cases the
process of writing the function will help you learn the new language to the
point where you may never use the emulation function.
Now to get rid of print.
Rather than add small amounts of PHP to HTML pages, I generate whole
pages from within PHP. I spent a lot of time writing:
<?php
print("<p><font face=\"Arial,Helvetica,sans-serif\">Hello from print</font></p>\n");
?>
It seemed logical to remove the print function and go straight to the
HTML tag. That let me think VB plus HTML rather than PHP. The above line
became the function:
<?php
function p($ptext) {
print("<p><font face=\"Arial,Helvetica,sans-serif\">$ptext</font></p>\n");
}
p("hello from p");
?>
If I am going to have some VB and HTML functions in a central file, how
do I get them in to every page? The Apache web server has an option to
append a file to the front of every page. Look up "prepend" in the Apache
documentation then create a file that contains:
<?php
function p($ptext) {
print("<p><font face=\"Arial,Helvetica,sans-serif\">$ptext</font></p>\n");
}
?>
If you cannot access the Apache controls, you can include the file at
the start of every page with PHP include() or PHP require. Read the PHP
documentation for tricks and traps.
Hmmm, we have a common file included in every page. We should split data
from program code. The above p() function should become:
<?php
$standardfont = "face=\"Arial,Helvetica,sans-serif\"";
function p($ptext) {
print("<p><font " . $standardfont . ">$ptext</font></p>\n");
}
?>
There is just one problem. $standardfont is not visible within a
function unless declared global. In VB, constants and variables can be
declared global when declared. In PHP, they have to be declared global in
every function that uses them. Although the PHP approach has merit, it
confuses us poor VB programmers. Why not just avoid the whole problem by
making $standardfont a function:
<?php
function standardfont() {
return("face=\"Arial,Helvetica,sans-serif\"");
}
function p($ptext) {
print("<p><font " . standardfont() . ">$ptext</font></p>\n");
}
?>
Functions are global to other functions. This approach suits people who
think in VB. (People who think "of" VB are likely to be Australians
thinking of a very popular and tasty beer.)
There is one other thing you might note in my examples. PHP lets you
write:
<?php
$x="blue";
print("The ocean is $x\n");
/*
That is an unnatural act in VB. We can
read your PHP code easier if you write:
*/
$x="blue";
print("The ocean is " . $x . "\n");
?>
If you use the . . approach, Homesite will pick up and highlight $x.
Homesite is an extremely popular editor for people working on ASP/VB sites
under NT so is probably what they will use when they start editing PHP.