Zendcon 2007 Features

695 views

Published on

Published in: Business, Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
695
On SlideShare
0
From Embeds
0
Number of Embeds
28
Actions
Shares
0
Downloads
18
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Zendcon 2007 Features

  1. 1. <ul><ul><li>PHP Features you didn't know existed </li></ul></ul>Elliott White III – Eli digg.com
  2. 2. What is this talk? <ul><ul><li>Good Question </li></ul></ul><ul><ul><li>A random list of 'neat things' that you can do in PHP, that perhaps you didn't know about. </li></ul></ul><ul><ul><li>At least hopefully there are a few that you haven't heard of. </li></ul></ul>
  3. 3. http_build_query() <ul><ul><li>$vals = array('search' => 'PHP 5 in Practice', </li></ul></ul><ul><ul><li> 'site' => 'digg.com'); </li></ul></ul><ul><ul><li>$query = http_build_query($vals); </li></ul></ul><ul><ul><li>$resend = http_build_query($_GET); </li></ul></ul>
  4. 4. ignore_user_abort() <ul><ul><li>ignore_user_abort(true); </li></ul></ul><ul><ul><li>$bool = connection_aborted(); </li></ul></ul>
  5. 5. Closing Connections Early <ul><ul><li>ignore_user_abort(true); </li></ul></ul><ul><ul><li>header(&quot;Connection: close&quot;); </li></ul></ul><ul><ul><li>header(&quot;Content-Length: &quot; , mb_strlen($response)); </li></ul></ul><ul><ul><li>echo $response; </li></ul></ul><ul><ul><li>flush(); </li></ul></ul>
  6. 6. register_shutdown_function() <ul><ul><li>function shutdown() {} </li></ul></ul><ul><ul><li>register_shutdown_function('shutdown'); </li></ul></ul>
  7. 7. set_time_limit() <ul><ul><li>set_time_limit(600); </li></ul></ul><ul><ul><li>Use with register_shutdown_function to handle timeouts. </li></ul></ul>
  8. 8. range() <ul><ul><li>$array = range(10,20); </li></ul></ul><ul><ul><li>$reversed = range(20,10); </li></ul></ul><ul><ul><li>$stepped = range(10,20,3); </li></ul></ul><ul><ul><li>$alpha = range('a','f'); </li></ul></ul>
  9. 9. readfile() as a passthrough <ul><ul><li>readfile('http://example.com/data.html'); </li></ul></ul><ul><ul><li>file_get_contents('http://example.com/data.html'); </li></ul></ul>
  10. 10. strip_tags() <ul><ul><li>$stripped = strip_tags($input); </li></ul></ul><ul><ul><li>$semi = strip_tags($input, '<p><b>'); </li></ul></ul>
  11. 11. Spellchecking <ul><ul><li>$dict = pspell_new('en'); </li></ul></ul><ul><ul><li>$words = str_word_count($str, 2); </li></ul></ul><ul><ul><li>foreach ($words as $word) { </li></ul></ul><ul><ul><li>if (!(pspell_check($dict, $word))) { </li></ul></ul><ul><ul><li>$suggest = pspell_suggest($dict, $word); </li></ul></ul><ul><ul><li>echo &quot;{$word}: &quot;, implode($suggest, ', '), &quot;<br />&quot;; </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>} </li></ul></ul>
  12. 12. Similar Text / Levenshtein <ul><ul><li>levenshtein() returns the number of changes needed: </li></ul></ul><ul><ul><li>$diff = levenshtein($name, 'Eli White'); </li></ul></ul><ul><ul><li>similar_text() can give you a percentage of similarity: </li></ul></ul><ul><ul><li>$num = similar_text($name, 'Eli White', $percent); </li></ul></ul>
  13. 13. Soundex / Metaphone <ul><ul><li>$key = metaphone($word); </li></ul></ul><ul><ul><li>$key = soundex($word); </li></ul></ul><ul><ul><li>Metaphone is 'supposed' to be more accurate since it understands basic rules of English pronunciation, and generates keys of variable length. </li></ul></ul>
  14. 14. serialize() & unserialize() <ul><ul><li>$var = array('digg' => 1, 'life' => 42); </li></ul></ul><ul><ul><li>$v = serialize($var); </li></ul></ul><ul><ul><li>$var = unserialize(&quot;a:2:{s:4:&quot;digg&quot;;i:1;s:4:&quot;life&quot;;i:42;}&quot;); </li></ul></ul>
  15. 15. Header buffering & headers_sent() <ul><ul><li>Ini File: </li></ul></ul><ul><ul><li>output_buffering = 4096 </li></ul></ul><ul><ul><li>if (!(headers_sent())) { </li></ul></ul><ul><ul><li>setcookie('what', 'It Works'); </li></ul></ul><ul><ul><li>} </li></ul></ul>
  16. 16. @ symbol <ul><ul><li>$result = @function(); </li></ul></ul><ul><ul><li>@$val += 1; </li></ul></ul><ul><ul><li>$whatever = @$_GET['v'] ? $_GET['v'] : 'default'; </li></ul></ul>
  17. 17. @ vs. isset() vs. array_key_exists() <ul><ul><li>@$var['a']['b'] </li></ul></ul><ul><ul><li>isset($var['a']['b']) </li></ul></ul><ul><ul><li>array_key_exists('b', $var['a']) </li></ul></ul>
  18. 18. trigger_error() <ul><ul><li>trigger_error(&quot;Whoops&quot;, E_USER_ERROR); </li></ul></ul>
  19. 19. Direct access of globals - $GLOBALS <ul><ul><li>$GLOBALS['myvar'] </li></ul></ul>
  20. 20. fscanf() using character sets <ul><ul><li>fscanf($file, &quot;%3s*%40[^ ] %f &quot;, $a, $b, $c); </li></ul></ul><ul><ul><li>fscanf($file, &quot;%3[a-c] %6[0-9] &quot;, $a3, $a6); </li></ul></ul><ul><ul><li>fscanf($file, ' &quot;[^&quot;]&quot; &quot;[^&quot;]&quot; '); </li></ul></ul>
  21. 21. do {} while (false); <ul><ul><li>do { </li></ul></ul><ul><ul><li>if (something()) { </li></ul></ul><ul><ul><li>break; </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>somethingelse(); </li></ul></ul><ul><ul><li>} while (false); </li></ul></ul>
  22. 22. php://input & php://stdin <ul><ul><li>Command line input: </li></ul></ul><ul><ul><li>file_get_contents('php://stdin'); </li></ul></ul><ul><ul><li>Raw Post data: </li></ul></ul><ul><ul><li>file_get_contents('php://input'); </li></ul></ul>
  23. 23. php://filter <ul><ul><li>file_put_contents('php://filter/write=string.toupper|string.rot13/resource=myfile.rot'); </li></ul></ul><ul><ul><li>readfile('php://filter/read=string.strip_tags </li></ul></ul><ul><ul><li>/resource=http://digg.com'); </li></ul></ul><ul><ul><li>var_dump(stream_get_filters()); </li></ul></ul>
  24. 24. compress.zlib:// <ul><ul><li>file_put_contents('compress.zlib://gzipfile.gz', 'DATA'); </li></ul></ul><ul><ul><li>readfile('compress.zlib://gzipfile.gz'); </li></ul></ul>
  25. 25. Generating lists of months/weekdays <ul><ul><li>foreach (range(1,12) as $month) { </li></ul></ul><ul><ul><li>$daystamp = mktime(0, 0, 0, $month); </li></ul></ul><ul><ul><li>$monthnames[] = date('F', $daystamp); </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>foreach(range(1,7) as $day) { </li></ul></ul><ul><ul><li>$stamp = mktime(0, 0, 0, 1, $day); </li></ul></ul><ul><ul><li>$days[date('N', $stamp)] = date('l', $stamp); </li></ul></ul><ul><ul><li>} </li></ul></ul>
  26. 26. Including code from other servers <ul><ul><li>include 'http://example.com/file.inc'; </li></ul></ul>
  27. 27. Possible Other Topics <ul><li>Output Buffering </li></ul><ul><li>SPL Iterators </li></ul><ul><li>Assertions </li></ul><ul><li>Exif </li></ul>
  28. 28. Digg is Hiring! <ul><li>Digg.com is hiring experienced PHP programmers! </li></ul><ul><li>http://digg.com/jobs </li></ul><ul><li>[email_address] </li></ul>
  29. 29. Any Questions? <ul><ul><li>For this presentation and more: http://eliw.com/ </li></ul></ul>Visit http://digg.com/

×