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
<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 = "July 29, 2004"; // 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 = "July 29, 2004";
        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

Dealing with data submitted via a form
<?
        /* 
        Description: Helper function to get values from a form post (type="post" or query string (type="get")
        Returns: value of key or null on failure
        */
        function getPOSTorGETValue($key)
        {
            if (isset($_POST[$key]))
            {
                $value = $_POST[$key];
            }
            else if (isset($_GET[$key]))
            {
                $value = $_GET[$key];
            }
            else
            {           
                $value = null;
            }    
            return $value;
        }
?>
<html>
        <head>
                <title>The Form Output</title>
        </head>
        <body>
                <?
			$first_name = getPOSTorGETValue("first_name");
			$last_name = getPOSTorGETValue("last_name");	
			echo "Your First Name is: " . $first_name . "<br>";
			echo "Your Last Name is: " . $last_name . "<br>";
                ?>
        </body>
</html>
Example Form Output

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/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/qdphp/loadStrings.php?type=save&x=90&y=80

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

We can look at http://itp.nyu.edu/~sve204/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/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/qdphp/loadStrings.php?type=save&x=" + x + "&y=" + y;
	String[] response = loadStrings(saveurl);
	
Here is a full example

Other things to pay attention to:

  • Permissions on the server: Set them to 777 to make the text file that you are accessing through PHP world readable and writable. Use chmod 777 filename on the command line or using your favorite file transfer application.

  • 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);