Press "Enter" to skip to content

Introducing the New Type Descriptors in the Zend Parameters Parser

Since PHP 5.3, zend_parse_parameters gained new descriptors: f (callback, returns zend_fcall_info + cache), H (array or HASH_OF(object) — lets an object be accepted where an array is expected), L (clamped long), Z (raw zval**), and * / + (0-or-more / 1-or-more variadic args). Cuts down the manual validation you used to do for callbacks and objects-as-arrays.

Filed in English, PHP应用, PHP源码分析
with 0 Comment

New Features in PHP 5.4

A PHP 5.4 alpha1 feature roundup: Traits (lightweight method reuse via 'use', with fixed conflict-resolution rules), array dereferencing ($a = explode(...)[0], and even lvalue use), DTrace support, plus the new built-in Webserver SAPI (php -S localhost:8000). Also notes the removals: break/continue $var, register_globals, etc.

Filed in English, PHP应用, 随笔
with 0 Comment

PHP Performance Optimization

Laruence's four takeaways from a PHP performance talk: (1) optimization advice is about nipping problems in the bud while coding, not a rulebook to apply after a problem appears; (2) advice is a guard against abuse, not a law — e.g. batch assign() beats repeated __set magic calls; (3) advice is language-specific — an interpreted language does none of the compiler optimizations C does, and every symbol costs a Hash Lookup; (4) dig to the bottom — one high-level statement may compile to hundreds of lines of C.

Filed in English, PHP应用, 随笔
with 0 Comment

Setting a Query Timeout for MySQL

Can you cap a slow query so it doesn't blow out PHP's max_execution_time? Yes — libmysql's MYSQL_OPT_READ_TIMEOUT (value 11, read from the enum), but only mysqli exposes mysql_options() to PHP, so you pass the numeric constant directly: $mysqli->options(11, 1). Laruence's catch: libmysql retries twice, so the effective timeout is 3x what you set — the practical floor is 3s.

Filed in English, MySQL/PostgreSQL, PHP应用, PHP源码分析
with 0 Comment

Using fastcgi_finish_request to Speed Up Page Responses

PHP FPM's fastcgi_finish_request() ends the client response and closes the connection but lets the PHP script keep running server-side — use it to defer logging, transcoding, or other post-render work off the critical path. Laruence's note: it's cleaner than flush() for this, and a function_exists() shim keeps the code portable to non-FPM environments.

Filed in English, PHP应用, 转载
with 0 Comment

PHP Internals — A Low-Probability Notice in Session GC

The occasional 'ps_files_cleanup_dir: opendir(/var/lib/php5) failed: Permission denied' Notice on apt/Debian PHP. Laruence traces it to the file_handler save handler's GC running on session_start: the default session dir is 733+sticky (root-owned, writable-but-not-listable by non-root workers), so s_gc's opendir() fails and fires an E_NOTICE. The check is rand < gc_probability/gc_divisor (default 1/100), so it's rare. Fix: set session.gc_probability=0 (Debian handles cleanup via an external cron), or fix the dir permissions.

Filed in English, PHP应用, PHP源码分析
with 0 Comment

Some PHP Coding Tips [last updated 2011-04-02]

Laruence's 17 hand-picked PHP performance/correctness tips: list() to skip explode() segments, NULL === over is_null, prefer === over ==, avoid continue, watch out for loose comparison in switch/in_array (strval the value, or pass a 3rd arg), use switch(TRUE) for if/elseif chains, define before use (undefined vars are 8x+ slower), swap without a temp, ~~ is ~3x faster than floor (but overflows on big numbers), do{}while(0) blocks, minimize @, avoid recursion, $_SERVER['REQUEST_TIME'] over time(), hoist strlen() out of the for-condition, prefer strncasecmp/strpbrk/stripos over regex, brace variables in double-quoted strings, and use FALSE for errors vs NULL for 'not found'.

Filed in English, PHP应用, 随笔
with 0 Comment