An Introduction to PHP
by Freddy18uk

Introduction & Contents
PHP (PHP HyperText Preprocessor) is a free server side scripting language, originally Personal Home Pages, as a set of cgi scripts. It is now a much larger scripting language available for many platforms, as modules (for apache etc...) and as a set of cgi files.

The Basic Syntax
PHP can be contained within any other content, so long as the file extension is recognised as to be parsed for PHP (.php by default). A file can contain a mixture of content, the only content which is parsed for php must be contained within these tags:

<?PHP   Scripts here ?>

Multiple instances of PHP sections can be made in one page.

The PHP syntax is similar, but not exactly the same as that for C and Java.
Variables are untyped, the processor will decide on their type at runtime, the variable does not need to be declared in advance. Variables begin with the Dollar symbol ($) such as $myvariable. PHP is case sensitive, so $myvariable, and $myVariable are not the same. Each PHP statement MUST end with the semi-colon character (;).
Like C and Java, assignment is performed using a single equals sign (=), equality is checked using a double equals sign (==) and inequality is checked using !=.
Comments are ignored by the processor, but allow you to comment your code for later use when reading, comments can either be single line, i.e. //comment or multiple lines /* Comments in here.*\

Input and Output
Content can be written to the standard output using either echo or print. Strings can be enclosed in either double or single quotes ("" or ''). Strings in double quotes are parsed for variables and control characters, hence require more processing, so unless variables appear within the string, use only single quotes.

PHP automatically parses the Query String for the variables passed from forms etc... By default, "Register Globals" is disabled, meaning you can no longer call a variable passed from a form, using just its name, such as $formfield1. Many may enable this, but the safest way is to use $_GET['formfield1'] which takes the formfield1 value from the get request. Similar there is $_POST[''] for the same thing.
Example

Assume the following HTML form

<form method="POST" action="myFirstScript.php">
Your Name: <input type="text" name="name"><b>
Your E-Mail address: <input type="text" name="email"><br>
</form>

The script

<html>
<head>
<title>Form Results</title>
</head>
<body>
<?PHP
  //Take the details from the input form
  $name = $_POST['name'];
  $email = $_POST['email'];
  //Now output it to the user
  echo "Your name is $name"; //Parse the string, $name is replaced with their name
  echo 'Your E-Mail address is'.$email; //Do not parse the string, instead concatenate the two strings using the period
?>
</body>
</html>

phpinfo()

<?PHP
  echo phpinfo();
?>

phpinfo() will output information about your current PHP installation. Towards the lower section, you will see the variety of environments available, they are not all listed here, as various variables come into action under certain situations.
Some common variables you may use
REMOTE_ADDR - The remote address, i.e. the Client's IP address.
HTTP_USER_AGENT - The Client's User-Agent, i.e. Mozilla, Netscape.
REQUEST_METHOD - Wether the page was requested using GET or POST.
REQUEST_URI - What the client actually requested.
To make use of the above, use getenv('variable') e.g. echo getenv('REMOTE_ADDR') will output the IP address.

Control Structures
The control structures are almost identical to C, except for the Switch statement can also be used on the non-ordinal date type String.
for ($i=0,$i<$somevalue;$i++) { doSomething; }
do { something; } while ($somevalue!=$someOtherValue)
while ($somevalue>$someOtherValue) { doSomething; }
switch ($value) { case 'x': doThis; break; }

Common Functions
Mail
Mail will allow you to send an e-mail, and it's syntax:
mail(TO,SUBJECT,MESSAGE[,HEADERS]);
TO - The recipient, just their plain e-mail address here.
SUBJECT - The subject.
Message - The Message.
Headers - These are optional, extra headers. If you know about e-mail headers, you can add them here, in the formation of "FIELD1: VALUE1\r\nFIELD2: VALUE2\r\netc...\r\n\r\n". This would be the ideal place to add the FROM(!) information not in the mail command. ("FROM: me@mydomain.com\r\n\r\n").
Date
Date will allow you to format a date, and if the second argument is omitted, that will be the current date.
Date(FORMAT[,TIMESTAMP]);
FORMAT - This is the PHP date format, as given here.
TIMESTAMP - This is optional, you can insert any timestamp here to format it.



Fancy learning C, C++ or Perl? The have a look in our tutorials section.