ICMphp

ITP HELP | RecentChanges | Preferences

PHP

<? 
echo "Hello World";
?>
[Try it.]

The most common use of php is mixing it in with hardwired html.

<html> 
        <head>
                <title>Baron</title>
        </head>
        <body>
                This is regular hardwired text in the html. <BR> Flipped a coin and...
		<br>
		<?  // start of PHP processing
        	$myArray = Array("Heads", "Tails");  //variables have to start with a dollar sign,
            $randomNumber = rand(0,1); //there are tons of nifty and easy functions like rand
            $answer = $myArray[$randomNumber]; //notice the variables aren't "typed" like int, float..
            echo "$answer , you loose." ; // echo sends things out, notice you don't have to use 
                        //all the +'s that you used in processing.  Anything in the quotes with a $ is a variable
	     // end of PHP processing
		?> 
        </body>
</html> 
[Try refreshing it a few times.]

The most exciting part of php is that input can come into your scripts from the user.

<html>
        <head>
                <title>Looking a query string</title>
        </head>
        <body>
                <?
			$first_name = $_GET["first"];  //there is an array called $GET that is set for you
			$last_name = $_GET["last"]; //it will contain all the values sent in on the end of the url
			echo "Your First Name is: $first_name <br>";
			echo "Your Last Name is: $last_name <br>";
                ?>
        </body>
</html>

[Try adding this "?first=Harry&last=Hoodini" to the end of the url.]


Persistance, remember something so the person or others can see it later. Obligatory bouncing ball example.

<?
error_reporting(E_ALL ^ E_NOTICE);
ini_set('display_errors', 1);
?>


<?

$xdir = readIt("xdir");
$ydir = readIt("ydir");
$locx = readIt("locx");
$locy = readIt("locy");
//$locy = 15;
$locx = $locx + $xdir;
$locy = $locy + $ydir;
if ($locx >= 20){
	$xdir = -1;
}
if ($locx <=  0){
	$xdir = 1;
}
if ($locy >= 20){
	$ydir = -1;
}
if ($locy <=  0){
	$ydir = 1;
}
writeIt("xdir",$xdir);
writeIt("ydir",$ydir);
writeIt("locx",$locx);
writeIt("locy",$locy);

for($i = 0; $i < $locy; $i++){
	echo "<br>";  //html for new line
}
for($i = 0; $i < $locx; $i++){
	echo "    ";  //html for 4 spaces
}

echo "O";



function writeIt($filename,$content){
	$dest = fopen("./up/$filename.txt", 'w') ;
	fwrite($dest,$content);
	fclose($dest);
}
function readIt($filename){
	$filename = "./up/$filename.txt";
	$size = filesize($filename);
	if ($size == 0) return 0;
	$src = fopen($filename, 'r');
	$content = fread($src,$size);
	fclose($src);
	return $content;
}
?>


[Try it.] (Refresh a few times).


Okay Now Processing talking to PHP.

//A riff on shawn van every's LoadStringsPHP by dano
public class LoadStringsPHP extends PApplet
{

  String[] everybody;

  void setup()
  {

    fill(255);
    stroke(127);
    println("Loading");
    String loadurl = "http://itp.nyu.edu/~dbo3/icm/php/saveOnServer.php?type=load";
    everybody = loadStrings(loadurl);
    println("how many people? " + (everybody.length-1));
    size(500,500);
  }

  void draw()
  {
    background(0);
    for(int i = 0; i < everybody.length-1; i++){
      String thisEntry = everybody[i];
    
      String[] partsOfThisEntry = thisEntry.split(",");
      if (partsOfThisEntry.length < 2) break;
      int x = int(partsOfThisEntry[0]);
      int y = int(partsOfThisEntry[1]);
      rect(x,y,20,20);
    }
  }

  void mousePressed()
  {
    int  x = mouseX;
    int y = mouseY;
    println("Saving: x = " + x + " y = " + y);
    String saveurl = "http://itp.nyu.edu/~dbo3/icm/php/saveOnServer.php?type=save&x=" + x + "&y=" + y;
    everybody = loadStrings(saveurl);
  }
  
void keyPressed()
  {
    if (key == 'c'){
      String clearurl = "http://itp.nyu.edu/~dbo3/icm/php/saveOnServer.php?type=clear";
        everybody = loadStrings(clearurl);
    }
  }
}


<?
	//these let you see the errors
    ini_set('error_reporting', E_ALL);
    ini_set('display_errors', true);

    if (isset($_GET['type']) && $_GET['type'] == "save")
    {
       	$file = fopen("./up/savedstate.txt","a"); //make the directory "up" world writable
		//there are limits to the $sign tipping off variables, if it is an array, you have to enclose in curly brackets
		fwrite($file,"{$_GET['x']},{$_GET['y']}, \r");  //add a return character so processing will later get array of lines
		fclose($file);
		$filelines = file("./up/savedstate.txt");  //spew it all back out.
        echo $filelines[sizeof($filelines) - 1];

       // echo "Saved x = " . $_GET['x'] . " and y = " . $_GET['y'];
    }
    else if (isset($_GET['type']) && $_GET['type'] == "load")
    {
        $filelines = file("./up/savedstate.txt");  
        echo $filelines[sizeof($filelines) - 1];
    }
	 else if (isset($_GET['type']) && $_GET['type'] == "clear")
    {
        $file = fopen("./up/savedstate.txt","w"); //make the directory "up" world writable
		fclose($file);
    }
    else
    {
        echo "Nothing";
    }
?>

ini_set('error_reporting', E_ALL);
ini_set('display_errors', true); 
[Try clicking to add squares, reload and they will come back]

Original-recipient: rfc822;dbo3@mail.nyu.edu

We (ITS) didn't want to default to seeing errors (a security thing for its). However, you can put these 2 lines at the top of your program:

error_reporting(E_ALL ^ E_NOTICE); ini_set('display_errors', 1);

and it should display the errors except for notices.

If you want to see the notices as well then it's:

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

Errors are also being sent to the error log and the above lines won't work if there's a syntax error (like a missing semicolon) in the program, but that will be in the error log.

So this unix line command, in an ssh terminal window logged into itp.nyu.edu, will display your error if you run it soon enough.

tail /var/log/httpd/error_log Shows the last 10 lines

tail -25 /var/log/httpd/error_log Will show the last 25 lines of the error log.

I've actually put this line in my .bash_profile in my account: alias error='tail /var/log/httpd/error_log'

so that all I have to do is type: error on the command line and get the tail of the error log.

Also, if you navigate to the location of the program the line command: php progname.php will execute the program locally and display any error messages in line.


ITP HELP | RecentChanges | Preferences
This page is read-only | View other revisions
Last edited October 31, 2007 12:27 pm (diff)
Search:
To EDIT, You have to enter an ADMINISTRATOR password (guess) in Preferences. And refresh