Consider the difference in syntax between
the following two code snippets. The first presents incorrect use of conditional
include() statements due to the lack of proper block enclosures:
if (expression)
include ('filename');
else
include ('another_filename');
?>
The next snippet presents the correct use of conditional include() statements by
properly enclosing the blocks in curly brackets:
110 CHAPTER 3 ?– PHP B ASICS
if (expression) {
include ('filename');
} else {
include ('another_filename');
}
?>
One misconception about the include() statement is the belief that because the
included code will be embedded in a PHP execution block, the PHP escape tags aren??™t
required. However, this is not so; the delimiters must always be included. Therefore,
you could not just place a PHP command in a file and expect it to parse correctly,
such as the one found here:
echo "this is an invalid include file";
Instead, any PHP statements must be enclosed with the correct escape tags, as
shown here:
echo "this is an invalid include file";
?>
?– Tip Any code found within an included file will inherit the variable scope of the location of its caller.
Interestingly, all include() statements support the inclusion of files residing on
remote servers by prefacing include()??™s argument with a supported URL. If the resident
server is PHP-enabled, any variables found within the included file can be
parsed by passing the necessary key/value pairs as would be done in a GET request,
like this:
include "http://www.
Pages:
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194