PHP Reference

PHP is a logical and powerful Server-Side scripting language, but it can have complex syntax and tricky  concepts to master. This article is provided as a useful reference to the most common PHP statements and commands, helping you to quickly and effectively code your projects.

 

PHP Statements

array  ( "key" => "value", ...);

array is usually written on a single line, with comma-seperated values and queries written within brackets.

die("message");

die's "message" must be a regular string. You can use variables to display the information within brackets.

do {block} while (condition);

You can do multiple functions by wrapping them in curly braces, however don't forget the condition is always in regular brackets. While comes after the block of functions specified in do.

echo item;

You can echo anything,  variables, HTML or plain strings. To echo values of variables, put them in double quotes (""). To echo the variable names themselves, put it in single quotes (' ').

explode(delimiter, $string );

explode splits a string by a string. Delimiter is the character to split by, for example, |. $string is the variable string that you want exploded. Remember to wrap this all in brackets.

extract ($array);

You can extract data from an array by using extract. $array is the variable the array is held in. Put this in regular brackets.

for (startingvalue; endingvalue; increment) {block};

for processes a block of commands as many times as it takes PHP to count from startingvalue to endingvalue, in increment. For example, you could use for (5; 10; 1) {echo "Hello world!};. This would echo Hello World! to the screen five times, since 5 to 10 in ones goes 5 times. Don't forget startingvalue, endingvalue and increment must be semi-colon seperated and wrapped in brackets, whilst the block of commands is in curly braces.

 foreach ( $array as $key => $value) {block}

foreach processes the block of statements in curly braces through each value of the array specified in $array.

function functionname(value,value...) {block}

The function statement is very useful in PHP. It lets you use the block of code, block, specified in curly braces, as many times as you like simply by calling the functionname in your code. You can pass values to the function by specifying them in (value,value), comma-seperated in brackets.

header ("Location: URL");

header redirects the page instantly to the URL specified after Location: . Not that this must be within brackets, then within double-quotes (""). Location must have a colon and space following for best syntax.

if (condition1) {block1}

    elseif (condition2) {block2}

    else {block3}

The if statement is very powerful in PHP. If condition1 is met, then block1 will process. Else, if condition1 is not met, and condition2 meets, then block2 will process. Finally, if both condition1 AND condition2 fail, block3 will process. Don't forget the conditions must be in regular brackets and the blocks in curly braces.

implode($glue,$array);

implode joins array elements in $array with the glue, $glue. $glue could be a comma, space, or any other string used to seperate values in the array.

session_start();

session_start() begins a new PHP session on the server, and creates user cookies and data on the server.

session_destroy();

session_destroy() ends the session and stops data being written to user cookies or to the server. 

switch var {case value statements break; ...}

The switch statement is similar to the if statement, but it lets you run multiple statements in the same expression. In many cases, you may want to compare the same variable or value with many different values - this is where switch is helpful. Remember to wrap everything in curly braces.

unset(variable);

unset destroys any variables placed inside its brackets. 

while (condition) {block}

while executes the code placed in curly brackets, block, all the time that the condition is met. Be careful with while, as you can easily run into infinite loops.

 

Regular Expressions

The following are special characters used in pattern matching for regular expressions. The characters are followed by a hashs (#), followed by their meaning. The two hashes seperate meaning from symbol and are not used in the expressions.

^ # Beginning of line

$ # End of line

. # Any single character

? # Preceding item is optional

( ) # Groups literal characters

[ ] # Any character in set

[^ ] # Any character not in set

+ # One or more

* # Zero or more

{ , } # Range of repetitions

\ # Escapes character

( | | ) # Alternative strings