my name is george phillips and i am a freelance web developer based in london

blog

HTML5 – Doctype

The new HTML5 Doctype is <!DOCTYPE HTML>

That’s it!

PHP – Operators

Operator Discription Example
Arithmatic Basic Maths $a + $b
Array Array Union $a + $b
Assignment Assign Values $a = $b + 36
Bitwise Manipulates bits within bytes 12 ^ 9
Comparison Compares two values $a < &b
Execution Executes contents of back ticks '1s -a1'
Increment/Decrement Add or Subtract 1 $a++
Logical Boolean $a and $b
String Concatenation $a . $b

Operators take a different number of operands.

Unary operators ($a++) or (-$a) take a single operand.
Binary operators take two ($a + $b) etc.
One Ternary which is ? x : y

Precedence (High - Low)

Operators

() Parentheses
++ -- Increment/Decrement
! Logical
* / % Arithmetic
+ - . Arithmetic and String
<< >> Bitwise
< <= >= > Comparison
== != === !== Comparison
& Bitwise (and reference)
^ Bitwise
| Logical
&& Logical
|| Logical
? : Ternary
= += -= *= /= %= &= != ^= <<= >>= Assignment
and Logical
xor Logical
or Logical

These are left to right except where operator precedence is in effect. So go right to left. The direction of processing is called associativity.

Operators with right to left associativity:

Operator

NEW Create a new objects
! Logical NOT
~ Bitwise NOT
++ -- Increment and decrement
+ - Unary and negation
(int) Cast to an integer
(double) Cast to a float
(string) Cast to a string
(array) Cast to an array
(object) Cast to an object
@ Inhabit error reporting
? : Conditional
= Assignment

For example, lets look at the assignment separator where all three variables are set to 0.

<?php
  $level = $score = $time = 0
?>


Multiple assignment is possible only if the right hard part of the expression is evaluated first, then processing continues right to left.

PHP – Literals & Variables

The simplest form of expression is a literal which means something that evaluates to itself. An expression can also be a variable, which evaluates to the value that’s been assigned to it.

<?php
  $myname = "George";
  $myage = 27;
  echo "a: " . 73      . " <br />" // Numeric literal
  echo "b: " . "Hello" . " <br />" // String literal
  echo "c: " . FALSE   . " <br />" // Boolean literal
  echo "d: " . $myname . " <br />" // Variable string literal
  echo "e: " . $myage  . " <br />" // Variable numeric literal
?>

These return:
a: 73
b: Hello
c: // false so nothing
d: George
e: 27

In conjunction with operators, it’s possible to create more complex expressions that evaluate to useful results…

When you combine assignment of control-flow constructs with expressions, the result is a statement.

<?php
  $daysToNewYear = 366 - $dayNumber ; // Expression
 &nbsp:   if ($daysToNewYear < 30 )
      {
        echo "Not long to go now until new year!"; //Statement
      }
?>

PHP – Expressions

An expression is a combination of values, variables, operators and functions that results in a value.

y = 3(abs(2x) + 4)

In PHP

  $y = 3 * (abs(2 * $x) + 4);

The value returned (y or $y in this case) can be a number, string or a Boolean value.

Boolean is TRUE or FALSE, 5 == 6 is FALSE

Uppercase Booleans can be redefined. Lowercase cannot.

Some booleans:

<?php
  echo "a : [" . ( 20 < 9 ) . "] <br />",
  echo "b : [" . ( 5 == 6 ) . "] <br />",
  echo "c : [" . ( 1 == 0 ) . "] <br />",
  echo "d : [" . ( 1 == 1 ) . "] <br />",
?>

The output of this code would be:

a: [1]
b: []
c: []
d: [1]

Notice that both expressions a and d evaluate to true, which has a value of 1. But b & c evaluated to false don’t show anything because PHP FALSE is defined as NULL or nothing.

PHP – Variable Scope

Local variables are created in and used inside a function, they can only be accessed by the same function.

Global variables can be called by any function. All your code can access them.

Static Variables can be sued to keep track of how many times a function has been called. It’s contents are not wiped when the function has ended unlike a local variable.

Superglobal variables are predefined variables and contain lots of useful information about the current running program and it’s environment.

Superglobals are often used by hackers trying to find exploits to break into your website. You should always sanitise superglobals before using them… the htmlentities function is a good way to do this.

  $came_from = htmlentities( $_SERVER['HTTP_REFERRER'] );

PHP – Functions

Functions are declared like so:

<?php
  function longdate($timestamp)
  {
    return date("l F jS Y", $timestamp);
  }
?>

To output today’s date with this function, place the following in your code:

  echo longdate( time() );

This call uses the built in PHP time function to fetch the current unix timestamp and passes it the the new longdate function, which returns the appropriate string the the echo command for display.

PHP – Echo vs. Print

Print is like echo but it can be used in more complex functions.

  $b ? print "TRUE" : print "FALSE";

PHP – Constants

Constants are similar to variables except they are immutable. One you have defined one it’s value is set for the rest of the program and cannot be changed.

One example for a constant might be to hold the location of your server root (containing the files of your website) like this:

  define("ROOT_LOCATION", "/user/local/www/");

To read it’s contents you refer to it like a variable (but it isn’t proceeded by the $):

  $directory = ROOT_LOCATION;

Now when you need to run your code on a different server, you only need to change one line of code.

Use uppercase for constants. It’s good practice.

PHP – Loosely Typed

Like the headline says, PHP is loosely typed like JavaScript.

PHP – Multiple Line Commands

There are times when you need to output quite a lot of text and multiple echo statements would be messy and time consuming. There are two ways to combat this.
The first is to just put multiple line in quotes. Variables can also be assigned.

<?php
  $author = "George Phillips";

  echo "This is a Headline

  This is line one
  This is line two
  Written by $author." ;
?>

Or

<?php
  $author = "George Phillips";

  $text = "This is a Headline

  This is line one
  This is line two
  Written by $author.";
?>

PHP also offers a multiline sequence using the <<< operator commonly referred to as here-document or heredoc for short.

<?php
  $author = "George Phillips";

  echo <<<_END
  This is a headline

  Line one.
  Line two.
  Written by $author.

  _END;
?>

This outputs everything between the _END tag if it were a double quoted string. So you can write entire sections of HTML in PHP code and just replace specific dynamic posts with PHP variables.

The closing _END; tag MUST appear right at the start of a new line and the only thing on the line. Not ever a comment is allowed.