PHP Programming/Integration Methods (HTML Forms, etc.)

Integrating PHP.
There are quite a few ways that PHP is used. Following are a few methods that PHP can be called.
Forms.
Forms are, by far, the most common way of interacting with PHP. As we mentioned before, it is recommended that you have knowledge of HTML, and here is where we start using it. If you don't, just head to the HTML Wikibook for a refresher.
Form Setup.
To create a form and point it to a PHP document, the HTML tag &lt;form&gt; is used and an action is specified as follows:
 &lt;form method="post" action="action.php"&gt;
 &lt;!-- Your form here --&gt;
 &lt;/form&gt;
Once the user clicks "Submit", the form body is sent to the PHP script for processing. All fields in the form are stored in the variables $_GET or $_POST, depending on the method used to submit the form.
The difference between the GET and POST methods is that GET submits all the values in the URL, while POST submits values transparently through HTTP headers. A general rule of thumb is if you are submitting sensitive data, use POST. POST forms usually provide more security.
Remember $_GET and $_POST are superglobal arrays, meaning you can reference them anywhere in a PHP script. For example, you don't need to call "global $_POST" or "global $_GET" to use them inside functions.
Example.
Let's look at an example of how you might do this.
 &lt;!-- Inside enterlogin.html --&gt;
 &lt;html&gt;
 &lt;head&gt;
 &lt;title&gt;Login&lt;/title&gt;
 &lt;/head&gt;
 &lt;body&gt;
 &lt;form method="post" action="login.php"&gt;
 Please log in.&lt;br/&gt;
 Username: &lt;input name="username" type="text" /&gt;&lt;br /&gt;
 Password: &lt;input name="password" type="password" /&gt;&lt;br/&gt;
 &lt;input name="submit" type="submit" /&gt;
 &lt;/form&gt;
 &lt;/body&gt;
 &lt;/html&gt;
This form would look like the following:
And here's the script you'd use to process it (login.php):
 &lt;?php
 // Inside enterlogin.html
 if($_POST['username'] == "Spoom" &amp;&amp; $_POST['password'] == "idontneednostinkingpassword")
 echo("Welcome, Spoom.");
 else
 echo("You're not Spoom!");
 ?&gt;
Let's take a look at that script a little closer.
 if($_POST['username'] == "Spoom" &amp;&amp; $_POST['password'] == "idontneednostinkingpassword")
As you can see, $_POST is an array, with keys matching the names of each field in the form. For backward compatibility, you can also refer to them numerically, but you generally shouldn't as this method is much clearer. And that's basically it for how you use forms to submit data to PHP documents. It's that easy.
For More Information.
PHP Manual: Dealing with Forms
PHP from the Command Line.
Although PHP was originally created with the intent of being a web language, it can also be used for commandline scripting (although this is not common, because simpler tools such as the bash scripting are available).
Output.
You can output to the console the same way you would output to a webpage, except that you have to remember that HTML isn't parsed (a surprisingly common error), and that you have to manually output newlines. The typical hello world program would look like this:
 &lt;?php
 print "Hello World!\n";
Notice the newline character at the end-of-line - newlines are useful for making your output look neat and readable.
Input.
PHP has a few special files for you to read and write to the command line. These files include the stdin, stdout and stderr files. To access these files, you would open these files as if they were actual files using fopen, with the exception that you open them with the special php:// "protocol", like this: $fp = fopen("php://stdin","r");
To read from the console, you can just read from stdin. No special redirection is needed to write to the console, but if you want to write to stderr, you can open it and write to it: $fp = fopen("php://stderr","w");
Bearing how to read input in mind, we can now construct a simple commandline script that asks the user for a username and a password to authenticate himself.
 &lt;?php
 $fp = fopen("php://stdin","r"); 
 print "Please authenticate yourself\n";
 print "Username: ";
 // rtrim to cut off the \n from the shell
 $user = rtrim(fgets($fp, 1024));
 print "Password: ";
 // rtrim to cut off the \n from the shell
 $pass = rtrim(fgets($fp, 1024));
 if (($user=="someuser") &amp;&amp; ($pass=="somepass")) {
 print "Good user\n";
 // ... do stuff ...
 } else die("Bad user\n");
 fclose($fp);
Note that this script just serves as an example as to how to utilise PHP for commandline programming - the code contained here demonstrates a very poor way to authenticate a user or to store a password. Remember that your PHP scripts are readable by others!