Monday, 29 June 2015

Understanding variables in PHP

PHP defaults to pass by reference instead of pass copy, What this means is that in older versions of PHP passing a variable to a function would have passed a copy of the variable to the function thus using x2 amount of memory.  PHP was updated to pass a reference as default which would only make a copy if you changed the variables content.  To pass an actual reference to a variable you would have to put the & symbol before your variable for example making the scope of the functions variable be external to the function.

fn(&$x);


The & represents the address of a variable;

a pointer is a number 0...n which when used in conjunction with software development represents a memory address.

so for example


$list = Array(...);


the variable $list holds an Array of information or in other words points to an area of memory

lets say that $list holds 1Mb of information but since that information is really a block of memory (1048576 bytes) and lets say that the first bit of information is in the memory address 1,000,000 and for the purpose of this conversation it is a solid block of memory with no fragmentation so the end point is (1000000+1048576) = 2,048,576

Since php now passes variables by reference

1 $results = fn($list );
2
3 function fn($val){
4 echo $val;
5 }


the variable $val is a pointer to the same memory space as $list and thus can be said to have a "reference" to the data. So what does this mean?

well instead of php having to have two variables $list & $val both containing 1Mb of data in memory at the same time (said to be a copy) we can pass a 32/64bit pointer which references the original value.

Now the interesting part if we added an additional line between 3&4

$val[count($val)] = 'woo';


this would cause php to make a complete copy of the data at that memory location and then change hte memory location that $val points to. Thus any changes to $val would not effect $list; as it is now a local variable to the function.

if you want changes to be applied the $list variable then change line one to be $result = fn(&$list);

Friday, 20 February 2015

Will Apple launch a games console this year?


Since the moment that Apple announced Metal my first thought was not of the phone but of a fantastic upgrade for an AppleTV.  If Apple added the Latest processor to the Apple TV and allowed apps for your TV.  Then you would just have to add a couple of good game controllers and you have a fantastic media console with Siri and the ability to play console quality games on.  So for £150-£160 you could have a fantastic Gaming machine with two controllers.  Thats cheaper than the Nintendo Wii on launch in 2006.

If you think about it why would Apple want to produce a physical TV unit. My HD TV is 8 years old and I have no intention of replacing it any time soon.  But a new console which would be £100 upgrade every 2-3years  would just blow Sony & Microsoft out of the way.  Not to mention that this would be a great opportunity for indie game developers to get into homes.

Nintendo sold 3.19M Units in the first quarter.  How many people would buy an Apple TV instead of a £400 Playstation or XBox.  After all it's easier to find £150 that £400.


If I knew were to look I would be looking at who makes components for controllers and see if they are buying large quantities.

Fingers crossed.