if you wanna use a variable in it, get rid of the ' ' around it.
If you have double quotes " " variables and escape characters will be interpreted, if you use single quotes ' ' its just treated as a normal string with no special processing.
And also, variables dont ever need to be enclosed in quotes if you wish to echo them or use them in a function call.
So
echo asc2hex($search, '%'); //will work and is preferred
and
echo asc2hex("$search", '%'); //will work, isnt really the best practice unless you want to use other words in there like "$search was the search phrase"
and
echo asc2hex('$search', '%'); //will actually encode $, s, e, a, r, c, h