FairFX SMS Codes

To find balance:

FAIR BAL 6789 to 60040 where 6789 is the last 4 digits of your FairFX currency card

To top up

Text FAIR 300 USD 6789 to 60040 where 300 is the amount, USD is the requested currency (GBP, EUR or USD) and 6789 is the last 4 digits of your FairFX currency card. .

then….

Text FAIR BUY 123 6666 to 60040 where 123 is the 3 digit security number (CVV) from the back of your payment card and 6666 is the last 4 digits of your payment card number. Your payment card is the debit or credit card you want to use to buy the currency, the details of which you have already saved on this site. No charge is levied by FairFX for this service, ordinary text rates apply.

PHP 5.4.0 New Features

With the PHP 5.4.0 RC3 released last week, 5.4.0 will be arriving soon. As well as a whole bunch of bug fixes, there are a few new interesting features. Here is a selection of some of the more interesting or handy features

Magic quotes removed

The 4th August PHP 5.4.0 Alpha 3 was the version where they removed magic quotes. (Finally!)

- Removed features:
  . Removed magic_quotes_gpc, magic_quotes_runtime and magic_quotes_sybase
    ini options. get_magic_quotes_gpc, get_magic_quotes_runtime are kept
    but always return false, set_magic_quotes_runtime raises an
    E_CORE_ERROR. (Pierrick, Pierre)

@ Operator speed increase

Using the @ operator (silence) to ignore any errors has been given some speed increases, which should help scripts that use a lot of them.

$file = @file_get_contents('/invalid'); // faster in 5.4.0

$GLOBALS initialized only if used

The $GLOBALS variable is only initialised if it is used. May cause bugs in existing scripts.

Array dereferencing support added

A neat trick that means you don’t have to store the returned result to a temporary variable when you want to access a certain array item. In < php 5.4.0 you had to do this:

function example() {
return array(
1 =&gt; 'first result',
2 =&gt; 'result you want'
);
} 

$tmp =  example();
echo $tmp[2];

But in php 5.4.0 you can do this:


function example() {
return array(
1 =&gt; 'first result',
2 =&gt; 'result you want'
);
} 

echo example()[2];

Binary value support added

Binary values can be used in php, for example:

$num = 0b001010

Support for traits has been added

Traits are a new feature to PHP that lets you easily reuse code in classes. Because a class can only inherit one class (although of course the parent class(es) could inherit more), using traits allows you to easily reuse sets of methods in different classes.

Traits are written in a similar way to classes, but they cannot be instantiated.

Example of a trait:


<?php

trait example_trait {

	function example_function_from_trait() {
		echo 'example';
	}

}

class example_class1 {
	use example_trait; // <<--
	function __construct() {

		$this-&amp;gt;example_function_from_trait();
			// uses function from the trait

}

}

?>

Traits have now been added to php 5.4.0.

Using Anonymous Functions when stored as an array

In the previous version of PHP you could store anonymous functions in arrays, but to call the anonymous function it required a temporary variable.

The old way of doing this (PHP 5.3.x):

<?
// set up the anonymous function, stored in an array
$anon_funcs = array();
$anon_funcs['sayhello'] = function()
{
echo 'Hello World!';
}
?>

To call it in php 5.3.x:

<?
$tmp = $anon_funcs['sayhello'];
$tmp();

//$anon_funcs['sayhello']() would not work
?>

But now you don’t need that temporary variable, so in php 5.4.0 you can call it by just doing this:

<?
$anon_funcs['sayhello']();
?>

Which makes code shorter and neater.

Short array syntax added

No need for array(), you can just use [ and ]

$array = [123,456,789101112];

Built in web server (for testing)

PHP now has a new SAPI module (cli-server) which is a built in web server that is intended for testing purposes. Here are some examples of using PHP’s built-in web server (from their docs).

It is run from the command line with

php -S localhost:8000

That command will use the document root of the current working directory. (So do the following command first to move to the directory you want to serve)

$ cd ~/public_html
$ php -S localhost:8000

If you want to start with a different document root for example ~/webfiles/), use this command:

php -S localhost:8000 -t ~/webfiles/

Then visit http://localhost:8000/ in your browser to view the pages.

You can use router scripts, which will ‘redirect’ to the requested file.

If you use a router script and want to return the requested file (for example if a user goes to http://localhost:8000/image.jpg, and you want to show image.jpg) you must return false. This example should make sense –

<?php
// router.php
if (preg_match('/\.(?:png|jpg|jpeg|gif)$/', $_SERVER['REQUEST_URI']))
    return false;    // serve the requested resource as-is.
else {
    echo 'Welcome to PHP'
}
?>

When using the following command, any image requests (for example http://localhost:8000/image.png) will show the image (if it exists of course), because the script will return false. If it isn’t a png/jpg/jpeg/gif request, it will echo ‘Welcome to PHP‘.

$ php -S localhost:8000 router.php

E_ALL now includes E_STRICT

E_ALL never used to include E_STRICT, you had to explicitly enable it if setting E_ALL. In PHP 5.4.0, E_STRICT is included in E_ALL.

Closures in Classes

write up on closure changes here

Making Kevin Luck’s jQuery DatePicker library select the last day of the week

If you use http://www.kelvinluck.com/assets/jquery/datePicker/v2/demo/ – Kevin Lucks jQuery DatePicker v2 and are using the selectWeek option but want the last day of the week (i.e. Sunday) to be the date in the textbox, make these changes

change this:

 d = d.addDays(- (d.getDay() - Date.firstDayOfWeek + 7) % 7);

to this (add the +6, so it selects a date 6 days after the default. Modify this if you need a different day of the week. Can also play with this if you need the week before selected etc. Have a search in the source code for ‘selectWeek’.)

 d = d.addDays(- ((d.getDay() - Date.firstDayOfWeek + 7) % 7) + 6);

so it looks like:

			setSelected : function(d, v, moveToMonth, dispatchEvents)
{
	if (d &amp;lt; this.startDate || d.zeroTime() &amp;gt; this.endDate.zeroTime()) {
		// Don't allow people to select dates outside range...
		return;
	}
	var s = this.settings;
	if (s.selectWeek)
	{
		d = d.addDays(- ((d.getDay() - Date.firstDayOfWeek + 7) % 7) + 6);
		if (d &amp;lt; this.startDate) // The first day of this week is before the start date so is unselectable...
		{
			return;
		}
	}

mysql connect in php

Know all the other main mysql functions off by heart. *Always* forget the order of mysql_connect’s variables. Even though they are obvious!

$conn = mysql_connect('localhost', USERNAME, PASSWORD) or die ('Could not connect to db');
mysql_select_db(DATABASE);

although mysqli and pdo are safer/better to use.

mysqli

$mysqli = new mysqli('localhost', USERNAME, PASSWORD, DATABASE);

pdo:
http://php.net/manual/en/ref.pdo-mysql.php

<?
$pdosettings = array(
    PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"
);

$pdo = new PDO(
    'mysql:host=HOSTNAME;dbname=DATABASENAME',
    'USERNAME',
    'PASSWORD',
    $pdosettings
); 

?>