PHP/MySQL ->
Simple UDF (User Defined Function) in MySQL

This UDF function returns delivery price depending on customer's location:
USE colombo;

DROP FUNCTION IF EXISTS fn_getdeliveryprice;

CREATE FUNCTION fn_getdeliveryprice
   (p_country Varchar(50), p_city Varchar(50)) RETURNS Float
BEGIN
DECLARE v_price Float;
/*
for not identified customer's location delivery price is 45 euro
*/
SET v_price = 45;

IF p_country = 'iceland' THEN
   SET v_price=8.15;
END IF;
IF p_country = 'poland'
  IF p_city = 'warsaw'
   SET v_price=7.85;
  ELSE
   SET v_price=9.75;
  END IF;
END IF;

/* ... here more price options can be placed ... */

RETURN v_price;

END


Then we can call this function using single SELECT:

SELECT fn_getdeliveryprice('poland', 'warsaw');

Reciving in the message window: 9.75

Or call function for every row in SELECT statement:
SELECT CustomerID, CustomerName,
fn_getdeliveryprice(Country, City) AS DelivPay
FROM Customers
WHERE CustomerID BETWEEN 10 AND 12;

The results will came as following:
CustomerIDCustomerNameDelivPay
10Erika Nass8.14
11Somerset Dogan45
12Famke Bacher45


sqlexamples.info