PDA

View Full Version : Basics of PHP



Lily
4th September 2004, 03:11 PM
The Basics of PHP



Table of Contents

What is PHP?
How can I obtain PHP?
Local Webservers
Variables
Functions
Operators
Predefined Variables
Loops
Regular Expressions
References
Conclusion





What is PHP?

whatisPHP, or laterally referred to as "HyperText PreProcessor", is a coding language mainly used in the creation of web pages. It is a language that has long since been used to create automated things, which are generally created with loops.

Why PHP? Why not HTML instead?

In the wonderful world of PHP, you get a whole new coding language. If statements, else statements, predefined variables, arrays, the list goes on and on. If you want a good web based coding language, PHP is for you. HTML is not capable of generating dynamic content in the way PHP is, and the two are worlds apart in syntax/general use.




How can I obtain PHP?

obtainYou can obtain PHP by going to the website (http://www.php.net) and downloading the file which suits you best.



Local Webservers

serverEver wanted to test your PHP skills but don't have a webserver? Well, you can set one up on your computer. First, you'll need the proper software (http://httpd.apache.org/download.cgi) to set one up.

Keep in mind, I have an outdated version of Apache so these steps may be somewhat incorrect.

Step 1] Install Apache. For domain, put localhost and for administrator put email@yourdomain.com. For server name, you will want to put 127.0.0.1, and for the next step, put for all users on port 80 as service. Choose typical install, of course.

Step 2] Install PHP. Once installed, copy php4ts.dll into C:\WINDOWS\System32 or wherever your System32 folder is located. Copy php.ini-dist and rename the copy to php.ini and move it to C:\WINDOWS or wherever your windows folder is located.

Step 3] Find your \Apache\conf\ directory and open httpd.conf.

If you want PHP, put this at the very bottom of httpd.conf:

#for the cgi binary (you can use that one compiled with force cgi redirect too)
ScriptAlias /php4/ "c:/location/of/php/"
Action application/x-httpd-php4 "/php4/php.exe"
AddType application/x-httpd-php4 .php

After you insert this, restart Apache. Click the feather icon in your system tray, open it and click "Restart".

Step 4] Navigate to your Apache\htdocs folder. Open up your favourite text editor, and insert the following(if you chose the PHP install):


<?php phpinfo(); ?>

Save as phpinfo.php and go to http://localhost/phpinfo.php

If you did not choose PHP, simply type in "Hello world." and save as index.html, then navigate to http://localhost/index.html

Step 5] You're done! If it worked correctly, you now have a webserver on localhost with a PHP installation. If you encountered any errors during this installation, feel free to send me a private message.




Variables

varVariables are very important in PHP. They are used to store information for later use, or to open things with functions such as fopen() or new COM().

Let's say you want to store 'blah' in $var. You would do this:


$var = 'blah';

This assigns 'blah' to $var. Note that singular quotations will not parse PHP code or special characters, but you can use double quotations to parse variables and special characters such as newlines and tabs.


$var = "\t$var\n";

This will assign $var to $var's old data, adding a tab preceding the data and following it with a line break. Note that in double quotations, you will need to escape(backslash) certain characters in order to treat them as a literal character, otherwise you will end up with a parse error. For this reason, and ease of writing, I recommend using only singular quotations unless you need to parse code and newlines/tabs.

An array is a type of data storage in which a number of indexes point to a respective element containing data. All kinds of data can be stored in an array, they are very commonly used and one of the most key data handling functions to the PHP environment.


$var = array("blah1", "blah2", "blah3");

This would assign an array indexing 0, 1, and 2 to point to "blah1", "blah2", and "blah3", and thus each element can be accessed by using $var[0], $var[1], and $var[2]. Note that if you do not specify a key ID, the index will automatically default to 0 and count upward from there.

You can also assign an array to an array element, thus creating a multidimensional array and allowing for even more flexible arrays. Though these are not as common, it is still good to know as they are quite useful.


$var = array(
'blah' => 'Hello',
'blah2' => 'How are you?',
'blah3' => array(
'blah4' => 'I am fine',
'blah5' => 'Thank you anyways',
),
);

In this array, 'blah3' points to an array that indexes some keys and elements of it's own. In this scenario, you would call blah4 by using $var['blah3']['blah4']. You can find out more about arrays on the official PHP website (http://www.php.net).




Functions

functionsFunctions are used to call things later on that you want to use frequently, instead of repeating the same code over and over, or functions can be used to parse a certain string.


function hello($world)
{
if ($world == "Hello world!") {
$hello = "Hello to you too!";
}
else
{
$hello = "Goodbye!";
}
return $hello;
}

This would set $hello to "Hello to you too!" if $world is "Hello world!" Otherwise, it will set $hello to "Goodbye!"

If and else are statements in most if not all coding languages used to determine if something is blah. Else is used if the conditions in an if statement to not match. Example:


$blah = 10;
while ($blah < 10)
{
if ($blah < 5)
{
echo "Blah is less than five.";
}
else
{
echo "Blah is greater than five.";
}
$blah++;
}

This "if" statement echoes out "Blah is less than five." while $blah is less than five, if it is greater than five it echoes out "Blah is greater than five." and halts the while loop when $blah reaches 10.

The elseif statement

This statement is used in conjunction with if, just as else is. They usually go in this order:


if (conditions)
{
do stuff
}
elseif (conditions)
{
do stuff
}
else
{
do stuff
}

An "if" statement structure will generally be followed by "elseif" if you plan to use 3 or more conditional statements. "if" is always the beginning conditional, with "else" always being the ending conditional. Any other conditionals that you want to evaluate must be used inbetween the two as "elseif"




Operators

operatorsOperators are very important. You will almost always use them in PHP.

Assignment Operators

This example is taken from PHP's website (http://www.php.net).


$a = 3;
$a += 5; // sets $a to 8. Like saying $a = $a + 5;
$b = "Hello ";
$b .= "There!"; // sets $b to "Hello There!", just like $b = $b . "There!";

In the above example, you'll notice two operators, "+"(plus) and "."(dot). The plus operator acts as a mathematical calculation, and will add the integer provided to the existing value of $a. If $a is currently 2, then in the above example, $a is now 7.

The dot operator is used to concatenate two strings together. In the above example, we have set $b to "Hello ", and then below it we have $b .= "There!";. This means that we are adding "There!" onto the end of the existing string allocated to $b, and our string is now "Hello There!".

Optionally, and more frequently, you can use this operator in the middle of code to concatenate strings and more, I most commonly use this to add function data in the middle of a string.


echo "PHP Info:\n\n".phpinfo()."\n\nEnd of PHP Info";

Comparison Operators


$a == $b

$a is equal to $b. This is a comparison usually used to make sure something is what you want it to be.


$a === $b

$a is equal to $b, or they are of the same type. Most commonly used for boolean TRUE or FALSE, also used for NULL and useful for constants defined in the script.


$a != $b

$a isn't equal to $b. This is used when you don't want something to equal a certain thing.


$a <> $b

See $a != $b.


$a !== $b

$a is not equal to $b, or they are not of the same type. Most commonly used for boolean TRUE or FALSE, also used for NULL and useful for constants defined in the script.


$a < $b

$a is strictly less than $b. This is used mostly for mathematical comparison.


$a > $b

$a is strictly greater than $b. See $a < $b.


$a <= $b

$a is less than or equal to $b. See $a < $b.


$a >= $b

$a is greater than or equal to $b. See $a < $b.


$a =& $b

$a is a reference to the content of $b, for all intents and purposes they are the same variable. More on this in the references section.

These operators are very important, as I said previously, and you will use them a lot. If you want to find out more you'll have to visit the PHP website.

Escape Characters

A backward slash can be used to "escape" some characters when using certain characters inside of quotations. Example


echo "This is "quotes" inside of quotes.";

That would not work, because PHP interprets "quotes" as an attempt to close and reopen the string, rather than treating the quotations as a literal character and thus part of the string itself.


echo "This is \"quotes\" inside of quotes.";

The backslashes shown in this example will cause PHP to treat the quotations as a literal character, and part of the string, rather than attempting to close the quotations.




Predefined Variables

prePredefined variables are variables that already exist in PHP. They are there for a specific use, such as how the $_GET[] array is used to parse the URL of the website you are on, and $_GET[] has many uses.

$GLOBALS

Contains a reference to every variable which is currently available within the global scope of the script. The keys of this array are the names of the global variables.

$_SERVER

Variables set by the web server or otherwise directly related to the execution environment of the current script.

$_GET

Variables provided to the script via HTTP GET. More explained in the opening statement.

$_POST

Variables filled via forms, or HTTP POST.

$_COOKIE

Variables provided to the script via HTTP cookies.

$_FILES

Variables provided to the script via HTTP post file uploads.

$_ENV

Variables provided to the script via the environment.

$_REQUEST

Variables provided to the script via the GET, POST, and COOKIE input mechanisms, and which therefore cannot be trusted. The presence and order of variable inclusion in this array is defined according to the PHP variables_order configuration directive.

$_SESSION

Variables which are currently registered to a script's session.

Note: This information was taken directly from the PHP website, as it is quicker and more explanatory than if I had explained it.


Loops

loopsLoops are important if you want to run something more than once, with a difference each time depending on what kind of loop you are using.

While Loops

While loops are the simplest of loops. You can use a wide variety of conditionals to loop for your desired effect, the most common is a variable integer.


while ($a <= 10)
{
echo "'a' is " . $a;
$a++;
}

While $a is less than or equal to 10, the value of $a will be output to the viewer and the value of $a will be increased. As you can see, these are very simple in theory, however they can easily get more complicated as seen below.


while (list($index,$element) = $_GET)
{
echo "Page: $index\nArea: $element\n\n";
}

The above will set the array key and element of each set of HTTP $_GET found in the array, and then print each set of them until the end is reached. Note the use of a single equals operator to denote assignment, meaning the conditional will continue until list() returns a null result.

For Loops

For loops are the most complex loops in PHP. They behave like their C counterparts. The syntax of a for loop is:


for (expr1; expr2; expr3) statement

The first expression (expr1) is evaluated (executed) once unconditionally at the beginning of the loop.

In the beginning of each iteration, expr2 is evaluated. If it evaluates to TRUE, the loop continues and the nested statement(s) are executed. If it evaluates to FALSE, the execution of the loop ends.

At the end of each iteration, expr3 is evaluated (executed).

Example:


for ($i=1; $i<=10; $i++)
{
echo "Variable 'i' is: $i";
}

Foreach Loops

PHP 4 (not PHP 3) includes a foreach construct, much like Perl and some other languages. This simply gives an easy way to iterate over arrays. foreach works only on arrays, and will issue an error when you try to use it on a variable with a different data type or an uninitialized variable. There are two syntaxes; the second is a minor but useful extension of the first:


foreach (array_expression as $value) statement
foreach (array_expression as $key => $value) statement

The first form loops over the array given by array_expression. On each loop, the value of the current element is assigned to $value and the internal array pointer is advanced by one (so on the next loop, you'll be looking at the next element).

The second form does the same thing, except that the current element's key will be assigned to the variable $key on each loop.

Example:


$array = array("one", "two", "three");
foreach ($array AS $index => $element)
{
echo "Key: $index\nValue: $value\n\n";
}




Regular Expressions

regexRegular expressions are a very special and delicate string of characters that will match patterns more or less exactly how you tell them to. The pattern is very sensitive, and there are many operators that require being escaped to be treated as literal strings. I consider these to be one of the most difficult aspects of good programming.

Delimiters

A delimiter is the anchor that denotes the beginning and end of a regular expression pattern. The two most common are the forward slash(/) and hash symbol(#). In general, the forward slash is the most widely accepted, however due to escape character complications, in some patterns you may opt to use the hash symbol to cut back on escape characters and make the pattern easier to read/write.


/example/
#example#

Above is how a pattern encased by delimiters will look.

Anchors

Anchors are special characters or pattern strings that allow you to customize your regular expression pattern. You can make them start at where you want, or end where you want, or even make only whole pattern matches apply rather than partial matches.

'^' is the "beginning of pattern" anchor, however it also serves to negate characters if used inside of a character class(square brackets []).

'$' is the "end of pattern" anchor, and should only be used to denote the end of the match should you choose to do so.


/^example$/
#^example$#

This will match the string 'example' only if the entire string provided is 'example'.

'\b' is a string that is treated as an anchor whose primary use is anchoring the beginning and end of a word. By default, PCRE(Perl Compatible Regular Expressions) will match anything with a partial match. '\b' will make it strictly match the whole pattern only, with no partial matches.


/\bexample\b/
#\bexample\b#

This will match 'example' as a whole word only in the string provided, but will not limit it to only matching the entire string as 'example'.

Character Classes

A character class is a set of characters that is encased inside of square brackets([]) in order to match them inside of a pattern.

The '-' operator is used to specify the range of characters. For example, a-z will work for the entire alphabet, and 0-9 for the entire number range.


/^[a-zA-Z0-9]+?$/
#^[a-zA-Z0-9]+?$#

The above pattern would only match an entire string that is composed of alphanumeric characters, ascii and other special characters would invalidate the match.

Let's suppose that we want to match any word that does not contain any vowels.


/\b[^aeiou]+?\b/
#\b[^aeiou]+?\b/#

The '^' operator used inside of the character class will negate the character sets. In this case, the letters 'aeiou' are negated, and the pattern will only match against whole words containing consenants.

Quantifiers

A quantifier is an operator that is used to denote how many characters we want to match in a pattern. Quantifiers in question are '.'(dot), '+'(plus), '*'(asterisk), '?'(question), and '{}' braces.

The dot operator is not so much a quantifier as the others, however it is used in conjunction with them in order to create stringless matches. A dot operator, by default will match any character that is not a newline.


/.*/
#.*#

Will match any string, period. No pun intended. :)

The '+' quantifier is the most commonly used, and tells the pattern that we want it to match one or more time(s).

The '*' quantifier is also very common, and tells the pattern that we want it to match zero or more time(s). This is particularly useful for optional matches that we do not want to require in the expression, but would like to check for the presence of anyway.

The '?' quantifier is the same as the '*' quantifier, however the main difference is that if you append this quantifier to the end of an existing '+' or '*' quantifier, it will cause the quantifier to become ungreedy. By default, all quantifiers are greedy and will match as much as they possibly can. With an ungreedy quantifier, they will match as little as possible.


/[a-zA-Z0-9]+/
#[a-zA-Z0-9]+#

The above will match any letter or number that is repeated one or more times.


/[a-zA-Z]+[0-9]*/
#[a-zA-Z]+[0-9]*#

The above will match any letter one or more times, and following it any digit if there is one, otherwise it will still match a letter not followed by a digit.


/[a-zA-Z]{3,5}[0-9]{1,3}/
#[a-zA-Z]{3,5}[0-9]{1,3}#

Circular braces serve to denote the minimum to maximum amount of times we want a repetition to occur. In the above example, any letter repeated 3 to 5 times followed by a number repeated 1 to 3 times will be matched.

Subpatterns

Subpatterns are patterns encased in parenthesis which allow you to optionally match part of a pattern, but not require it. This is useful for things such as matching website URLs, and matching email addresses.

The '|' operator works just as it does in PHP. Used inside of a subpattern, you can specify several subpatterns to match, but it will only match one of them.


/^(http:\/\/|https:\/\/)*(www\.)*giveupalready\.com*(\/)*$/
#^(http://|https://)*(www\.)*giveupalready\.com*(/)*$#

The above pattern will match any iteration of giveupalready.com, optionally matching the http:// and https:// prefix, and optionally matching any content following the URL. These matches can get quite a bit more complex, and you can nest subpatterns inside of eachother for more accurate matches.


/^(http:\/\/|https:\/\/|ftp:\/\/)*(www\.)*giveupalready\.com*(\/([a-z0-9]+(\.php|\.php\?[a-z0-9]+=[a-z0-9]+|\.htm|\.html)))*$/iU
#^(http://|https://|ftp://)*(www\.)*giveupalready\.com*(/([a-z0-9]+(\.php|\.php\?[a-z0-9]+=[a-z0-9]+|\.htm|\.html)))*$#iU

In the above example, the pattern will match any GUA URL containing optionally the http://, https://, and ftp:// prefix, also matching, but not requiring the www prefix, following the URL by any additional pages ending in .php, .htm, and .html, also optionally matching the first HTTP $_GET parameter. As you can see, I have ended my pattern with 'iU', these are parameters denoting case insensitivity and ungreedy quantifiers, and are far from the only parameters that can be used at the end of the pattern...

Parameters

At the end of a regular expression, you can denote parameters to further customize your pattern. These include, but are not limited to, 'i', 's', and 'U'.

'i', if specified, will cause the expression match to ignore case sensitivity, and will match any case regardless of what it is.

's', if specified, will cause the dot operator to match all newline(\n \r) characters.

'U', if specified, will cause all quantifiers to act ungreedy, thus acting as if they had a ? appended to the end of them.


As you can see, regular expressions are quite powerful. I have only included very basic explanations here, and much more can be found on http://us.php.net/manual/en/regexp.reference.php
I don't advise using POSIX regular expressions, however if it can't be avoided, you can find information on them here (http://us.php.net/manual/en/book.regex.php).




References

referenceA reference is something that allows you to access existing variables through means of other variables.


function example(&$data)
{
$output =& $data;
foreach ($output AS $index => $element)
{
$output[$index] = preg_replace('#^[^a-z0-9]+$#i','',$element);
}
}
$array = array('abcdefghijklmnopqrstuvwxyz','1234567890','! @#$%^&*()');
example($array);
print_r($array);

In the above example, a reference to $array is passed into $data. At the start of the script, we also pass a reference from $data to $output. Now, all three of our variables are linked, and any changes made to them will affect all three of them. The regular expression will cause any array element that is not alphanumeric to be replaced with nothing, thus leaving the end result as 'abcdefghijklmnopqrstuvwxyz1234567890' due to our imploded string at the end of the function. Note that since we've passed a reference to the function parameter, everything we do to the variable inside the function will affect the data passed to it outside of the function.




Conclusion

closureIn conclusion, PHP is one of the best web based languages. If I had a choice of web based languages to integrate into my website, I would definitely choose PHP.



If I've stated anything wrong in this guide, feel free to send me a PM and I will correct it. Thanks for reading, and I hope this helped you in some way.

Note: Some of this information was taken directly from PHP's website, if you would like to find out more about PHP, more advanced operators and such, visit this website (http://www.php.net).