Документация
HTML CSS PHP PERL другое

Entering PHP Scripts

 
Previous
Table of Contents
Next

Entering PHP Scripts

Before we delve too deeply in to the PHP language, we will look at how to enter scripts and how code interacts with HTML markup.

Marking Sections of PHP Code

There are a few ways to indicate that a section of the input file contains PHP script. The most common way is as follows:

<?php
  echo "Hello Everybody!";
?>

This method is safe for both XHTML and XML files; therefore, it is the method we will use throughout this book.

Another way to demarcate PHP script is as follows:

<?
  echo "Bonjour tout le monde!";
?>

This is called using short tags. Short tags are available only if the short_open_tag setting is enabled in your php.ini configuration file (see Appendix A). The use of this style is generally discouraged, as it does not save you much in typing and can create problems as you move your code from server to server, where one of them might not have short tags enabled.

A third way of entering script is

<script language="php">
  echo "Ciao a tutti!";
</script>

This method does not buy us very much and is generally used much less than the first style.

One final style of inputting script exists to support some older graphical HTML editing programs that are unable to understand script directives very well and would move script around when they generated HTML for user input. However, some of these editors could support tags by marking code as ASP script. To let users continue to use these editors, you can mark the PHP script using these ASP tags:

<%
     echo "Guten Tag alle!";
%>

ASP tags can only be used if the asp_tags setting is enabled in the php.ini configuration file.

Even though we will use the first style of tags, we have shown you all the possible types to prepare you for other codes you may encounter.

Mixing PHP and HTML

There is nothing that requires large blocks of PHP code when writing HTML and PHP: You are completely free to mix the markup and script as much as you wish. For example, you might want to personalize a greeting message ($userName is a variable, which we will introduce more formally soon):

<?php
   $userName = "Chippy the Chipmunk";
?>

<p align='left'>
  Hello there, <b><?php echo $userName; ?></b>
</p>

A shortcut exists for this particular usage. It involves the short tags discussed in the previous section along with an equals sign (=):

<?= $userName ?>

This is the same as typing

<?php echo $userName; ?>

This can be a handy way to save a bit of typing when you are injecting several expressions into your HTML. However, we will continue to avoid using short tags and stick with the normal syntax.

The flexibility available when mixing PHP and HTML allows us to be creative when we get into more advanced language constructs. (These concepts will be introduced in Chapter 2, "The PHP Language.")

<?php

   if ($file_received_successfully === TRUE)
   {
?>
   <p align='center'> Thank for your contribution </p>
<?php
   }
   else
   {
?>
   <p align='left'>
     <font color='red'>
     <b>Error:  The file was not correctly received.</b>
     </font>
   </p>
<?php
   }

?>

This, like some other things we will encounter as we learn about web application programming with PHPwhile perfectly valid in PHPis probably something we should use sparingly. We should write code that is legible and easily maintainable whenever possible.

Statements and Comments

Statements in PHP are separated by a semicolon (;). Statements can be grouped together by wrapping them in brackets ({}), which is sometimes called a block of code. Any number of statements can be placed on one line. Individual items (or tokens) within a statement can be separated by arbitrary amounts of whitespace (space, newline characters, or tabs). Statements can even span more than one line.

 <?php

    $x = 123; $y = 456; $z = "hello there"; $a = "moo";

  {
       echo "This is a group of statements";
       $m = "oink";
  }
    $userName
                        =
  "Chippy the Chipmunk"

                              ;
?>

Just because one can do this, however, does not mean it is recommended. Programmers are always encouraged to make their code legible.

The end of a section of PHP script is also a valid way to terminate a statement:

<?php echo "Semi-colon not necessary here!" ?>

There are three basic styles for entering comments in PHP:

<?php
     /*
      * This is our first style of comment.
      */
     echo "Style 1";

     //
     // This is our second style of comment.  It is "single line"
     //
     echo "Style 2";

     #
     # This third style is also "single line."
     #
     echo "Style 3";
?>

The first two styles are very common in higher-level programming languages, such as C/C++, Java, and C#, while the latter style will be familiar to programmers of Perl and shell scripts.

The first style of comments beginning with "/*" causes the PHP processor to ignore all text until it sees a closing "*/" sequence. This can cause problems if you try to nest comments.

<?php
/*
    /**
     *  this is a comment.
     */
    echo "This is some code";
*/
?>

Because of the nested comment, the processor stops treating the code as comment text at the first "*/" token and reports and error at the second, because it no longer believes itself to be processing comments.

The two types of single-line comments cause the PHP language processor to ignore all code until the end of the current line or current PHP script section.

<?php
    // all of this line is ignored.
    echo "But this line prints just fine.";
?>

<?php #Comment!! ?><b>This prints</b><?php echo "this prints" ?>


Previous
Table of Contents
Next
© 2000- NIV