PHP Fatal error: Can’t use method return value in write context

November 19th, 2010 1 comment

I came across the following error recently when trying to evaluate in PHP whether a value was empty:

Fatal error: Can't use method return value in write context

It turns out that the PHP empty function cannot accept the return value from another function so you have to first define the variable before passing it to the empty() function.

// will generate a fatal error
empty(nl2br($string));
empty($obj->getSomeValue());

// will work
$val = nl2br($string);
$val = $obj->getSomeValue();
empty($val);

Alternatively, you can use the following function instead:

/**
 * Determines whether a variable is considered to be empty,
 * additionally accepts returned values from functions
 *
 * @param mixed	$val	The value to evaluate
 * @return boolean
 */
function empty_return($val) {
	return empty($val);
}

// example
empty_return(nl2br($string));
Categories: PHP Tags:

Firebug console wrapper

November 9th, 2010 No comments

The Firebug console is extremely useful when trying to debug javascript. However, for users that don’t have Firebug installed or are using a browser without a built in console, you either have to delete all logging from your scripts before publishing them or create a “dummy” console object to prevent errors.

A more convenient method is to create a wrapper for the console that you can call instead. Read more…

Categories: Firefox, Javascript Tags:

Automatically Collapse FirePHP Responses

November 3rd, 2010 1 comment

FirePHP is a Firefox extension that allows you to log directly to the Firebug console from PHP. This is great for AJAX applications because you can debug your server side scripts without interferring with the responses.

Each script that returns a FirePHP log will generate it’s own group in the Firebug console which is expanded by default which means if you are logging a lot of data on the server side (such as database queries) then there will be a lot of data displayed in the console which you may not want to see all the time.

My solution to this was to edit the FirePHP extension so that each script response is collapsed automatically. You can then click a response to expand it if you want to view more details. Read more…

Categories: Firefox, Javascript, PHP Tags:

Alternative DATEDIFF for MySQL

October 26th, 2010 No comments

The DATEDIFF() function was added in MySQL version 4.1.1. For earlier versions that do not support this function you can make a work-around for it using the TO_DAYS function:

SELECT TO_DAYS(date1) - TO_DAYS(date2);

-- example
SELECT TO_DAYS('2010-01-03') - TO_DAYS('2010-01-01'); -- returns '2'

This will give the same effect as DATEDIFF(date1, date2) by converting each date to the number of days since year 0 and subtracting them.

Note: According the MySQL manual, TO_DAYS() is not intended for use with dates that precede the year 1582 because it does not take into account the days that were lost when the Gregorian calendar was introduced so using this method is not advisable for dates earlier than this.

Categories: MySQL Tags:

Blarg!

October 26th, 2010 1 comment

blarg.co.uk is back!

Categories: Uncategorized Tags: