Software Engineers Handbook/Language Dictionary/PHP

PHP
, which stands for 'PHP: Hypertext Preprocessor', is an open source scripting language. It can be embedded in HTML, similar to how JSP.

Type
PHP is a little hard to classify. The creators declare that it's a web development scripting language and, well, it is. Though it's largely procedural and has slowly gone towards being an object oriented language (PHP 5). So, really PHP can be what you want it to be. Though note, it's an interpreted language, which can be compiled down some. PHP.net's introduction pages

Execution Entry Point
PHP is generally accessed from a webserver processing an html page with some php in it, or a php file.

In an html file:

Or you can create an entire php file by enclosing the entire thing in the <? and ?>. The general trend is to call these files .php files for files that render something like html and .inc files for library files.

General Syntax
Variables contain a '$' in front of them and do not need to be declared. Lines end with a semicolon, like in C. As with most web development scripting languages, there are a few global variables, like $_SESSION and $_POST, $_GET, and $_REQUEST.

A general assignment goes like this:

Comments
// this is an inline comment. Everything after the // is a comment. Block comments are specified by a starting /* and ending */ They can span multiple lines.

Php contains phpdoc - where you can create an html manual from comments before the function declaration. They look like this:

There's a few other @ types you can use here. Sources of how to generate phpdocs are needed. More to come.

Variable Declarations
declare i as an integer $i = 1;

Or: $str = 'string';

There's no datatype declaration required, so you can change a variables datatype though assigning it to something else:

$foo = 1; $foo = 'string';

One stumbling block with this is that you need to know how to concat strings versus adding values.

For concating strings you can use the "." or ".=". If you want to add values use "+" or "+=".

If you do this: $foo = 'string'; $foo += ' is cool';

You'll end up with some number verus "string is cool", as you've added strings and not concatinated them.

Method Declaration/Implementation
Function declaration:

You can default parameters like this:

You don't need to declare any return value.

When you declare a function to use with in a class, it looks just the same as the one shown above, with the only difference is with it being physically located inside a class boundary.

Scope
All variables have a single scope in PHP. If it's in a function, then it has local scope. If it's outside a function, then it has global scope.

You can create a global variable in two ways. The first way is by putting it outside a function. The second way is to declare it this way:

global $foo;

This makes it available from anywhere.

To access a global value from inside a function you need to declare that you're using it, otherwise php thinks it's completely different from the global variable.

YES:

NO:

PHP will create the $foo inside of the function and not associate it with the global $foo. The two $foo's are completely different entities in the "NO" example.

$bar is local and its scope is limited to the function.

PHP has excellent documentation on scoping available.

Conditional Statements
There's the typical if/else statements:

Braces can be removed as long as there's only a single statement after the condition:

Another valid way to write an if statement is like this:

$bar = (($foo == true) ? 'test' : 'not test');

The first two are a bit easier to read.

There's also a case/switch conditional:

Please visit php's structure page for more information and alternate ways conditions can be written.

Looping Statements
PHP includes the traditional loops - a for loop, while and do while. There's a foreach loop as well.

For:

PHP has a special foreach loop that's useful for dealing with arrays:

While:

or

Do while:

Please visit php's loop page for more information on loops and alternative ways of writing them.

Output Statements
Here's how to print out 'Hello World!' in PHP.

echo 'Hello World!';

When you want to include a PHP variable inside the string you need to use double quotes, as PHP will not parse the data between single quotes, it will just print it out.

$foo = 'bar'; echo "foo$foo";

// prints out foobar;

It's good to use single quotes when there's no variables in the string as it saves processing time.

Error Handling/Recovery


Containers
PHP has an array as a native data type. To use:

Arrays in PHP are very powerful and useful. They can be used like a hash, as in the first and third example - you can use index's and strings into the arrays. You don't necessarily need to declare the array, though it's a good practice. Also, you can just write two brackets, as in $foo[], to signify that you want the data to go into the next index, as in example 2.

Algorithms


Garbage collection


Physical Structure


Tips


Web References
There's a great PHP library at php.net. This has the manual there as well as library pages - so it's probably appropriate for any user who's familiar with programming to some degree. You'll definitely need this page as a reference for the libraries.

Books and Articles
