This is similar question to [MySQL and PHP - insert NULL rather than empty string](https://stackoverflow.com/questions/4620391/mysql-and-php-insert-null-rather-than-empty-string) but I'm still encountering the problem.

I'm trying to create a function so that an empty string is inserted as a NULL into MySQL.

I create the function IsEmptyString:

```
function IsEmptyString($val){
    if (trim($val) === ''){$val = "NULL";}
}

```

Before inserting the the variable, I escape it and then I call the function above. I've also tried `$val = NULL;`

What am I doing wrong? Also should I call the function before escaping?