Title: Using PHP program ($$$)
Course: PHP/MySQL Course

PHP block

PHP is a programming language that will create 'extra content' for your web html file before the server sends it back to the browser.

When a user selects a page in his browser that points to a php page on the web server a sequence of things happen.

1. The request for the page is sent to the web server with a filename ending in .php

2. The web server software (apache in many cases) sees the .php on the file extension and will start tell the php engine installed as part of the web server to process the page.

3. The php engine will open the php file and look for any <? ?> enclosed code blocks. It will run the code that is between the <? ?> code blocks and any output will be inserted into the existing HTML inplace of the code block area.

4. Once all the code blocks have been executed, the final HTML will be passed to the web server.

5. The web server will in turn send it over the internet to the original browser, who will display it as a new page.

Question?
What file extension does a php file have normally?

- (|\.)(php|PHP)

Inside the code block

The php programming language will be in code blocks that are insterspersed in the middle of normal HTML.

The start of the code block is indicated by a <?
the there can be any amount of php code
then you must have a ending ?> to mark the end of that block of code and the beginning of more normal HTML language.

Here is a simple example: (we have left out some html stuff for simplicity)

<html>
<head>
<title>Sample</title>
</head>
<body>
<h3>This page is designed by</h3>
<?
echo "Gerry W. Jenkins";
echo "<br /> &copy; Copyright 2009 <br />";
echo 1000 + 1200 / 10;// do some math
?>
</body>
</html>

This has a code block with two lines of php code.
It is the echo statement, which will echo what ever string value is passed to it as a parameter to the right of it.

In the second echo statement you will see a <br /> tag in the string as well as a HTML special character code for a copyright symbol.

In the third echo statement, you see a mathematical expression, that will be calculated and the converted automatically to a string and passed to the echo statement.

Note that each statement must have a semicolon (;) to end the statement as in C, C++ and java.

Click on this link to see the result of this php program: test1.php

Question?
What marks the end of a statement?

- ;
- semicolon

Some more details

<?
echo "Gerry W. Jenkins";
echo "<br /> &copy; Copyright 2009 <br />";
echo 1000 + 1200 / 10; // do some math
?>
The // do some math is called a comment.

the // will mark anything after the // on the same line as a comment.
Comments are ignored by the php engine. The are for putting in message to yourself and other programmers to document and explain your code.

A # will do work exactly as // for inserting a comment till the end of line.

So this is a valid comment:

echo 1000 + 1200 / 10; # do some math next

Another type of comment is to use

/* to mark the beginning and */ to mark the end with the comment between the two marks. this type of comment can be in the middle of a code line or take up several lines.

For example you could put a 'comment block' at the top of your code block area by doing this at the start of the code block?

<?
/*****************************
* Programmer: Gerry Jenkins *
* Program: Sample PHP Code *
*****************************/

Question?
What two characters mark the end of a code block?

- ?>