Quick and Dirty PHP

Basic HTML

<html> <!-- Required for all HTML pages -->
	<head> 
		<title>The Top Bar Title</title>
	</head>
	<body>
		Some information
		<!-- A Comment Tag -->
	</body>
</html> <!-- Closing tag, required -->
		
Example 1

Basic HTML with PHP

PHP = Hypertext Preprocessor
It runs on the server side; meaning that the PHP code is compiled and executed on the server before it is sent to the web browser.
<html> <!-- Required for all HTML pages -->
        <head>
                <title>The Top Bar Title</title>
        </head>
        <body>
                Some information
		<br> <!-- A line break -->
                <!-- A Comment Tag -->
		<?  // Denotes the start of PHP processing
        		echo "The Date and Time is: ";  // print a string, end with a semicolon
        		$mydata = "October 29, 2008"; // Assign a variable 
        		echo $mydata; // print the string variable
		?> 

        </body>
</html> <!-- Closing tag, required -->
		
Example 2
Take note of the different comment styles.
The "//" style within PHP and "<--" outside of PHP (normal HTML)
Variables begin with "$".
"=" are assignment.
"echo" is the print function.
";" ends line of code.
PHP files are saved with ".php" not ".html"

PHP Functions

Can occur anywhere in the page within "<? ?>" tags
<?
function printDate()
{
	echo "The Date and Time is: ";
	$mydata = "October 29, 2008";
        echo $mydata;
}
?>
<html>
        <head> 
                <title>The Top Bar Title</title>
        </head>
        <body>
                <? 
			printDate();
                ?>
        </body>
</html>
Example 3

Built-in PHP Functions

More than you can imagine: Everything from writing XML to creating images on the fly including creation of Flash files and running Java applications
PHP.net is your best friend
<?
function printDate()
{
        echo "The Date and Time is: ";
        $mydata = date("g:i:s A T D. M, j Y");
	/*  -- Multiline comment
	"date" is a built-in function that has some interesting formatting capabilities.
	*/
        echo $mydata;
}
?>
<html>
        <head>
                <title>The Top Bar Title</title>
        </head>
        <body>
                <?
                        printDate();
                ?>
        </body>
</html>
Example 4
date page from the PHP Manual

PHP with Forms

PHP is ideal for generating HTML forms and even more so for dealing with data that comes back from forms.

A Basic HTML Form

<html>
        <head>
                <title>A Basic HTML Form</title>
        </head>
        <body>
		<form action="form_example.php" type="get">
			First Name:<input type="text" name="first_name"><br>
			Last Name:
			<select name="last_name">
				<option value="Smith">Smith</option>
				<option value="Doe">Doe</option>
			</select>
			<br>
			<input type="submit" value="Submit Me">
		</form>
        </body>
</html>
                
HTML Form Example

Form Processing with PHP

The "form" tag's "action" attribute points to the page which recieves the data that is submitted). In this case the following code is saved on the server as form_example.php
<html>
        <head>
                <title>The Form Output</title>
        </head>
        <body>
                <?
			$first_name = $_GET['first_name'];
			$last_name = $_GET['last_name'];	
			echo "Your First Name is: " . $first_name . "<br>";
			echo "Your Last Name is: " . $last_name . "<br>";
                ?>
        </body>
</html>
Example Form Output

In the above example, you notice that the data is coming from the previous page (the page that contains the form) in the form of $_GET['something'] where "something" is the name of the form field. The reason is that PHP puts all of the data that is submitted to it in a global variable. In this case, the global variable is $_GET which is an array containing all of the data that was sumbmitted by the "get" method.

If we rewrote the form example to use "post" instead of "get" as the "type" in the "form" tag, the data would come in the $_POST array and our code would look like this: $first_name = $_POST['first_name']; and so on..

Using the "get" method, the data is attached to the URL of the request. If you take a look, you see that the URL from the form to the next page has a "?" followed by a form element's "name" followed by an "=" with the form element's value. Any additional elements are passed after that seperated by an "&".

form_example.php?first_name=Shawn&last_name=Van%20Every

If you use the "post" method, the data does not show up on in the URL (and more data can be passed from page to page). The disadvantage is that users can not link or bookmark the results of a form that was submitted via a "post".

Using PHP with loadStrings() in Processing

This is an example using PHP to save the state of an Applet developed in Processing

Here is PHP that is saved on the server that hosts the Applet (security restrictions).

This php is called using the following URL: http://itp.nyu.edu/~sve204/icm_fall08/qdphp/loadStrings.php.

As you can see in the code it is programmed to respond to a query string that includes a variable called type. The PHP is set to respond when type=save or type=load. When type=save the PHP saves what is passed in for x and y and saves them to a file on the server. When type=load the PHP loads data from the same file and sends the data saved in the file to back to whomever loaded the url.

http://itp.nyu.edu/~sve204/icm_fall08/qdphp/loadStrings.php?type=save&x=90&y=80

http://itp.nyu.edu/~sve204/icm_fall08/qdphp/loadStrings.php?type=load

We can look at http://itp.nyu.edu/~sve204/icm_fall08/qdphp/savedstate.txt to see what is inside the file.

Enter Processing's loadStrings() function

Since we can request URL's through loadStrings, we can use the PHP above to load the x and y values change them and save them back out.

Loading:
    String loadurl = "http://itp.nyu.edu/~sve204/icm_fall08/qdphp/loadStrings.php?type=load";
    String[] response = loadStrings(loadurl);
    int[] rints = int(split(response[response.length-1],","));
    x = rints[0];
    y = rints[1];    	
	
Saving:
	String saveurl = "http://itp.nyu.edu/~sve204/icm_fall08/qdphp/loadStrings.php?type=save&x=" + x + "&y=" + y;
	String[] response = loadStrings(saveurl);
	
Here is a full example

Other things to pay attention to:

  • Seeing PHP errors: PHP on the ITP server isn't set to show all errors. Use the following two lines in your PHP code to enable error output while you are developing:

    ini_set('error_reporting', E_ALL);
    ini_set('display_errors', true);