+ PHP 7

Reading up on PHP7, notes to self.

To concatenate strings use the . (period)

Single or double quotes

A single quote string will not expand the variable, it will simply print the string. But the value of variable inside a double quoted strings will be expanded.

echo 'Hello ' . 'World!' . "\n";

$out = 'Hello ';
$out .= 'World!';
$out .= "\n";
echo $out;

$name = 'Johan';
echo 'Hello $name!' . "\n";
echo "Hello $name!\n";

Hello World!
Hello World!
Hello $name!
Hello Johan!

Specity the end of a variable name with a curly brace

If a dollar sign ($) is encountered, the parser will greedily take as many tokens as possible to form a valid variable name. Enclose the variable name in curly braces to explicitly specify the end of the name.


$fruit = "apple";
echo "He drank $fruit juice";
echo "<BR>";
echo "He likes $fruit" . "s";
echo "<BR>";
echo "He likes {$fruit}s";

>> He drank apple juice
>> He likes apples
>> He likes apples


$arr = array(1, "two", array("x", "y"));

// short syntax, PHP > 5.4
$arr = [1, "two", array("x", "y")];

echo print_r($arr);


appending to an array, omitting the index will append to the array.

$arr[] = "append me";

Use list() to unpack arrays.

list($a, $b) = $result_array;

Headers, Redirect & Output Buffering

Before the HTML is sent to the browser the server sends a HTTP header (as do the browser to the server).

Example of an HTTP header.

HTTP://1.1 200 OK
Date: Thu, 28 Feb 2015 01:20:16 GMT
Server: Apache/2.2.12

The first line is the version of HTTP used followed by a status code.The header MUST come before any HTML is sent to the server. The php function header($string); is used to send a header. If we want to use the header function after page contents, we must make sure that output buffering is turned on in our php.ini file. It is also possible to enable output buffering on single pages using ob_start(); and ob_end_flush();

An example of a using the header functin to do a redirect. The exit tells the server that the script is done.

header("Location: some_page.php");


URLs/Links GET $_GET

With a GET request PHP automatically takes all query parameters and put them into an associative array assigned to the super global $_GET. With a POST the parameters are assigend to the super global $_POST.