Photo: Old School

My Student Identity Card from elementary school. Yes, I had terrible hair back then.

Photo: Toronto City Centre Airport

Scenes from the airport on May 24, 2011 before heading to New York.

Posted from Toronto, Ontario, Canada.

PHP Utility Functions: get_value_or_default

Some handy functions I use to grab values from arrays and objects (especially $_REQUEST and friends), without having to constantly run isset or !empty checks everywhere (to avoid undefined index notices). You can specify a default value if the index doesn’t exist in the array or object. Also lets you sanitize the value(s) using a callback before returning them.

Note: No amount of helper functions will protect you from Bad Things. Always validate/sanitize untrusted data.


$var ) ? $object->$var : $default;
	elseif( is_array( $object ) )
		$value = ! empty( $object[$var] ) ? $object[$var] : $default;
	else
		$value = $default;

	if( is_callable( $sanitize_callback ) ) {
		if( is_array( $value ) )
			$value = array_map( $sanitize_callback, $value );
		else
			$value = call_user_func( $sanitize_callback, $value );
	}

	return $value;
}

function get_request_var( $var, $default = '', $sanitize_callback = '' ) {
	return get_value_or_default( $var, $_REQUEST, $default, $sanitize_callback );
}
function get_get_var( $var, $default = '', $sanitize_callback = '' ) {
	return get_value_or_default( $var, $_GET, $default, $sanitize_callback );
}
function get_post_var( $var, $default = '', $sanitize_callback = '' ) {
	return get_value_or_default( $var, $_POST, $default, $sanitize_callback );
}



Posted from Mississauga, Ontario, Canada.