Guide to PHP and MySQL (Buffalo State College Version)
Chapter 3: PHP Variables and Arrays
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 (
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:
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:
An associative array allows you to access the array elements using a keyword/value pair. For example:
Sorting PHP Arrays
Task - Create Your First PHP Web Page
For this task you will create aa new folder (directory) on your
localhost web server (and the
bscacad3 server) called
cis475 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
$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:
- 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.)
- Use PHP to echo an
<h1>tag that contains your web page name
<p>tag that encloses a brief paragraph about yourself
- At least one of each of these tags:
- Use CSS in place of any
- 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
vars.php files to your bscacad3 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
vars.php files to my web server.