php

Building GEOS on CentOS

It should be simple, but I ran into a number of errors.

First I got stuck on libtool: line 990: g++: command not found. It turns out I needed to install g++ using:

yum install gcc-c++

Then I got stuck on this one:

platform.h:110:2: error: #error "Can not compile without isnan function or macro
[...]
"Coordinate.inl:38: error: ‘ISNAN’ was not declared in this scope

The author of this page faced the problem, but the real insight came from this bug report on an unrelated project:

In these versions of g++ isnan is defined as a macro in <math.h> unless is included, in which case std::isnan() is defined instead.

And so I tried replacing #include <math.h> with #include <cmath> in include/geos/platform.h. Amazingly, that worked.

Testing apply_filters() times

Testing how long it takes to assign a variable versus assigning through WordPress’ apply_filters(). Filters are core to WordPress, but I haven’t yet looked at the total number of apply_filters() calls used throughout the code. The answer to this question is that calling a non-existing filter before assignment is about 21 times more costly than […] » about 300 words

Testing file include times for a file that may or may not exist

Question: Should you check for a file before attempting to include it, or just suppress errors? Calling file_exists requires stating it twice if the file does exist, so that could take longer. Answer: the file_exists pattern is more than five times faster than the @include pattern for a file that doesn’t exist, and not substantially […] » about 300 words

On wp_enqueue_scripts and admin_enqueue_scripts

An argument has erupted over the WordPress actions wp_enqueue_scripts and admin_enqueue_scripts vs. init. One of the points was about specificity, and how wp_enqueue_scripts and admin_enqueue_scripts can reduce ambiguity. I didn’t realize I had strong opinions on it until the issue was pressed, but it turns out I think wp_enqueue_scripts and admin_enqueue_scripts are unnecessary and unfortunate additions […] » about 300 words

Confirming that object references in arrays are preserved while cloning the arrays

A short test to confirm references are preserved in cloned arrays. The result is: Now let’s mess with one piece of that to check if the object was passed by reference or got cloned: Confirmed, the object is passed by reference, even though the array that contained it was cloned: » about 300 words

PHPQuery

I have Matthew Batchelder to thank for introducing me to PHPQuery. I haven’t used it yet, but someday I’ll have need to select text elements from HTML using the PHP5 PEAR module. From the description “server-side, chainable, CSS3 selector driven Document Object Model (DOM) API based on jQuery JavaScript Library.”

Why PHP’s RegEx Is Slow, And What You Can Do About It (if you happen to be a committer on the PHP project)

Regular Expression Matching Can Be Simple And Fast, by Russ Cox:

Perl [and PHP and others] could not now remove backreference support, of course, but they could employ much faster algorithms when presented with regular expressions that don’t have backreferences.

How much faster? About a million times (no, I do not exaggerate).

I use a lot of regular expressions, and relatively few of them use backreferences. It’d be worth optimizing.

Lessons Learned: Why It’s Better Not To Use Parentheses When They’re Optional

There it is in the PHP manual for return():

Note: since return() is a language construct and not a function, the parentheses surrounding its arguments are not required. It is common to leave them out, and you actually should do so as PHP has less work to do in this case.

I knew the parentheses were optional, but I’ve been merrily using them all along. And I probably would have continued doing so until I saw the second note attached to the docs:

Note: You should never use parentheses around your return variable when returning by reference, as this will not work. You can only return variables by reference, not the result of a statement. If you use return ($a); then you’re not returning a variable, but the result of the expression ($a) (which is, of course, the value of $a).

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).

Installing PHP APC On RHEL/CentOS

  1. Yum up some packages:
    ```

    yum install php-pear php-devel httpd-devel

              </td>
            </tr>
          </table>
        </div>
    
      2. Install APC using pear (the pear installer is smarter than the pecl installer):
      
        When the installer asks about APXS, say ‘no’. </p> <div class="wp_syntax">
          <table>
            <tr>
              <td class="code">
                ```
    pear install pecl/apc
    
          </td>
        </tr>
      </table>
    </div>
    
    1. Tell PHP to load APC:
      ```

      echo extension=apc.so > /etc/php.d/apc.ini

                </td>
              </tr>
            </table>
          </div>
      
        4. Restart Apache: <div class="wp_syntax">
            <table>
              <tr>
                <td class="code">
                  ```
      /sbin/service httpd graceful
      
            </td>
          </tr>
        </table>
      </div>

      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:

      <td>
        PHP
      </td>
      
      <td>
        Java
      </td>
      
      <td>
        Ruby
      </td>
      
      <td>
        Python
      </td>
      
      <td>
        PERL
      </td>
      
      <td>
        167
      </td>
      
      <td>
        246
      </td>
      
      <td>
        85
      </td>
      
      <td>
        98
      </td>
      
      <td>
        109
      </td>
      
      <td>
        110
      </td>
      
      <td>
        71
      </td>
      
      <td>
        22
      </td>
      
      <td>
        19
      </td>
      
      <td>
        31
      </td>
      
      <td>
      </td>
      
       
      internet engineering jobs
      web design jobs

      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

      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

      Speedy PHP: Intermediate Code Caching

      I’ve been working on MySQL optimization for a while, and though there’s still more to done on that front, I’ve gotten to the point where the the cumulative query times make up less than half of the page generation time. So I’m optimizing code when the solution is obvious (and I hope to rope Zach […] » about 500 words

      PHP Libraries for Collaborative Filtering and Recommendations

      Daniel Lemire and Sean McGrath note that “User personalization and profiling is key to many succesful Web sites. Consider that there is considerable free content on the Web, but comparatively few tools to help us organize or mine such content for specific purposes.” And they’ve written a paper and released prototype code on collaborative filtering.

      Vogoo claims to be a “a powerful collaborative filtering engine that allows Webmasters to easily add personalization features to their Web Sites.”

      Parsing MARC Directory Info

      I expected a record that looked like this: LEADER 00000nas 2200000Ia 4500 001 18971047 008 890105c19079999mau u p 0uuua0eng 010 07023955 /rev 040 DLC|cAUG 049 PSMM 050 F41.5|b.A64 090 F41.5|b.A64 110 2 Appalachian Mountain Club 245 14 The A.M.C. White Mountain guide :|ba guide to trails in the mountains of New Hampshire and adjacent parts […] » about 600 words