fwrite() has a Double Tip that is Misplaced

On page 131, the fwrite() function has a TIP that reads:
To read the entire file into a string, use the function filesize().

// file.txt contains the sentence: Hello World!
$filename = 'file.txt';
$file = fopen($filename, 'r');
$string = fread( $file, filesize($filename) );
// file.txt now contains at the end: Hello World!

You’ll notice that this is the same tip as fread() and doesn’t belong. It should be considered ‘removed’.

PHP & Ampersand: Passing by Reference

The following PHP Reference excerpt is from pages 20-21.

& – Pass by Reference

References allow two variables to refer to the same content. In other words, a variable points to its content (rather than becoming that content). Passing by reference allows two variables to point to the same content under different names. The ampersand ( & ) is placed before the variable to be referenced.


$a = 1;
$b = &$a; // $b references the same value as $a, currently 1
$b = $b + 1; // 1 is added to $b, which effects $a the same way
echo "b is equal to $b, and a is equal to $a";
b is equal to 2, and a is equal to 2


Use this for functions when you wish to simply alter the original variable and return it again to the same variable name with its new value assigned.

function add(&$var){ // The & is before the argument $var
$a = 1;
$b = 10;
echo "a is $a,";
echo " a is $a, and b is $b"; // Note: $a and $b are NOT referenced
a is 2, a is 2, and b is 11

You can also do this to alter an array with foreach:

$array = array(1,2,3,4);
foreach ($array as &$value){
$value = $value + 10;
unset ($value); // Must be included, $value remains after foreach loop
Array ( [0] => 11 [1] => 12 [2] => 13 [3] => 14 )

What tricks do you have for using the ampersand in PHP to pass by reference?
Leave them in the comments below!

Strip_tags() – Less Than you Bargained For

While this may be handy for removing HTML from a string, be forewarned that the function is a lot less picky than you may think when it comes to the less than symbol ( < ). First, the section from the PHP book:

strip_tags($string [, allowed_tags])

allowed_tags – [optional] $string

Remove HTML tags and comments from $string. If specific tags should be
excluded, they can be specified inside allowed_tags.

$string = "<p>This is a paragraph. </p><strong>Yay!</strong>";
echo strip_tags($string), strip_tags($string, '<p>');

HTML Source Code:

This is a paragraph. Yay! <p>This is a paragraph. </p>Yay!

So what happens to the following example, when we want to remove all the tags? Fair warning, something strange happens:

$string = "I <strong>love</strong> this book because it costs <$20.";
echo strip_tags($string);

HTML Source Code:

I love this book because it costs

As you can see, it removed the <$20 portion of the string as well, even without the closing greater than ( > ) tag at the end. Be careful when using strip_tags(), especially without specifying the allowed tags, or consider using an alternate such as htmlspecialchars() to encode the characters into their html equivalent rather than removing them.