Home | PHP | Visual Basic | Flash | HTML
 
  Embedding PHP in HTML
One of the nicer things about PHP is that, unlike CGI scripts, which require you to write server-side code to output HTML, PHP lets you embed commands in regular HTML pages. These embedded PHP commands are enclosed within special start and end tags, which are read by the PHP interpreter when it parses the page. Here is an example of what these tags looks like:

<?php ... PHP code ... ?>

You can also use the short version of the previous, which looks like this:

<? ... PHP code ?>

To see how this works, create this simple test script, which demonstrates how PHP and HTML can be combined:

<html> <head><basefont face="Arial"></head>
<body> <h2>Q: This creature can change color to blend in with its surroundings. What is its name?</h2>
<?php // print output echo '<h2><i>A: Chameleon</i></h2>'; ?>
</body> </html>

Save the previous script to a location under your web server root as question.php, and browse to it. You’ll see a page like Figure 3-1. And here is what the HTML source of the rendered page looks like:

<html> <head><basefont face="Arial"></head> <body>
<h2>Q: This creature can change color to blend in with ↵ its surroundings.
What is its name?</h2> <h2><i>A: Chameleon</i></h2> </body> </html>

When you requested the previous script through your browser, the web server intercepted your request and handed it off to PHP. PHP then parsed the script, executing the code between the <?php...?> marks and replacing it with the resulting output. The result was then handed back to the web server and transmitted to the client. Because the output contained valid HTML, the browser was able to render it for display to the user.



Writing Statements and Comments As you can see from the previous example, a PHP script consists of one or more statements, with each statement ending in a semicolon. Blank lines within the script are ignored by the parser. Everything outside the tags is also ignored by the parser, and returned as is; only the code between the tags is read and executed.

For greater readability, you should add comments to your PHP code, as I did in the previous example. To do this, simply use one of the comment styles listed here:

<?php
// this is a single-line comment
# so is this
/* and this is a multiline comment */ ?>

Storing Values in Variables
Variables are the building blocks of any programming language. A variable can be thought of as a programming construct used to store both numeric and nonnumeric data. The contents of a variable can be altered during program execution, and variables can be compared and manipulated using operators.

PHP supports a number of different variable types—Booleans, integers, floating point numbers, strings, arrays, objects, resources, and NULLs—and the language can automatically determine variable type by the context in which it is being used. Every variable has a name, which is preceded by a dollar ($) symbol, and it must begin with a letter or underscore character, optionally followed by more letters, numbers, and underscores. For example, $popeye, $one_day, and $INCOME are all valid PHP variable names, while $123 and $48hrs are invalid variable names.

Variable names in PHP are case-sensitive; $count is different from $Count or $COUNT.
To see PHP’s variables in action, try out the following script:

<html> <head><basefont face="Arial"></head> <body>
<h2>Q: This creature has tusks made of ivory. ↵ What is its name?</h2>
<?php // define variable $answer = 'Elephant'; // print output echo "<h2><i>$answer</i></h2>"; ?>
</body> </html>

Here, the variable $answer is first defined with a string value, and then substituted in the echo() function call. The echo() function, along with the print() function, is commonly used to print data to the standard output device (here, the browser). Notice that I’ve included HTML tags within the call to echo(), and they have been rendered by the browser in its output.

Assigning and Using Variable Values

To assign a value to a variable, use the assignment operator, the equality (=) symbol. This operator assigns a value (the right side of the equation) to a variable (the left side). The value being assigned need not always be fixed; it could also be another variable, an expression, or even an expression involving other variables, as here:

<?php $age = $dob + 15; ?>

To use a variable value in your script, simply call the variable by name, and PHP will substitute its value at run time. For example:

<?php $today = "Jan 05 2004"; echo "Today is $today"; ?>

Saving Form Input in Variables

Forms have always been one of the quickest and easiest ways to add interactivity to your web site. A form enables you to ask customers if they like your products and casual visitors for comments. PHP can simplify the task of processing webbased forms substantially, by providing a simple mechanism to read user data submitted through a form into PHP variables. Consider the following sample form:

<html> <head></head>
<body> <form action="message.php" method="post"> Enter your message: <input type="text" name="msg" size="30">
<input type="submit" value="Send"> </form>
</body> </html>

The most critical line in this entire page is the <form> tag:

<form method="post" action="message.php"> ... </form>

As you probably already know, the method attribute of the <form> tag specifies the manner in which form data will be submitted (POST), while the action attribute specifies the name of the server-side script (message.php) that will process the information entered into the form. Here is what message.php looks like:

<?php // retrieve form data in a variable $input = $_POST['msg'];

// print it echo "You said: <i>$input</i>"; ?>

To see how this works, enter some data into the form (“boo”) and submit it. The form processor should read it and display it back to you (“you said: boo”). Thus, whenever a form is POST-ed to a PHP script, all variable-value pairs within that form automatically become available for use within the script through a special PHP container variable, $_POST. To then access the value of the form variable, use its name inside the $_POST container, as in the previous script. If the form uses GET instead of POST, simply retrieve values from $_GET instead of $_POST.

--VISIT THIS PAGE REGULARLY FOR MORE TUTORIALS ON THE NEXT UPDATE
--LESSONS FROM THE BOOK ENTITILED How To Do Everything With PHP & MySQL 2005