All chapters...Table of contents
A simple HTML document
Cascading style sheets
Programming in PHP
Server side includes
CGI & Perl
Examples of colours
In this chapter...Embedding PHP
Printing date and time
PHP is multilingual
Date of the current page
Date of another page
Date dependent output
Taalkeuze...U hebt gekozen om Engels als eerste taal te gebruiken. Klik hier om te kiezen voor Nederlands.
Programming in PHP
PHP, which stands for "PHP: Hypertext Preprocessor," is a widely-used Open Source general-purpose scripting language that is especially suited for Web development and can be embedded into HTML. Full documentation of PHP can be found at www.php.net
Including PHP in your webpages is simple. One most webservers, you will need to give your file and .php extension rather than a .html one. That will tell the webserver to process the file with PHP. You can mix PHP and HTML like this:
The time is: <?php echo time() ?> seconds since 1970...
The time is: 1597279202 seconds since 1970...
The time you see now is the Unix time stamp, seconds since 1970. Longer code can be mixed with HTML in this way:
The table of seven:<BR>
The table of seven:
<?php tells PHP that code starts there, ?> indicates the end of code. In the first example you see the use of time(), one of the many build-in functions of PHP. This one gives the time in seconds since 1970, the Unix timestamp. The echo and print functions do essentially the same, they output to the HTML page. The second example shows the use of a variable names $i.
Every line in a PHP script ends with a semicolon, unless there is only one line in a script. A viewer of your page cannot see anything that is between <?php and ?>, also not when using the "view source" option in a browser. Only the output of a print or echo function will appear in a web page.
Welcome, today is <?php echo strftime("%A") ?> and the time is <?php echo strftime("%R") ?>.
Welcome, today is Thursday and the time is 02:40.
The strftime() function produces dates and times. It takes at least one argument, a string to format the date. You have many options, here are some of the most important ones:
You can combine as many of these in one strftime() command as you wish:
<?php print "Today is: " . strftime("%a, %e %B '%y and the time is %H:%M:%S") ?>
Today is: Thu, 13 August '20 and the time is 02:40:02
The strftime() function normally outputs the time at the moment it is called. You can give the strftime() function a second argument, the time in seconds since January 1, 1970. This is the standard Unix way of describing the time. For example, December 13, 2003 is 1071270000:
<?php print "Another date was " . strftime("%A, %d %B %Y", 1071270000) ?>
Another date was Saturday, 13 December 2003
Thursday 13 August
This is especially practical if your site is multilingual.
<?php print "This page last updated: " . strftime ("%A %d %B %Y", getlastmod()) ?>
This page last updated: Saturday 02 June 2012
Remember that once you upload your file by FTP to a web server, you are like to see the date you last uploaded the file, not the date you actually modified it.
index.php was last modified: Saturday 02 June 2012
This small script first uses the file_exists() function to verify the file actually exists. If the file exists, the filemtime() function is called to get the date the file was last modified. You can only use these filesystems function for information on files that are on the same server as your script.
Special offer: 6 for the price of 5
In both examples you see the use of the "if, elseif, else" control structure. If and elseif are followed by a logical expression. Else is what happens if none of the logical expressions is true. You can use as many "elseif" structures as you want to.
In the chapter on forms I use a simple script that processes the output of all the forms and prints them into a table. This is how the script looks:
<HTML><HEAD><TITLE>Form output tester</TITLE></HEAD><BODY>
The script makes use of the predefined variable $_POST. $_POST is an array. If a form uses the METHOD="post" attribute, all input from the form will be in the $_POST array. For example, if you have a text field named "email" than that input can be found in $_POST['email'].
Because $_POST is an array, you can use the foreach control structure to get the contents of the array. foreach takes every value in $_POST and, in the example above, stores the name in $i and the value in $j. This technique makes the script very flexible and enables it to capture the input from any form, irrespective of the number of fields it has and what they are called.
The script uses two build-in functions of PHP to for security. The strip_tags() function removes all tags, PHP and HTML. This avoids malicious users from including scripts or risky tags in the form. A second security measure is to maximize the length of the input. This is done with the substr() function. This function takes three arguments. The first is a string, the second is the starting point and the third is the lenght of the string to take. For the starting point 0 means the first character. So $i is cut to a maximum length of 20 characters and $j to a maximum of 255 characters. As you can see from this example you can nest functions without problems.
Finally, the script uses the print function. You can see how the two variables $i and $j can be included in a string and will print properly.
This is a very basic example of processing a form. In real life you will want to validate the input, for example check if all obligatory fields have been filled out. Then you want to do something with the input, like e-mail it to yourself of store it in a database.
This site conforms completely to open standards of the World Wide Web Consortium (W3C). Click on the logos to verify that.
©1995-2020 Jan Weijers, the Netherlands. All rights reserved.