PHPBuilder - Switching From VB to PHP Page 3



RSS Twitter
Articles Tricks And Hacks

Switching From VB to PHP - Page 3

by: Peter
|
July 30, 2000

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");
}

?>

« Previous Page
1
|
2
|
3
|
4
Next Page »

Comment and Contribute

Your comment has been submitted and is pending approval.

Author:
Peter

Comment:



Comment:

(Maximum characters: 1200). You have characters left.