Beginner's PHP
Written by Jen

For newbie webmasters, the types of scripts avialable on the internet may be slightly overwhelming. We have the basic HTML and CSS, then added with some Javascript, ASP, CGI and Perl, it's hard not to fall off your chair. But admist all the acronyms and technical jargon, there emerges PHP. Hypertext Preprocessor is a server side scripting language, and is by far the most popular web development script online.

For those truly serious about PHP and want to start from scratch, you may want to download and install the latest PHP package from php.net. Keep in mind that you will only need to do this if you are hosting your own website. Most website hosts already include PHP as part of their premium package, although there are also a handful of free PHP-supporting website hosts.

The Basics
Just like javascript PHP can be embedded in HTML pages. But unlike javascript, you will need to make your file extension .php for your PHP scripts to be expressed. If you end your file in .html or .htm or anything else your PHP scripts will not be read and will become redundant.

As with all scripts, PHP has starting and closing tags:

<?php

your code entered here

?>

You can choose to place PHP anywhere in the body of your page. In fact, you can place it immediately after HTML codes and make it act just like regular HTML. For example...

Jen: What theme do you want for next month's layout?
<br>
<?php
echo 'Kathrin: I was thinking maybe a plushie layout?';
?>

...will just become:

Jen: What theme do you want for next month's layout?
Kathrin: I was thinking maybe a plushie layout?

when the page is displayed. And when someone sources the code for that page, it will just give them:

Jen: What theme do you want for next month's layout?
<br>
Kathrin: I was thinking maybe a plushie layout?

So where did the PHP script go? When you requested the script above, Apache intercepted your request and handed it off to PHP. PHP then parsed the script, executing the code between the <?php...?> marks and replacing it with the output of the code run. The result was then handed back to the server and transmitted to the client. Since the output contained valid HTML, the browser was able to render it for display to the user.

Notice the text in green in the example above. Every PHP statement must end in a semi-colon (;). There is an exception for the last line of the PHP block because question mark in the closing PHP tag acts as a semi-colon, but as a cautionary procedure, make sure you add a semi-colon to every scripted line.

As an added bonus to the echo function, you can also add HTML to it and it will still perform the task. For example:

<?php
echo '<b>faeries have wings</b>';
?>

And this will just come out as faeries have wings, like it would with normal HTML.

Comments
If you want to keep your code neat and elements distinguishable from one another, it is highly recommended that you use comments in your scripts. PHP supports both single-lined and multi-lined comment blocks

<?php

// a single-line comment uses two forward slashes

/* a multi-line
comment uses two
enclosing forward slash
and asterisks */

?>

Variables
Every programming language has variables, and PHP is no different. PHP supports a number of different variable types: integers, floating point numbers, strings and arrays. Every variable has a name. In PHP, a variable name is preceded by a dollar symbol ($) and must begin with a letter or underscore, optionally followed by more letters, numbers and/or underscores. For example, $neo, $Reality and $JEN are all valid PHP variable names while $5bucks and $22nd are invalid. Be careful of cases. Variables are case sensitive so $neoreality, $Neoreality and $NEOREALITY are completely different variables.

Adding variables to your PHP script it easy:

Jen: So what pets should we have on the layout and how many?
<br>
<?php
// define variables
$pet = 'Scorchio';
$number = 2;

// put into resulting script
echo "Kathrin: I think we should just use $pet and have $number so it's symmetrical.";
?>

Looking at this, you're probably wondering why variables could of any use. This is just a simple example, usually variables are used larger scripts where there are many repeated factors or changing variables.

In the above example, notice how the $number value of 2 wasn't put in quotation marks like the other variable values. This is because the equal sign (=) acts just like it does in mathematical equations. Except in PHP, the equal sign is called the assignment operator and is used to assign a value to a variable. The value being assigned need not always be fixed - it could also be another variable, an expression, or even an expression involving other variables:

<?php

$nps = $savings + 10000;

?>

You can choose to have more than one assignment in at time, ie. you can have more than one assignment operator (=) in a line.

Variable Types
PHP supports a wide variety of data types, the basics being integer, character, string and Boolean types.

Boolean: specifies a true or false value.

<?php

$question = $true;

?>

Integer: a whole number (can be positive or negative).

<?php

$staff = 5;

?>

Floating-point: typically a fractional number such as 12.5 or 0.254789. Floating point numbers may be specified using either decimal or scientific notation.

<?php

$savings = 123.45;

?>

String: a sequence of characters, like "jennifer" or "refrigerator". String values may be enclosed in either double quotes ("") or single quotes(''). String values enclosed in double quotes are automatically parsed for special characters and variable names. If these are found, they are replaced with the appropriate value.

<?php

$name = 'Jen';
$adjective = 'awesome';

$sentence = "$name is $adjective";
echo $sentence;

?>

And that will just come out as "Jen is awesome", which is so totally true, lol. If your sentence contains apostrophes or quotation marks, it might alter the reading frame of your script. Avoid this by using a backslash (\) where apostrophes or quotation marks are to be used.