| |||||||||||||||||||||||||||||||||||||||||||||||
|
All chapters... Table of contentsIntroduction A simple HTML document Images Lists Links Tables Frames Forms Cascading style sheets JavaScript Programming in PHP Server side includes CGI & Perl Extended characters Examples of colours Learn more Software Recent changes Alphabetical index In this chapter...
Embedding PHPPrinting date and time PHP is multilingual Date of the current page Date of another page Date dependent output Processing forms
Taalkeuze...
U hebt gekozen om Engels als eerste taal te gebruiken. |
Programming in PHPPHP, 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 Embedding PHPIncluding 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: 1283863268 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. Printing date and timePHP is very useful to make basic dynamic elements in your page such as printing the date and the time: Welcome, today is <?php echo strftime("%A") ?> and the time is <?php echo strftime("%R") ?>. Welcome, today is dinsdag and the time is 14:41. 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: di, 7 september '10 and the time is 14:41:08 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 zaterdag, 13 december 2003 PHP is multilingualPHP knows the names of days and months in many languages. You can use the set_locale() function to chose one:
<?php
dinsdag 07 september This is especially practical if your site is multilingual. Date of the current pagePHP provides a handy function to get the modification date of the current page. The getlastmod() function returns a Unix timestamp which you can feed to the strftime() function: <?php print "This page last updated: " . strftime ("%A %d %B %Y", getlastmod()) ?> This page last updated: Monday 28 April 2008 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. Date of another pageTo get the modification date of another file, you need to do some more work:
<?php index.php was last modified: Monday 28 April 2008 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. Date dependent outputUsing the date and time functions of PHP you can make parts of your web pages dependent on de date. Consider these two examples:
<?php Good afternoon
<?php 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. Processing formsIn 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.
|
||||||||||||||||||||||||||||||||||||||||||||||
|
Feedback welcome...
Open standards...
This site conforms completely to open standards of the World Wide Web Consortium (W3C). Click on the logos to verify that.
| |||||||||||||||||||||||||||||||||||||||||||||||
|
©1995-2010 Jan Weijers, the Netherlands. All rights reserved.
| |||||||||||||||||||||||||||||||||||||||||||||||