Skip to content

Instantly share code, notes, and snippets.

@cam-gists
cam-gists / diriterator.php
Created July 2, 2012 21:38
PHP: Directory Iterator
<?php
// create new DirectoryIterator object
$dir = new DirectoryIterator("/my/directory/path");
// loop through the directory listing
foreach ($dir as $item) {
echo $item . "<br>";
}
@cam-gists
cam-gists / dierror.php
Created July 2, 2012 21:39
PHP: Directory Iterator with Errors
<?php
try {
$dir = new DirectoryIterator("/non/existent/path");
foreach ($dir as $item) {
echo $item . "<br>";
}
}
catch (Exception $e) {
echo get_class($e) . ": " . $e->getMessage();
}
@cam-gists
cam-gists / fef.php
Created July 2, 2012 21:41
PHP: File Extension Filter
<?php
class FileExtensionFilter extends FilterIterator
{
// whitelist of file extensions
protected $ext = ["php", "txt"];
// an abstract method which must be implemented in subclass
public function accept() {
return in_array($this->getExtension(), $this->ext);
}
@cam-gists
cam-gists / rdi.php
Created July 2, 2012 21:42
PHP: Recursive Directory Iterator
<?php
// create new RecursiveDirectoryIterator object
$iter = new RecursiveDirectoryIterator("/my/directory/path");
// loop through the directory listing
// we need to create a RecursiveIteratorIterator instance
foreach (new RecursiveIteratorIterator($iter) as $item) {
echo $item . "<br>";
}
@cam-gists
cam-gists / iter.php
Created July 2, 2012 21:50
PHP: Iterator
<?php
// http://phpmaster.com/using-spl-iterators-2/
class Library implements Iterator
{
// an internal pointer to the current position
// in the dataset
protected $position = 0;
// an array of the titles of the books in the library
protected $books = [
@cam-gists
cam-gists / ia.php
Created July 2, 2012 21:52
PHP: Iterator Agregate
<?php
class Library implements IteratorAggregate
{
protected $books = [
"Professional PHP Programming",
"Programming Perl",
"A Byte of Python",
"The Ruby Way"
];
@cam-gists
cam-gists / pdo.php
Created July 2, 2012 22:20
PHP: PDO Proper Query
<?php
$db = new PDO("mysql:host=localhost;dbname=testdb", "dbuser", "dbpassword");
$result = $db->query("SELECT dt_date FROM some_table");
while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
print_r($row);
var_dump($row["dt_date"]);
}
$result->closeCursor();
@cam-gists
cam-gists / PHP: Unix Time Methods
Created July 2, 2012 22:23
PHP: Localization time
<?php
// Localization
$result = $db->query("SELECT int_date FROM some_table");
while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
$dt = new DateTime();
$tz = new DateTimeZone($_SESSION["userTZ"]);
$dt->setTimestamp($row["int_date"]);
$dt->setTimezone($tz);
print $dt->format("r");
}
@cam-gists
cam-gists / time.php
Created July 2, 2012 22:35
PHP: Unix Time Methods
<?php
print time();
//1324402770
$unixTime = time();
print_r(getdate($unixTime));
// Array
// (
// [seconds] => 48
// [minutes] => 54
// [hours] => 12
@cam-gists
cam-gists / varargs.php
Created July 2, 2012 22:58
PHP: Function w/ Variable # of Args
<?php
// http://phpmaster.com/defining-and-using-functions-in-php/
function calcAverage() {
// initialize value to be used in calculation
$total = 0;
// find out how many arguments were given
$arguments = func_num_args();
// loop to process each argument separately