This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| // create new DirectoryIterator object | |
| $dir = new DirectoryIterator("/my/directory/path"); | |
| // loop through the directory listing | |
| foreach ($dir as $item) { | |
| echo $item . "<br>"; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| try { | |
| $dir = new DirectoryIterator("/non/existent/path"); | |
| foreach ($dir as $item) { | |
| echo $item . "<br>"; | |
| } | |
| } | |
| catch (Exception $e) { | |
| echo get_class($e) . ": " . $e->getMessage(); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?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); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?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>"; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?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 = [ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| class Library implements IteratorAggregate | |
| { | |
| protected $books = [ | |
| "Professional PHP Programming", | |
| "Programming Perl", | |
| "A Byte of Python", | |
| "The Ruby Way" | |
| ]; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?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(); | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?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"); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| print time(); | |
| //1324402770 | |
| $unixTime = time(); | |
| print_r(getdate($unixTime)); | |
| // Array | |
| // ( | |
| // [seconds] => 48 | |
| // [minutes] => 54 | |
| // [hours] => 12 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?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 |