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

edit

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

edit

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

In an html file:

 <html>
 <body>
 Hello World! <br>
 Today is 
 <?
    $date = date('D M dS');
    echo $date;
 ?>
 </body>
 </html>

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

edit

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:

 $a = 'bar';
 $b = 4;

Comments

edit
// 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.

 /*
  * this is a block comment 
  */

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

 /**
  * Function description
  *
  * @param  <datatype>   <$name of var>   <description of var>
  * 
  * @return <datatype>
  */

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

Variable Declarations

edit

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

edit

Function declaration:

 function foo( $param1, $param2 ) {
     return 'hello';
 }

You can default parameters like this:

 function foo( $param1, $param2=100 ) {
     return 'hello';
 }

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.

 class Foo { 
  function bar () {
      return 'something';
  }
 }

Scope

edit

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:

 $foo = 2;
 function foo() {
     global $foo;
     $bar = 1;
     return $foo + $bar;
 }

NO:

 $foo = 3;
 function foo() {
     $bar = 1;
     return $foo + $bar;
 }

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

edit

There's the typical if/else statements:

 if( $a == $b ) {
     $c = $d;
     // can put more statements here
 } else if ( $b == $c ) {
     $c = $e;
     // can put more statements here
 } else {
     $c = $f;
     // can put more statements here
 }

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

 if( $a == $b ) 
     $c = $d; // only one statement allowed
 else 
     $c = $f; // only one statement allowed

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:

 switch ($foo) {
     case 'foo':
        echo 'bar';
        break;// breaks out of the switch case, to prevent us from running 
              // the stuff below this. 
     case 'bar':
        echo 'foo';
        break; // breaks out of the switch case, to prevent us from running 
               // the stuff below this. 
     default: 
        echo 'empty';
 }

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

Looping Statements

edit

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

For:

 // prints out the numbers from 0 to 4, as soon as $i equals 5 it stops. 
 for(
     echo $i;
 }

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

 foreach( $fooArray as $key=>$value ) {
     // starting at the begining of the array it will give the key (index) 
     // and value for each item.
 }

 // you can also just say:
 foreach( $fooArray as $value ) {
     // you don't need to use $key
 }

While:

 // this counts backwards from 100 printing out each number like this 
 // "100...99...98..." and so on until it reaches 0, then stops
 $i = 100;
 while( $i > 0 ) {
     echo "$i...";
     $i--;
 }
or
 // keeps checking the $cond variable and stops when it equals false
 // if $cond == false from the start, the stuff in the loop will never execute. 
 while( $cond != true ) {
     // do something
 }


Do while:

 // this does the same thing as the above "for" loop, only it always executes 
 // at least once before it checks if the condition ($i <5) has been met, 
 // or is still good. 
 $i = 0;
 do {
     $i++
     echo "$i...";
 while( $i < 5 );

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

Output Statements

edit

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

edit

<Describe error handling and recovery. Give examples as appropriate.>

Containers

edit

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

 // example 1
 $ages = array();
 $ages['Bobby'] = 12;
 $ages['Rob'] = 12;

 // example 2
 $foo = array();
 $foo[0] = 8;
 $foo[1] = 9;
 $foo[] = 10; // puts 10 in $foo[2]

 // example 3
 $bar[0] = 12; // don't need to specifically declare $bar, it's just nice to. 
 $bar['total'] = 3;
 $bar[] = 13; // put's 13 in $bar[1]

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

edit

<List algorithms or references to lists of algorithms available natively for this language. List ways to incorporate algorithms if they are not native to the language. Or, if not available, describe that.>

Garbage collection

edit

<Describe whether the garbage collection is automatic or manual.>

Physical Structure

edit

<Describe how the files, libraries, and parts are typically divided and arranged.>

Tips

edit

<Please include tips that make it easier to switch to this language from another language.>

Web References

edit

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

edit

<List additional books and articles that may be helpful. Please include for what level reader the references are appropriate. (beginner/intermediate/advanced)>