Php

PHP Magic Constants: __LINE__, __FILE__, __DIR__, __FUNCTION__, __CLASS__, __METHOD__, and __NAMESPACE__

I’ve been using __FILE__ for years, but I never thought to look for its siblings.

echo ' line:'. __LINE__ .' file:'. __FILE__ .' directory:'. __DIR__ .' function:'. __FUNCTION__ .' class:'. __CLASS__ .' method:'. __METHOD__ .' namespace:'. __NAMESPACE__;

I feel as though I should have noticed these earlier; they’re clearly referenced in the docs for debug_backtrace(), after all.

Scaling PHP

This two year old post about Rasmus Lerdorf’s PHP scaling tips (slides) is interesting in the context of what we’ve learned since then. APC now seems common, and it’s supposedly built-in to PHP6. Still, I’d be interested in seeing an update. Are MySQL prepared statements still slow?

And that’s where Rasmus’ latest presentation comes in. We don’t learn anything about MySQL prepared statements, but we do learn how to find choke points in our applications using callgrind and other tools. In his examples, he can do a little over 600 transactions per second with both static HTML and simple PHP, but various frameworks — with many inclusions and function calls — can slow that to under 50 transactions per second (I suppose they’d explain that in a TPS report).

Mark Jaquith On WordPress Security For Plugin Developers

I’ve been pretty aware of the risks of SQL injection and am militant about keeping my database interactions clean. Mark Jaquith today reminded me about the need to make sure my browser output is filtered through clean_url(), sanitize_url(), and attribute_escape(). Furthermore, we all need to remember current_user_can(), check_admin_referer(), and nonces. » about 100 words

Is My PHP Script Running Out Of Memory?

I’ve got a PHP script that sometimes just dies with no errors to the browser and no messages in the error log. I’ve seen this in the past with scripts that consumed too much memory (yeah, it should have issued an error, but it didn’t, and increasing the memory limit fixed it), but now the memory limit is set pretty high and I’m not sure I want to increase it further. I certainly don’t want to increase it without seeing where it’s going wrong, anyway.

To do that, IBM developerWorks says the memory_get_usage() and memory_get_peak_usage() functions are for me. And they offer some other interesting tips as well.

Web Development Languages

David Cloutman pointed to Craiglist’s job ads as an indicator of programming language popularity. Here’s the hit counts for “web design jobs” and “internet engineering jobs” in the Bay Area:

 PHPJavaRubyPythonPERL
internet engineering jobs1672468598109
web design jobs11071221931

Cloutman has a few ideas for what the numbers mean, but I’m just entertained by the data. (Note: he corrected his original numbers.)

JSON on RHEL & PHP 5.1.6

Stuck with PHP 5.1.6 on RHEL or even CentOS (and a sysadmin who insists on using packages)? Need JSON? I did. The solution is easy:

yum install php-devel<br /> pecl install json

The pecl install failed when it hit an 8MB memory limit, and I was clueless about how to fix it until I learned that the pecl installer ignores the php.ini. Turns out the best solution is to use the pear installer (which does follow php.ini settings):

pear install pecl/json

Parse HTML And Traverse DOM In PHP?

I spoke of this the other day, but now I’ve learned of PHP’s DOM functions, including loadHTML(). Use it in combination with simplexml_import_dom like this:

``` $dom = new domDocument; $dom->loadHTML('
  • one
  • two
  • three
    • sublist item
'
); if($dom){ $xml = simplexml_import_dom($dom); print_r($xml); } ```

This IBM developerWorks article has some more useful info.

Here’s some code I prototyped to parse out the ISBNs and LCCN (or any data, really) from an an average record in Scriblio:

``` $dom = new domDocument; $dom->loadHTML($content); if($dom){ $xml = simplexml_import_dom($dom); } foreach($xml->body->ul->li as $thing){ if($thing['class'] == 'isbn'){ foreach($thing->ul->li as $stuff){ print_r($stuff); } } if($thing['class'] == 'lccn'){ foreach($thing->ul->li as $stuff){ print_r($stuff); } } } ```

Apache, MySQL, and PHP on MacOS X

p0ps Harlow tweeted something about trying to get an AMP environment running on his Mac. Conversation followed, and eventually I sent along an email that look sorta like this: If you’re running 10.4 (I doubt it, but it’s worth mentioning because I’m most familiar with it), here’s how I’ve setup dozens of machines for web […] » about 300 words