Zendcon 2007 Features

29
PHP Features you didn't know existed Elliott White III – Eli digg.com

Transcript of Zendcon 2007 Features

Page 1: Zendcon 2007 Features

PHP Features you didn't know existed

Elliott White III – Elidigg.com

Page 2: Zendcon 2007 Features

Oct 10, 2007 PHP Features You Didn't Know Existed

What is this talk?

Good Question

A random list of 'neat things' that you can do in PHP, that perhaps you didn't know about.

At least hopefully there are a few that you haven't heard of.

Page 3: Zendcon 2007 Features

Oct 10, 2007 PHP Features You Didn't Know Existed

http_build_query()

$vals = array('search' => 'PHP 5 in Practice', 'site' => 'digg.com');

$query = http_build_query($vals);

$resend = http_build_query($_GET);

Page 4: Zendcon 2007 Features

Oct 10, 2007 PHP Features You Didn't Know Existed

ignore_user_abort()

ignore_user_abort(true);

$bool = connection_aborted();

Page 5: Zendcon 2007 Features

Oct 10, 2007 PHP Features You Didn't Know Existed

Closing Connections Early

ignore_user_abort(true);header("Connection: close");header("Content-Length: " , mb_strlen($response));echo $response;flush();

Page 6: Zendcon 2007 Features

Oct 10, 2007 PHP Features You Didn't Know Existed

register_shutdown_function()

function shutdown() {}

register_shutdown_function('shutdown');

Page 7: Zendcon 2007 Features

Oct 10, 2007 PHP Features You Didn't Know Existed

set_time_limit()

set_time_limit(600);

Use with register_shutdown_function to handle timeouts.

Page 8: Zendcon 2007 Features

Oct 10, 2007 PHP Features You Didn't Know Existed

range()

$array = range(10,20);

$reversed = range(20,10);

$stepped = range(10,20,3);

$alpha = range('a','f');

Page 9: Zendcon 2007 Features

Oct 10, 2007 PHP Features You Didn't Know Existed

readfile() as a passthrough

readfile('http://example.com/data.html');

file_get_contents('http://example.com/data.html');

Page 10: Zendcon 2007 Features

Oct 10, 2007 PHP Features You Didn't Know Existed

strip_tags()

$stripped = strip_tags($input);

$semi = strip_tags($input, '<p><b>');

Page 11: Zendcon 2007 Features

Oct 10, 2007 PHP Features You Didn't Know Existed

Spellchecking

$dict = pspell_new('en');$words = str_word_count($str, 2);foreach ($words as $word) {

if (!(pspell_check($dict, $word))) {$suggest = pspell_suggest($dict, $word);echo "{$word}: ", implode($suggest, ', '), "<br />";

}}

Page 12: Zendcon 2007 Features

Oct 10, 2007 PHP Features You Didn't Know Existed

Similar Text / Levenshtein

levenshtein() returns the number of changes needed:

$diff = levenshtein($name, 'Eli White');

similar_text() can give you a percentage of similarity:

$num = similar_text($name, 'Eli White', $percent);

Page 13: Zendcon 2007 Features

Oct 10, 2007 PHP Features You Didn't Know Existed

Soundex / Metaphone

$key = metaphone($word);

$key = soundex($word);

Metaphone is 'supposed' to be more accurate since it understands basic rules of English pronunciation, and generates keys of variable length.

Page 14: Zendcon 2007 Features

Oct 10, 2007 PHP Features You Didn't Know Existed

serialize() & unserialize()

$var = array('digg' => 1, 'life' => 42);$v = serialize($var);

$var = unserialize("a:2:{s:4:"digg";i:1;s:4:"life";i:42;}");

Page 15: Zendcon 2007 Features

Oct 10, 2007 PHP Features You Didn't Know Existed

Header buffering & headers_sent()

Ini File:output_buffering = 4096

if (!(headers_sent())) { setcookie('what', 'It Works');}

Page 16: Zendcon 2007 Features

Oct 10, 2007 PHP Features You Didn't Know Existed

@ symbol

$result = @function();

@$val += 1;

$whatever = @$_GET['v'] ? $_GET['v'] : 'default';

Page 17: Zendcon 2007 Features

Oct 10, 2007 PHP Features You Didn't Know Existed

@ vs. isset() vs. array_key_exists()

@$var['a']['b']

isset($var['a']['b'])

array_key_exists('b', $var['a'])

Page 18: Zendcon 2007 Features

Oct 10, 2007 PHP Features You Didn't Know Existed

trigger_error()

trigger_error("Whoops", E_USER_ERROR);

Page 19: Zendcon 2007 Features

Oct 10, 2007 PHP Features You Didn't Know Existed

Direct access of globals - $GLOBALS

$GLOBALS['myvar']

Page 20: Zendcon 2007 Features

Oct 10, 2007 PHP Features You Didn't Know Existed

fscanf() using character sets

fscanf($file, "%3s*%40[^\n] %f\n", $a, $b, $c);

fscanf($file, "%3[a-c]\t%6[0-9]\n", $a3, $a6);

fscanf($file, ' "[^"]" "[^"]" ');

Page 21: Zendcon 2007 Features

Oct 10, 2007 PHP Features You Didn't Know Existed

do {} while (false);

do { if (something()) { break; } somethingelse();} while (false);

Page 22: Zendcon 2007 Features

Oct 10, 2007 PHP Features You Didn't Know Existed

php://input & php://stdin

Command line input:file_get_contents('php://stdin');

Raw Post data:file_get_contents('php://input');

Page 23: Zendcon 2007 Features

Oct 10, 2007 PHP Features You Didn't Know Existed

php://filter

file_put_contents('php://filter/write=string.toupper|string.rot13/resource=myfile.rot');

readfile('php://filter/read=string.strip_tags/resource=http://digg.com');

var_dump(stream_get_filters());

Page 24: Zendcon 2007 Features

Oct 10, 2007 PHP Features You Didn't Know Existed

compress.zlib://

file_put_contents('compress.zlib://gzipfile.gz', 'DATA');

readfile('compress.zlib://gzipfile.gz');

Page 25: Zendcon 2007 Features

Oct 10, 2007 PHP Features You Didn't Know Existed

Generating lists of months/weekdays

foreach (range(1,12) as $month) { $daystamp = mktime(0, 0, 0, $month);

$monthnames[] = date('F', $daystamp);}

foreach(range(1,7) as $day) {$stamp = mktime(0, 0, 0, 1, $day);$days[date('N', $stamp)] = date('l', $stamp);

}

Page 26: Zendcon 2007 Features

Oct 10, 2007 PHP Features You Didn't Know Existed

Including code from other servers

include 'http://example.com/file.inc';

Page 27: Zendcon 2007 Features

Oct 10, 2007 PHP Features You Didn't Know Existed

Possible Other Topics

• Output Buffering• SPL Iterators• Assertions• Exif

Page 28: Zendcon 2007 Features

Oct 10, 2007 PHP Features You Didn't Know Existed

Digg is Hiring!

Digg.com is hiring experienced PHP programmers!

http://digg.com/[email protected]

Page 29: Zendcon 2007 Features

Oct 10, 2007 PHP Features You Didn't Know Existed

Any Questions?

For this presentation and more:

http://eliw.com/

Visit http://digg.com/