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
Explanation
|
Example 4.9
Explanation
|
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:
Format
<strong><?= expression ?></strong>
<?= $color ?>
<strong><? echo statement; ?></strong>
<? echo $color; ?>
You have chosen a <strong><?= $color ?></strong> paint for your canvas.
Example 4.11
Explanation
|
[previous] [next]
URL: