- Before You Begin: Accessing PHP
- Creating a Sample Application: Bob's Auto Parts
- Embedding PHP in HTML
- Adding Dynamic Content
- Accessing Form Variables
- Understanding Identifiers
- Examining Variable Types
- Declaring and Using Constants
- Understanding Variable Scope
- Using Operators
- Working Out the Form Totals
- Understanding Precedence and Associativity
- Using Variable Handling Functions
- Making Decisions with Conditionals
- Repeating Actions Through Iteration
- Breaking Out of a Control Structure or Script
- Employing Alternative Control Structure Syntax
- Using declare
Adding Dynamic Content
So far, you haven’t used PHP to do anything you couldn’t have done with plain HTML.
The main reason for using a server-side scripting language is to be able to provide dynamic content to a site’s users. This is an important application because content that changes according to users’ needs or over time will keep visitors coming back to a site. PHP allows you to do this easily.
Let’s start with a simple example. Replace the PHP in processorder.php with the following code:
echo "<p>Order processed at ";
echo date('H:i, jS F Y');
You could also write this on one line, using the concatenation operator (.), as
echo "<p>Order processed at ".date('H:i, jS F Y')."</p>";
In this code, PHP’s built-in date() function tells the customer the date and time when his order was processed. This information will be different each time the script is run. The output of running the script on one occasion is shown in Figure 1.3.
Figure 1.3 PHP’s date() function returns a formatted date string
Look at the call to date(). This is the general form that function calls take. PHP has an extensive library of functions you can use when developing web applications. Most of these functions need to have some data passed to them and return some data.
Now look at the function call again:
date('H:i, jS F')
Notice that it passes a string (text data) to the function inside a pair of parentheses. The element within the parentheses is called the function’s argument or parameter. Such arguments are the input the function uses to output some specific results.
Using the date() Function
The date() function expects the argument you pass it to be a format string, representing the style of output you would like. Each letter in the string represents one part of the date and time. H is the hour in a 24-hour format with leading zeros where required, i is the minutes with a leading zero where required, j is the day of the month without a leading zero, S represents the ordinal suffix (in this case th), and F is the full name of the month.
For a full list of formats supported by date(), see Chapter 19, “Managing the Date and Time.”