Difference between revisions of "Learning PHP (2)"
Karl Jones (Talk | contribs) (→Date) |
Karl Jones (Talk | contribs) |
||
Line 116: | Line 116: | ||
* [http://php.net/manual/en/function.date.php DateTime In PHP] | * [http://php.net/manual/en/function.date.php DateTime In PHP] | ||
* [http://code.tutsplus.com/tutorials/dates-and-time-the-oop-way--net-35395 Dates and Time - The OOP Way] | * [http://code.tutsplus.com/tutorials/dates-and-time-the-oop-way--net-35395 Dates and Time - The OOP Way] | ||
+ | |||
+ | [[Category:Learning]] | ||
+ | [[Category:PHP]] |
Latest revision as of 06:48, 23 April 2016
This article contains examples of PHP.
See also Learning PHP (1).
Contents
Timestamp
PHP provides the time
function, which returns the time (from the server clock).
The time can be formatted and used in a variety of ways.
Code snippet:
<p><?php echo time(); ?></p>
Observe how the above code snippet mixes HTML with PHP code islands.
Use your browser's View Source or Inspect Element to confirm that the web page's source code contains only HTML, no PHP.
See [time] for documentation, Timestamp for general information.
Date
PHP provides the date()
function, which returns date and time.
The date and time can be formatted and used in a variety of ways.
Code snippet:
<p><?php echo date("Y/m/d"); ?></p> <p><?php echo date("l"); ?></p>
Observe how the above code snippet mixes HTML with PHP code islands.
Use your browser's View Source or Inspect Element to confirm that the web page's source code contains only HTML, no PHP.
See PHP date for more information.
HTML special character handling
Warning: this topic is critical to web security.
Always use htmlspecialchars (or some equivalent technology) when processing user input. Always, always, always.
PHP provides a function named htmlspecialchars which handles special HTML characters.
Handles, in this case, includes replacing dangerous HTML with safe HTML.
See PHP htmlspecialchars function.
See Online example
GET method
PHP can read query strings submitted by get requests -- that is, request sent using the get method.
The the $_GET[ key ]
array contains all of the query string keys and values.
Remember to use htmlspecialchars for web security.
<?php echo htmlspecialchars ( $_GET['firstname'] ) ; echo "<br />" ; echo htmlspecialchars ( $_GET['email'] ) ; ?>
POST method
PHP can read query strings submitted by post requests -- that is, requests sent using the post method.
The the $_POST[ key ]
array contains all of data received from a web form.
Remember to use htmlspecialchars for web security.
<?php echo htmlspecialchars ( $_POST['firstname'] ) ; echo "<br />" ; echo htmlspecialchars ( $_POST['email'] ) ; ?>