Usual PHP file is practically an HTML text with included PHP scripts. To include a PHP script inside an HTML document
we need to write all the PHP commands we want to run between <?php and ?>. Also there is
a shorter form of the opening construction with word php omitted:
usual HTML commands<?PHP commands?>again HTML commands
Like JavaScript we can include as many PHP script in our document as we want. However, there are two important differences
between JS and PHP:
JavaScript is to be run on the client computer, while PHP script are run on the server (we are not
talking about server side JavsScript in this course)
files with PHP instructions in them should have special extensions that are defined by the web server settings
(usually they are: php, php3, php4, and/or phtml). If you put a PHP
script with a file with extension html you end up viewing the code you typed in, not the result
of the code.
We also would like to introduce a simple PHP command echo. This command prints its arguments.
Thus, the following three lines of code produce <h1>Hello</h1> in the result file.
echo "<h1>";
echo "Hello";
echo "</h1>";
Comments in PHP language
Like always we start talking about new script language by introducing the most important part of any language --
comments. PHP supports three types of comments:
// comment everything up to the end of the line
# comment everything up to the end of the line
multiline comments. Everything between the start comment /* and the end comment
*/ will be ignored by the PHP interpreter.
Example:
<?
// this line is commented
echo "Hello";
/* these three
lines are also
commented */
?>
Variables in PHP scripts
Variable in PHP are very much like ones in JavaScript. Except one small detail --
the names of all variables are to start with $. To describe a new variable
all you need to do is to initialize it by usual assignment operator:
$a = "this is a string";
$b = 12; // this is going to be an integer variable
$c = 12.4 # this is double
You do not need to specify type of a new variable. It will be chosen automatically depending on the value you assigned to
the variable. You don't even have to say var in front of a new variable like we did in JavaScript. If you need
a new variable to store some value just assign this value to the variable.
However, with this approach we sometimes need to make sure that a particular variable was defined. There are several
functions that work with variables two of them are very simple (but still useful):
isset() -- takes a variable as an argument and returns true if the variable is defined and
false otherwise
empty() -- does completely the opposite job. Takes a variable name as an argument and returns
true if this variable is not defined, false otherwise.
Function unset() removes a variable passed as an argument from the memory. After calling
unset($name);
variable $name no longer exists.
Function gettype() takes a variable as its argument and returns a string containing a type of the
variable. For example, the following PHP code
prints types of each declared variable according to the value we assigned to it. In this example also
please pay attention that the string each echo command is printing contains three parts -
two constant strings and result returned be the gettype() function. To add all three pieces
together we use dot operator, which concatenates two strings into one.
Passing arguments to PHP scripts
Now we would like to discuss how we can pass variables from an HTML form into an
PHP script. To do this we have to do several things:
specify the name of the script we want to pass data in the form's parameter action
name each element inside the form (using name parameter of HTML tags)
set parameter method to POST if we need to "hide" information from user
If we did only the first two of these steps, then inside the script we can access each argument by
accessing $_REQUEST["argument_name"]. If we used method POST, then
we can access arguments by $_POST["argument_name"]. Where
argument_name is the name of the argument which value you want to get typed exactly
the same way you did it in the HTML form (PHP is case sensitive).
Let's consider a simple example. We'll create a small HTML form with only one input text element and
a submit button. We will name the element myname and specify action parameter
of the form as script.php:
<form action="script.php">
Enter your name: <input type=text name="myname">
<input type=submit value="Execute script">
</form>
Then script.php may look like this:
<html>
<body>
<?
$myname = $_REQUEST["myname"];
if( isset($myname) )
print "Your name is $myname ";
else
print "You are too lazy to type your name!";
?>
</body>
</html>
In this section we will not discuss why we should access the arguments passed to PHP script by
accessing such strange things as $_REQUEST or $_POST. We will postpone this discussion
till the moment we'll get to know what PHP arrays are.
Here you can find an example that shows how to pass data
not only from input text element, but also from select list, radio buttons, and check boxes.
Date and time functions in PHP.
Sometimes our PHP script has to know the current date and/or time to behave properly. PHP has
more than 10 different functions to work with
date and time. In this section we will discuss only three of them:
time() - does not require any arguments and returns current UNIX timestamp. In other
words, it returns the number of seconds since the Unix Epoch (January 1 1970 00:00:00 GMT).
date() - takes two arguments (the second one is optional). The first argument
of the date() function is a string containing format of the date. The function returns a
string with the current date and time formatted accordingly the format string. Optional second argument
can give the timestamp of the moment you want to print date/time about. For example, the following
two lines of code print the current month, day, and year and also time for the moment with timestamp
70000:
The following characters are recognized in the format string:
a - "am" or "pm"
A - "AM" or "PM"
B - Swatch Internet time
d - day of the month, 2 digits with leading zeros; i.e. "01" to "31"
D - day of the week, textual, 3 letters; e.g. "Fri"
F - month, textual, long; e.g. "January"
g - hour, 12-hour format without leading zeros; i.e. "1" to "12"
G - hour, 24-hour format without leading zeros; i.e. "0" to "23"
h - hour, 12-hour format; i.e. "01" to "12"
H - hour, 24-hour format; i.e. "00" to "23"
i - minutes; i.e. "00" to "59"
I (capital i) - "1" if Daylight Savings Time, "0" otherwise.
j - day of the month without leading zeros; i.e. "1" to "31"
l (lowercase 'L') - day of the week, textual, long; e.g. "Friday"
L - boolean for whether it is a leap year; i.e. "0" or "1"
m - month; i.e. "01" to "12"
M - month, textual, 3 letters; e.g. "Jan"
n - month without leading zeros; i.e. "1" to "12"
O - Difference to Greenwich time in hours; e.g. "+0200"
r - RFC 822 formatted date; e.g. "Thu, 21 Dec 2000 16:01:07 +0200" (added in PHP 4.0.4)
s - seconds; i.e. "00" to "59"
S - English ordinal suffix for the day of the month, 2 characters; i.e. "st", "nd", "rd" or "th"
t - number of days in the given month; i.e. "28" to "31"
T - Timezone setting of this machine; e.g. "EST" or "MDT"
U - seconds since the Unix Epoch (January 1 1970 00:00:00 GMT)
w - day of the week, numeric, i.e. "0" (Sunday) to "6" (Saturday)
W - ISO-8601 week number of year, weeks starting on Monday (added in PHP 4.1.0)
Y - year, 4 digits; e.g. "1999"
y - year, 2 digits; e.g. "99"
z - day of the year; i.e. "0" to "365"
Z - timezone offset in seconds (i.e. "-43200" to "43200"). The offset for timezones west of UTC is always negative, and for those east of UTC is always positive.
getdate() - takes only one argument (optional) a timestamp. Returns an
associative array (i.e. index with strings not numbers) containing the date information of the
timestamp, or the current local time if no timestamp is given, as the following array elements:
"seconds" - seconds
"minutes" - minutes
"hours" - hours
"mday" - day of the month
"wday" - day of the week, numeric : from 0 as Sunday up to 6 as Saturday
"mon" - month, numeric
"year" - year, numeric
"yday" - day of the year, numeric; i.e. "299"
"weekday" - day of the week, textual, full; i.e. "Friday"
"month" - month, textual, full; i.e. "January"
Thus, the following example prints the date and time exactly 7 week ago: