PHP Reference Book Blog

PHP Reference: Beginner to Intermediate PHP5

Archive for December, 2009

The getcwd() function is short for ‘GET Current Working Directory’. This can easily be combined with the scandir() function which returns an array of all the files and directories inside the specified directory. This tip was excluded from the book as an oversight.
 
A quick way to get a list of all the contents of the current directory is to use the following code:

function preprint($arr){
  echo '< pre>'.print_r($arr).'< /pre>';
}
$array = scandir(getcwd());
preprint($array); // nicely formatted display of the array

 
Of course, you can skip the print/echo portion if you don’t wish to display the contents and just use the array to perform other checks, but you get the idea.

Dec-21-09

fwrite() has a Double Tip that is Misplaced

posted by Mario Lurig

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) );
var_dump($string);
// 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’.

Dec-20-09

Index: array_combine() array_key_exists()

posted by Mario Lurig

Some quick corrections for the Function Index:

pg. 162

preg_match_all: 157-158

pg. 161

array_combine: 73
array_key_exists: 79-80