2.1. Embedding PHP Scripts

In Chapter 1, when I talked about Apache and web servers in general, I mentioned how a server will process PHP in a file before sending that file to the browser. But you might be curious how the server knows where to look for PHP.

By default, servers look for PHP only in files that end with the .php extension. But a .php file can contain elements that aren't part of your PHP script, and searching the entire file for potential scripts is confusing and resource-intensive. To solve this issue, all PHP scripts need to be contained with PHP delimiters. To begin a PHP script, you include the opening delimiter <?php and start coding. To finish, you simply add ?> to the end of the script. Anything outside of these delimiters will be treated as HTML or plain text.

You can see this in action by opening Eclipse and editing the test.php file by double-clicking the file in the project folder you created in the last chapter (full path: /xampp/htdocs/simple_blog/test.php) so it contains the following code:

<p>Static Text</p>
<?php
    echo '<p>This text was generated by PHP!</p>';
?>
<p>This text was not.</p>

Save the file, navigate to http://localhost/simple_blog/test.php in your browser, and you should see the following:

Static Text
This text was generated by PHP!
This text was not.

As you can see, the text inside the PHP delimiters was handled as a script, but the text outside was rendered as regular HTML. There is no limit to how many blocks of PHP you can include in a page, so the following snippet is completely valid:

<?php
    echo '<p>This is some text.</p>';
?>
<p>Some of this text is static, <?php echo 'but this sure isn't!'; ?></p>
<?php echo '<p>'; ?>
This text is enclosed in paragraph tags that were generated by PHP.
<?php echo '</p>'; ?>

The preceding code snippet outputs the following to the browser:

This is some text.
Some of this text is static, but this sure isn't!
This text is enclosed in paragraph tags that were generated by PHP.

2.1.1. Alternative Delimiters

There are a few alternative methods for delimiting your PHP scripts that you might come across from time to time, so you should be aware of them. However, it's important to note that the use of these alternative delimiters is discouraged, so you should avoid using them.

2.1.1.1. Short Tags

PHP offers a shortcut syntax known as short tags; these allow your scripts to be delimited with <? and ?>, which is easier to type. However, the use of short tags requires that the short_open_tag directive be enabled, which means that scripts using short tags can create compatibility problems for applications that need to run on multiple servers that might not have the same configuration.

The use of short tags conflicts with XML syntax (XML declarations use the syntax <?xml version="1.0" encoding="ISO-8859-1"?>), so you should not use them.


There is also a shortcut syntax for outputting data quickly, which you use like this:

<?='Some text to output.'?>

The previous snippet functions identically to this longer-winded syntax:

<?php
    echo 'Some text to output.';
?>

Again, keep in mind that you should avoid using this syntax because of its incompatibility, both with various server configurations and XML syntax.

2.1.1.2. HTML <script> Tags and ASP-Style Delimiters

For the sake of compatibility with editing software such as Microsoft Front Page, PHP also supports the use of HTML <script> tags:

<script language="php">
    echo 'This is some text';
</script>

Another option provided to Front Page users was Microsoft's ASP-style delimiters:

<%
    echo 'This is some text';
%>

Use of the <script> tag is discouraged because it can cause confusion with JavaScript in files. As of PHP 6, ASP-style tags are no longer supported.


..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.189.186.167