User login

php

PHP Arrays plus symbol (+) syntax

http://www.php.net/manual/en/language.operators.array.php

With the $array1 + $array2 syntax, anything with a text key in $array1 is kept, at the expense of anything with the same key in $array2.

PHP warning Unable to load dynamic library sqlite.so

Getting this every time i run Drush or anything else PHP on the command line:

PHP Warning: PHP Startup: Unable to load dynamic library '/usr/lib/php5/20090626/sqlite.so' - /usr/lib/php5/20090626/sqlite.so: cannot open shared object file: No such file or directory in Unknown on line 0

Fix:

sudo rm /etc/php5/conf.d/sqlite.ini

Strip non-numeric characters from a string with PHP Perl-style regular expression

<?php
$numbers_only_string = preg_replace('/\D/', '', $string);
?>

Yeah, that there's a regexp character for "not a digit". Craziness.

Use echo instead of print in themes for speed?

Hat tip to RobLoach who noted this in the #drupal IRC channel-- if we want to squeeze hundredths of a millisecond more out of our Drupal modules and themes, we should use echo instead of print for getting the data to the screen.

UPDATE: The PHP developers don't recommend switching from print to echo for speed reasons, suggesting that the difference can be dependent on the setup of your environment.

Random more useful PHP code speed improvements:

Remove a trailing slash (if any) from a string with PHP

php remove traling slash
php remove last character
http://us2.php.net/manual/en/function.rtrim.php

From a line in template.php that also takes out leading http:// to present a more human-friendly web address.

<?php
    $display = rtrim(str_replace('http://', '', $url), " /");
?>

Install and enable pspell for Apache with PHP5

This is a package requested by, at the least, the Coder Tough Love Drupal module.

Searched around for a fix to the error message on the coder tough love settings page that there was nothing to configure because it didn't have the pspell package installed, nothing at results for ubuntu pspell, enable pspell, php5-pspell, enable pspell linux said exactly what to do.

Naturally...

sudo apt-get install php5-pspell
sudo /etc/init.d/apache2 restart

is all you need on Debian / Ubuntu.

Convert an associative array to variables with the names of the keys and the matching values

php turn an array into variables matching keys
php associative array to variables

The function you want is

<?php
extract($array);
?>

http://us.php.net/extract

You can also do it the hard way.

Syndicate content