Blog

PHP – Problem with issetor()

In case you haven’t seen it before, issetor() is a quick and dirty way of getting a value from a variable that may or may not be defined: function issetor(&$var, $default=NULL) { return isset($var) ? $var : $default; } One unfortunate side-effect that most people are unaware of is that Read more…

By Chris West, ago
Blog

PHP Snippet – array_filter_keys()

While working on YourJS.com I was trying to figure out a quick way to use extract() to only convert some of the $_REQUEST values to variables in the symbol table. Then I realized I could use this: function array_filter_keys($array, $keys_to_include) { return array_intersect_key($array, array_flip($keys_to_include)); } Using this will give you Read more…

By Chris West, ago
Blog

PHP – XML To JSON Proxy

Today I was looking for a way to easily pull in RSS feed data from Apple Trailers onto my site via AJAX. At first I was thinking about using jQuery to do it but then I got thinking about how cool it would be to have a PHP script that Read more…

By Chris West, ago
Blog

PHP – Foreach By Reference

Today I was writing some code in which I wanted to modify the values of an array within a for-loop. The obvious solution would be to use the key to modify the value but there is actually another way of doing it: $arr = array(1,2,3,4,5); foreach ($arr as &$value) { Read more…

By Chris West, ago
Blog

Here Document

Something that you may not know about PHP and many other languages is that it provides heredoc syntax to build strings. What does this mean? Check out the following: $title = 'Test HTML Page'; $myCode = <<<STR <html> <head> <title>$title</title> </head> <body> <h1>$title</h1> <p>This is a test page to show Read more…

By Chris West, ago