This is a simple way to get a subset of associative array elements using an array of keys; basically you extract out the elements you want from a bigger array by listing the keys you need. Continue reading “Find subset of associative array in PHP”
Tag: php
Convert Large Integer to Hexadecimal without PHP Math extension
PHP has a couple of functions to convert integers to hexadecimal, dechex() and base_convert(). However, both functions lose precision on large integers.
Continue reading “Convert Large Integer to Hexadecimal without PHP Math extension”
Determine if a row has recently been updated using MySQL
This is a fairly common case I think. You have a table that has a datetime field that gets updated when a row is updated – you need to determine if a row has been updated recently.
Continue reading “Determine if a row has recently been updated using MySQL”
Loop through dates with strtotime() function
This simple method allows you to start at a date and iterate by either days or months until you reach the end date using my favourite PHP function, strtotime().
Continue reading “Loop through dates with strtotime() function”
Php Unserialize string after non UTF8 characters stripped out
This may be a pretty rare problem but I’ll post it regardless.
Continue reading “Php Unserialize string after non UTF8 characters stripped out”
Get last line of file in PHP
Here is a simple way to get the last line of a file without reading the entire file into memory.
Continue reading “Get last line of file in PHP”
How to debug your WordPress plugin
If you are developing a plugin on WordPress, you will need to debug your code as you go.
To enable debugging, go to your wp-config.php file.
Find the line…
define('WP_DEBUG', false);
Replace the line above with the following…
// Turns WordPress debugging on define('WP_DEBUG', true); // Tells WordPress to log everything to the /wp-content/debug.log file define('WP_DEBUG_LOG', true); // Doesn't force the PHP 'display_errors' variable to be on define('WP_DEBUG_DISPLAY', false); // Hides errors from being displayed on-screen @ini_set('display_errors', 0);
Now you all warnings and errors will show up in the /wp-content/debug.log file, including WordPress warnings of deprecated functions.
You can write directly to this log from your plugin using the error_log()
function.
Typically…
//output some debug string error_log( 'this works yo' ); //output some array/object error_log( print_r( $some_obj_or_array, 1 ) );
Kudos to this post. It has some good plugin development tips, including how to enable debugging on WordPress.
WP Plugins: How to remove a Filter
Do not use
remove_filter()
. It is possible it will break other hooks which can have unintended consequences. This post gives a really good alternative approach using static variables.
Worth to know for wordpress plugin authors: Making your plugin to safely unregister or remove a hook (filter or action) is not possible with the wordpress plugin API. Why? you might ask yourself. Even the name of the remove_filter() function suggests it and the codex documentation does say so as well.
But: It does not work, because this function does not work as intended. And beyond your own hooks, it has the potential to hinder the blog from working properly as it has the potential to remove other hooks then the specified one.
Does not is not exactly right, it does, but only sometimes. That’s specifically a bummer if you write plugins yourself, because you can not safely rely on it. For example, if running your plugin makes use of PHP 5.2 or greater it works. But it does not if it’s running on PHP 4 or PHP 5 lower…
View original post 1,582 more words
Search Associative Array with Wildcard in PHP
Here are a pair of functions that will allow you to search an associative array for either a key or a value, using a string with a wildcard (*).
Continue reading “Search Associative Array with Wildcard in PHP”
Parse Print_r output and convert into CSV
These days I seem to looking a lot a print_r output. For the uninitiated, print_r is a handy PHP function to allow you to print the contents of an array in a neat, readable format. Continue reading “Parse Print_r output and convert into CSV”