Returning NULL by reference

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
llfitness
Forum Newbie
Posts: 12
Joined: Mon Feb 20, 2006 3:28 pm

Returning NULL by reference

Post by llfitness »

I'm writing a handler for all my database calls. Because of the possibility that the function might be returning a LOT of data, I want it to be as fast as possible. In college I learned that, as long as the originating function wasn't going to be using a value any more, returning by reference was faster than returning by value.

- Is this true for PHP?
- If so, what is the "proper" way to return NULL, if necessary, by reference?

Although not necessarily relevant to my question, here is the code I am using for the db_handler. Note that I am using an error handler to collect information. Anything

Code: Select all

function &db_call($sql, $return_type)
	{
	// defined return types:
	// none: SQL_RESULT_NONE
	// single: SQL_RESULT_SINGLE
	// set: SQL_RESULT_SET
	// object: SQL_RESULT_OBJECT 
	
	global $db;

	// trigger_error("Looking closely at DB call:\n\n" . $sql . "\n\n" . 
		urldecode($sql), E_USER_NOTICE);

	$result = $db->Execute($sql);

	if ($result === FALSE)
		{
		trigger_error($db->ErrorMsg(), E_USER_ERROR);
		}
	elseif ($return_type == SQL_RESULT_NONE)
		{
		assert($result->RecordCount() == 0);
		return 0;
		}
	elseif ($return_type == SQL_RESULT_SINGLE)
		{
		if ($result->RecordCount() == 1)
			{
			return $result->fields;
			}
		else
			{
			return NULL;
			}
		}
	elseif ($return_type == SQL_RESULT_SET)
		{
		if ($result->RecordCount() > 0)
			{
			return $result->GetRows();
			}
		else
			{
			return NULL;
			}
		}
	elseif ($return_type = SQL_RESULT_OBJECT)
		{
		return $result;
		}
	else
		{
		trigger_error('This is an unexpected condition. 
			Check the second arguement of db_call... 
			or there may be other causes', E_USER_ERROR);
		return NULL; // this code will probably not happen.
		}
	} // END function db_call(...)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

set the variable to null, then return that.
Post Reply