The Building Blocks: Data Types, Literals, Variables, and Constants - Part 2/Page 2 | WebReference

The Building Blocks: Data Types, Literals, Variables, and Constants - Part 2/Page 2

[previous] [next]

The Building Blocks:
Data Types, Literals, Variables, and Constants - Part 2

Declaring and Initializing Variables

Variables are normally declared before they are used. PHP variables can be declared in a script, come from an HTML form, from the query string attached to the script's URL, from cookies, from the server, or from the server's environment. Variable names are explicitly preceded by a $. You can assign a value to the variable (or initialize a variable) when you declare it, but it is not mandatory.


To declare a variable called firstname, you could say:

You can declare multiple variables on the same line by separating each declaration with a semicolon. For example, you could say:

Double, Single, and Backquotes in Assignment Statements. When assigning a value to a variable, if the value is a string, then the string can be enclosed in either single or double quotes; if the value is returned from a function, then the function is not enclosed in quotes; and if the value is returned from a system command (see "Execution Operators" on page 143), then the command is enclosed in backquotes:

Example 4.8


  1. The variable called $name is defined and initialized within the string value "Joe Shmoe". The string can be enclosed in either single or double quotes.
  2. The variable called $age is assigned the floating-point value, 25.4. When assigning a number, the value is not quoted.
  3. The variable called $now is assigned the return value of the built-in date() function. The function is not enclosed in quotes or it will not be executed. Its arguments, "m/d/Y", must be a string value, and are enclosed in quotes.
  4. The variable $nothing is not assigned an initial value; it will have the value NULL.
  5. The string is enclosed in double quotes. The floating-point value of $age is evaluated within the string.
  6. The gettype() function tells us that the type of $nothing is NULL; that is, it has no value.
  7. The output of the PHP built-in date() function was assigned to $now and is printed (see Figure 4.10).

Figure 4.10 With or without quotes. Output from Example 4.8.

Example 4.9


  1. The UNIX/Linux cal command and its arguments are enclosed in backquotes (also called backticks). In PHP the backquotes are actually operators (see "Execution Operators" on page 143). The command is executed by the operating system. Its output will be assigned to the variable, $month.
  2. The PHP code is embedded within HTML <pre> tags to allow the calendar, $month, to be displayed in its natural format (see Figure 4.11).

Figure 4.11 Backquotes and UNIX. Output from Example 4.9.

Example 4.10

Figure 4.12 Backquotes and Windows. Output from Example 4.10.

Displaying Variables

The print and echo Constructs. So far, we have seen examples using both print and echo to display data. These language constructs can be used interchangeably. The only essential difference between echo() and print() is that echo allows multiple, comma-separated arguments, and print doesn't. Neither require parentheses around their arguments because technically they are not functions, but special built-in constructs. (In fact, arguments given to echo must not be enclosed within parentheses.) To print formatted strings, see printf and sprintf in Chapter 6, "Strings."

Consider the following. Three variables are declared:

echo() can take a comma-separated list of string arguments:

print() takes one string argument:

However, the concatenation operator can be used to print multiple strings or strings containing multiple variables:

or all of the variables can be enclosed in double quotes:

If a variable is enclosed in double quotes, it will be evaluated and its value displayed. If enclosed in single quotes, variables will not be evaluated. With single quotes, what you see is what you get. Like all other characters enclosed within single quotes, the $ is treated as a literal character.

The following strings are enclosed in single quotes:

The same strings are enclosed in double quotes:

Shortcut Tags. There are several shortcuts you can use to embed PHP within the HTML portion of your file, but to use these shortcuts, you must make a change in the php.ini file. (If you don't know where to find the php.ini file you are using, look at the output of the built-in phpinfo() function where you will find the correct path to the file.) Use caution: The PHP developers set this directive to "off" for security reasons.

From the php.ini file:

; Allow the <? tag. Otherwise, only <?php and <script> tags are recognized.
; NOTE: Using short tags should be avoided when developing applications or
; libraries that are meant for redistribution, or deployment on PHP
; servers which are not under your control, because short tags may not
; be supported on the target server. For portable, redistributable code,
; be sure not to use short tags.
short_open_tag = Off  <-- Turn this "On" to make short tags work

Instead of using the print() or echo() functions to ouput the value of variables, they can be nested within HTML code by using = and ?> shortcut tags where they will automatically be evaluated and printed. (Note: There can be no space between the question mark and the equal sign.) All of the following formats are acceptable:


<strong><?= expression ?></strong>
<?= $color ?>
<strong><? echo statement; ?></strong>
<? echo $color; ?>
You have chosen a <strong><?= $color ?></strong> paint for your canvas.

Example 4.11


  • 1, 2 Two variables are assigned the string values, "Marko" and "San Francisco".
  • 3, 4 The PHP shortcut tag is embedded within the HTML tags. PHP will evaluate the expression within the shortcut tags and print their values. The resulting HTML code contains the result of the evaluation as shown when viewing the browser's source (see Figure 4.13). In this example, the built-in date() function with a "l" option will return the day of the week. The variables, $name and $city are evaluated and placed within the HTML code.

Figure 4.13 HTML source page viewed in the browser.

Figure 4.14 Using shortcut tags. The output from Example 4.11.

[previous] [next]