Resources are passed by reference

September 7th, 2005. Tagged: PHP certification

When copying one resource to another, you're actually creating a reference to the original resource, this is not an actual copy.

This is illustrated by the following example:

echo '<pre>';
// create an image resource
$image = imagecreate(100, 100);

// print the resource
// the above prints "resource(30) of type (gd)"

// create a copy 
// (actually creating a reference, 
// although =& is not implicitly used)
$copy = $image;

// print the reference

// the above prints "resource(30) of type (gd)", same as before

// destroy the image resource

// this prints "resource(30) of type (Unknown)"
// so the image was destroyed

// this also prints "resource(30) of type (Unknown)"
// meaning that the reference points to a destroyed image

echo '</pre>';

Tell your friends about this post on Facebook and Twitter

Sorry, comments disabled and hidden due to excessive spam.

Meanwhile, hit me up on twitter @stoyanstefanov