PHP Tips


PHP
 is a programming language mainly developed for use with websites or browsers, but can do most anything any other programming language can do. The syntax highlighting was created using the built-in ability of PHP using my NoteTab “PHP Syntax Highlighting” Clip. Unfortunately, the HTML generated by PHP uses the deprecated font tag.

Here are the useful PHP snippets I have developed:

Here is the PHP command line used to generate the PHP syntax highlighting:

c:\php4\php.exe -q -s c:\mywebsite\webpage.php > c:\php4\webpage.htm

  • The following script shows how to display information conditionally by date. This is taken from the genealogy site I web master. Since I added this, I do not have to comment and uncomment the line about Family History Month. If all you have is one item to display, the else section can be omitted. It is shown here for those needing a more complete example. If you have a site with multiple events on a regular basis, you can use this script. The code it displays can be links, if you have links to pages you want displayed on a certain schedule.
<-- Copy below this line. -->
<?php
$month = date("F");
if ($month == "October")
    print "October is Family History Month.";
else
    print "Other information.";
?>
<-- Copy above this line. -->
  • The Following is a PHP script to read a file of facts or quotes, with one per line, and display them randomly.
<-- Copy below this line. -->
<?php
$lines = file('facts.txt');

// Loop through array
foreach ($lines as $line_num => $line) {
}
srand( ((int)((double)microtime()*1000003)) );
$rand_keys = array_rand($lines, 1);
echo $lines[$rand_keys] . "\n";
?>
<-- Copy above this line. -->
  • Here is the standard PHP code to test your PHP installation on your website. CAUTION: Do not leave this script on your site. This reveals critical information that could be used to crack your site.
<-- Copy below this line. -->
<?php
phpinfo()
?>
<-- Copy above this line. -->