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