P H P  

Hypertext Preprocessor
CPT 108 · Franklin College · Erich Prisner · 2002-2007

Homework: Read the pages "Introduction, Syntax, Variables, Echo, Operators, Comments, Forms, Functions" on the PHP Tutorial

PHP has been invented by Rasmus Lerdorf in 1994. He wanted to create guest book and counter for his own web pages. PHP then meant Personal Home Page Tools. This has been developed further by hundreds of volunteers, (it is open source, meaning it can be distributed freely, the source code is available and can be changed) Nowadays PHP stands for Hypertext Preprocessor. Hypertext is created at the server, and then the resulting HTML file is sent to the client. At the heart of PHP 4.0, there is the Zend engine. PHP is, like JavaScript, to some extend object-oriented.

PHP pages have the extension ".php"

PHP code is written directly into the HTML code, everything written inside the PHP tag <?php ... ?> is interpreted as PHP code. You can also write the PHP code between the tags <script language="php"> and   </script>.

PHP code is executed on the server, not on the client computer like JavaScript. Therefore we cannot test our PHP scripts unless we put it on a server able to run PHP.

Variables must begin with a $-sign in PHP! Moreover, no spaces are allowed inside a variable name.

Every statement in PHP must be ended with a semicolon.

Examples

build-in functions

echo: With the code echo "<b>Good morning, world </b>" ; you write the text "Good morning, world" in bold face on the page. Avoid quotation marks inside the string---use single quotation mark ', or, if you must use ", use \". You echo variables without quotation marks, like in echo $day;, but if you have to output both strings and variable, you concatenate using the period ., like in echo "today is" . $day;. If everything should be bold, you write echo "<b>today is" . $day . "<b>";

PHP and Forms

Mostly PHP is used to evaluate information submitted in forms, and respond accordingly. Your form needs an action attribute and a method attribute. "action", like in action="Filename.php" tells where the data is send when a type="submit" button is hit. We will usually use method="post" (rather than method="get"). When sending the button by hitting the type="submit" button, the names of all form elements become PHP variables, with a $ in front of them, which you cann access at the page where the information is send to.

See the separate page on radio buttons and how to handle them with HTML, Javascript, and PHP.

PHP and text files

The function for opening or creating) functions is $PointerName = fopen($FileName, 'w');, where $FileName is of course the name of the file you want to create (including extension, like "txt"), and the second parameter is

Every open text file has a pointer pointing (which we named "$PointerName" above) to a certain character in the file. If reading and writing should be allowed, you use 'r+' if the pointer startes at beginning, 'w+' if all existing data should be erased, and 'a+' if pointer startes at the end.

When you are done working with a file, you should close it using the function fclose($PointerName);

You write into an open file using the fputs function, as in fputs($PointerName,"Some text string");. You would start a new line with /n. You read with the "fgets" function, as in fgets($PointerName,5);. In this case you would read 5 byte, starting from the present location of the pointer, of course. You would rewind the pointer using the function rewind($PointerName);.