PHP 101

PHP is a very useful language for doing many types of development. Primarily it is used for web development (hence the name, Hypertext Preprocessor) but it is certainly not limited to that. For our purposes we will be using PHP as both a web development tool and as a means to glue together various webservices that we might access through our applications (which is what we are doing with Textmarks).

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"

Creating a PHP file for use on the command line

PHP can be developed in any text editor and doesn't require any compilation. More Information:
  • PHP: Basic Syntax - Manual


  • Variables

    Variables in PHP are not strictly typed, meaning that you do not have to differentiate between strings, integers and so on. They start with a "$".

    		<?PHP
    			$myString = "hello world\n";
    			echo($myString);
    			$myInteger = 1003;
    			echo($myInteger . "\n");
    			$somethingelse = false;
    			echo($somethingelse . "\n");
    		?>
    		
    More Information:
  • PHP: Types - Manual
  • PHP: Variables - Manual
  • PHP: Constants - Manual


  • Mathematical Operators

    		<?PHP
    			$aValue = 0;
    			$aValue++;
    			echo("aValue now = " . $aValue . "\n");
    			$aValue = $aValue + $aValue;
    			echo("aValue now = " . $aValue . "\n");
    			// % + - * / ++ -- and so on, same as in Processing/Java
    		?>
    		
    More Information:
  • PHP: Expressions - Manual
  • PHP: Operators - Manual


  • Control Structures, Logical Operators and Loops

    		<?PHP
    		
    			// If Statement
    			$aValue = 0;
    			if ($aValue == 0)
    			{
    				echo("aValue is 0");
    			}
    			else if ($aValue == 1)
    			{
    				echo("aValue is 1");
    			}
    			else if ($aValue >= 2)
    			{
    				echo("aValue is greater than or equal to 2");
    			}
    			else
    			{
    				echo("aValue something else");
    			}
    			echo("\n");
    			// Other Logical Operators ==, >, <, >=, <=, ||, &&
    
    		
    			// For loop
    			for ($i = 0; $i < 10; $i++)
    			{
    				echo("\$i = $i\n");
    			}
    			
    			// Also While
    		?>
    		
    More Information:
  • PHP: Control Structures - Manual


  • Arrays and Loops

    		<?PHP
    			// Pretty normal array
    			$anArray = array();
    			$anArray[0] = "Something";
    			$anArray[1] = "Something Else";
    			for ($i = 0; $i < sizeof($anArray); $i++)
    			{
    				echo($anArray[$i] . "\n");
    			}
    			
    			// Key Value Map
    			$anotherA = array("somekey" => "somevalue", "someotherkey" => "someothervalue");
    			$keys = array_keys($anotherA);
    			$values = array_values($anotherA);
    			for ($i = 0; $i < sizeof($keys); $i++)
    			{
    				echo($keys[$i] . " = " . $values[$i] . "\n");
    			}
    		?>
    		
    More Information:
  • PHP: Arrays - Manual


  • Functions

    		<?PHP
    			
    			function myFunction($somearg)
    			{
    				// You would do something here
    				return "You passed in $somearg";
    			}
    			
    			$passing_in = "Hello World";
    			$return_value = myFunction($passing_in);
    			echo($return_value);
    			
    			echo("\n");
    		?>
    		
    More Information:
  • PHP: Functions - Manual


  • Classes and Objects

    		<?PHP
    			class MyClass
    			{
    				var $myClassVar;
    
    				function set_var($new_var)
    				{
    						$this->myClassVar = $new_var;
    				}
    
    				function get_var()
    				{
    						return $this->myClassVar;
    				}
    			}
    	
    			$aClass = new MyClass;
    			$aClass->set_var("something");
    			echo("Var: " . $aClass->get_var() . "\n");		
    		?>
    		
    Classes and Objects in PHP are very similar to Java/Processing. Syntactically though they don't use the "." operator, instead using "->". Also, in the class definition you need to use the "this" keyword.

    More Information:
  • PHP: Classes and Objects - Manual


  • Some Interesting Functions

    isset()
    		<?PHP
    			$somevar = NULL;
    			if (isset($somevar))
    			{
    				echo("somevar is set");	
    			}
    			else
    			{
    				echo("somevar is NOT set");
    			}
    		?>
    		
    mail
    			$mailmsg = "A Mail Message";
    			$mailsubject = "A Mail Subject";
    			$mailfrom = "vanevery@walking-productions.com";
    			$mailto =  "vanevery@walking-productions.com;
    			$mailheaders = 'From: ' . $mailfrom . "\n"; 
    			$mailheaders .= 'Reply-To: ' . $mailfrom . "\n"; 
    			$mailheaders .= 'Return-Path: ' . $mailfrom . "\n";
    			$mailheaders .= "Message-ID: <".time().">\n"; 
    			$mailheaders .= "X-Mailer: Shawns PHP Mailer Function\n";          
    			ini_set('sendmail_from',$mailfrom);
    			$return = mail($mailto, $mailsubject, $mailmsg, $mailheaders);
    			ini_restore('sendmail_from'); 
    			echo "Mail Sent?: ";
    			var_dump($return);		
    		
    More information:
  • PHP: Mail - Manual


  • More Information:
  • Tons More Here: PHP: Function Reference - Manual