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 = "";
// 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");

Figure 3-1: PHP Variables Code

The rendered web page would look like this:

This file = /phpguide/ch03.php
Today is 2018-07-23 12:57:30

Figure 3-2: Print Variables Output

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
$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";
$tableHeredocString = <<< _END

Figure 3-3: PHP String Variables Code

The rendered web page would look like this:

<table> <thead> <tr> <th>Employee</th> <th>Department</th> <th>Location</th> </tr> </thead> <tbody>

<table> <thead> <tr> <th>Employee</th> <th>Department</th> <th>Location</th> </tr> </thead> <tbody>

<table> <thead> <tr> <th>Employee</th> <th>Department</th> <th>Location</th> </tr> </thead> <tbody>

Figure 3-4: Print String Variables Output

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");

Figure 3-5: A PHP Array Code

The rendered web page would look like this:

Array element 0 = 6
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

Figure 3-6: Print Array Output

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");

Figure 3-7: A PHP Associative Array Code

The rendered web page would look like this:

Array element AK = Alaska
Array element AL = Alabama
Array element AR = Arkansas
Array element AZ = Arizona

Figure 3-8: Print Associative Array Output

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> ");
echo("<br/>\n<b>After sort:</b> ");

Figure 3-9: Using sort() to Sort an Array
Before Sort: Array ( [0] => Ohio [1] => Florida [2] => Massachusetts [3] => Montana )
After Sort: Array ( [0] => Florida [1] => Massachusetts [2] => Montana [3] => Ohio )

Figure 3-10: Sort an Array Output
$states = array("Ohio","Florida","Massachusetts","Montana");
echo("<b>Before sort:</b> ");
echo("<br/>\n<b>After sort:</b> ");

Figure 3-11: Using rsort() to Reverse Sort an Array
Before Sort: Array ( [0] => Ohio [1] => Florida [2] => Massachusetts [3] => Montana )
After Sort: Array ( [0] => Ohio [1] => Montana [2] => Massachusetts [3] => Florida )

Figure 3-12: Reverse Sort an Array Output

Task - Create Your First PHP Web Page

For this task you will create aa new folder (directory) on your localhost web server called mfc211 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:

  1. Web Page Title
  2. Web Page Name
  3. Your Name
  4. Web Server
  5. Web Page Editor Software

For example: $title = "Jim Gerland's PHP Page";

Your web page should:

  1. Use a PHP include statement for your vars.php file
  2. Utilize the following HTML tags and include the following:
    • Appropriate <!doctype html>, <html>, and <head> tags
  3. Use PHP to echo a <title> tag to give your page a title that describes your page
  4. Use PHP to echo appropriate <meta> tags to provide keywords for SEO on your page (author, software editor, etc.)
  5. A <body> tag
  6. Use PHP to echo an <h1> tag that contains your web page name
  7. A <p> tag that encloses a brief paragraph about yourself
  8. At least one of each of these tags: <hr>, <br>
  9. Use CSS in place of any <strong> (<b>) or <em> (<i>) tags
  10. Use PHP code to display the current Date and time
  11. 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.

Let's Get Started with a PHP Variables Page!
Let's Get Started with a PHP Arrays Page!
Creative Commons License This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License. Copyright © 2016-2018 Jim Gerland