Guide to PHP and MySQL (University at Buffalo Version)
Chapter 3: PHP Variables and Arrays
PHP Variables
PHP is a loosely typed language. This means you do not have to declare a specific type (string, int, etc.) when creating a variable (unlike other languages such as Java). PHP variables begin with a dollar sign ($
) and the name must start with a letter or an underscore and can contain any number of letters, numbers, or underscores.
You can assign values to variables using the equals sign (=
).
$debug = 0;
$msg = "";
$PHP_SELF = $_SERVER['PHP_SELF'];
// Store today's date in the $today variable
$today = date("Y-m-d H:i:s");
print("This file = $PHP_SELF<br/>\n Today is $today<br/>\n");
?>
The rendered web page would look like this:
Today is 2025-03-19 01:51:15
Assigning Values to a String Variable
You can assign a long string to a variable using different methods. You could simply type the long string. You could also use string concatenaton. Or, you can use the PHP HEREDOC
method. The HEREDOC
method uses three (3) less than characters (<<<
followed by an identifier (a CAPITALIZED simgle word) and then the string with a closing underscore and the identifier on the last line with no space before the underscore. The "identifier" can be any CAPITALIZED word for example: ABC, HERE, END, or WHATEVER, as long as you use the same identifier to end the HEREDOC
(_ABC, _HERE, _END, or _WHATEVER). Here are some examples:
$tableLongString = "<table>\n
<thead>\n
<tr>\n
<th>Employee</th>\n
<th>Department</th>\n
<th>Location</th>\n
</tr>\n
</thead>\n
<tbody>\n";
echo("\$tableLongString:<br/>\n$tableLongString<br/>\n");
$tableConcatString = "<table>\n";
$tableConcatString .= " <thead>\n";
$tableConcatString .= " <tr>\n";
$tableConcatString .= " <th>Employee</th>\n";
$tableConcatString .= " <th>Department</th>\n";
$tableConcatString .= " <th>Location</th>\n";
$tableConcatString .= " </tr>\n";
$tableConcatString .= " </thead>\n";
$tableConcatString .= " <tbody>\n";
echo("<br/>\n\$tableConcatString<br/>\n$tableConcatString<br/>\n");
$tableHeredocString = <<< _END
<table>
<thead>
<tr>
<th>Employee</th>
<th>Department</th>
<th>Location</th>
</tr>
</thead>
<tbody>
_END;
echo("<br/>\n\$tableHeredocString:<br/>\n$tableHeredocString<br/><br/>\n");
?>
The rendered web page would look like this:
<table> <thead> <tr> <th>Employee</th> <th>Department</th> <th>Location</th> </tr> </thead> <tbody>
$tableConcatString:
<table> <thead> <tr> <th>Employee</th> <th>Department</th> <th>Location</th> </tr> </thead> <tbody>
$tableHeredocString:
<table> <thead> <tr> <th>Employee</th> <th>Department</th> <th>Location</th> </tr> </thead> <tbody>
PHP Arrays
Like most languages, PHP provides arrays to store multiple values in a single array variable.
An array in PHP is actually an ordered map. A map is a type that associates values to keys. This type is optimized for several different uses; it can be treated as an array, list (vector), hash table (an implementation of a map), dictionary, collection, stack, queue, and probably more. As array values can be other arrays, trees and multidimensional arrays are also possible. The first element of an array is referenced as the zero (0
element. For example:
$arr = array(6, 8, 2, 1, 4, 10, 3, 13, 11, 7);
for ($i = 0; $i < count($arr); $i++) {
print("Array element $i = $arr[$i]<br/>\n");
}
?>
The rendered web page would look like this:
Array element 1 = 8
Array element 2 = 2
Array element 3 = 1
Array element 4 = 4
Array element 5 = 10
Array element 6 = 3
Array element 7 = 13
Array element 8 = 11
Array element 9 = 7
An associative array allows you to access the array elements using a keyword/value pair. For example:
$statesArray = array("AK" => "Alaska", "AL" => "Alabama", "AR" => "Arkansas", "AZ" => "Arizona");
foreach ($statesArray as $state => $stateValue) {
print("Array element $state = $stateValue<br/>\n");
}
?>
The rendered web page would look like this:
Array element AL = Alabama
Array element AR = Arkansas
Array element AZ = Arizona
Sorting PHP Arrays
PHP provides a many functons for sorting arrays. The sort()
function sorts an array in ascending order and the rsort()
function sorts an array in reverse order.
$states = array("Ohio","Florida","Massachusetts","Montana");
echo("<b>Before sort:</b> ");
print_r($states);
sort($states);
echo("<br/>\n<b>After sort:</b> ");
print_r($states);
?>
sort()
to Sort an ArrayAfter Sort: Array ( [0] => Florida [1] => Massachusetts [2] => Montana [3] => Ohio )
$states = array("Ohio","Florida","Massachusetts","Montana");
echo("<b>Before sort:</b> ");
print_r($states);
rsort($states);
echo("<br/>\n<b>After sort:</b> ");
print_r($states);
?>
rsort()
to Reverse Sort an ArrayAfter Sort: Array ( [0] => Ohio [1] => Montana [2] => Massachusetts [3] => Florida )
Task - Create Your First PHP Web Page
For this task you will create aa new folder (directory) on your localhost
web server called cda215
and inside that folder create a web page called index.php
which will be used as your web server starting page for the rest of your tasks.
You will also create a sub-folder file called includes
which will be used to store your CSS, JavaScripyt, and assorted PHP files. In your includes
directory you will create a vars.php
file which will be used to store the following values:
- Web Page Title
- Web Page Name
- Your Name
- Web Server
- Web Page Editor Software
For example: $title = "Jim Gerland's PHP Page";
Your web page should:
- Use a PHP include statement for your vars.php file
- Utilize the following HTML tags and include the following:
- Appropriate
<!doctype html>
,<html>
, and<head>
tags
- Appropriate
- Use PHP to echo a
<title>
tag to give your page a title that describes your page - Use PHP to echo appropriate
<meta>
tags to provide keywords for SEO on your page (author, software editor, etc.) - A
<body>
tag - Use PHP to echo an
<h1>
tag that contains your web page name - A
<p>
tag that encloses a brief paragraph about yourself - At least one of each of these tags:
<hr>
,<br>
- Use CSS in place of any
<strong>
(<b>
) or<em>
(<i>
) tags - Use PHP code to display the current Date and time
- Use a linked CSS styles file to control and enhance the look and feel of your web page. Your page should have easy to use navigation and be easy to read using appropriate styles.
You *MUST* use the W3C Unicorn Validator to validate your HTML5/CSS3 code.
Then upload both your index.php
and vars.php
files to your localhost web Space and add <a>
(anchor) tags that utilize the 'download.php
' for the list entry for this assignment that allows me to download both your index.php
and vars.php
files to my web server.