passing variables from SMARTY to PHP or the reverse

I need to communicate the value of a variable between smarty & php.



Essentially I am setting up a block of PHP code within a smarty template file. The code will check for a URL parameter (called hash).



If the parameter does not exist, it will set the value of a PHP variable called searchterm equal to the value of the {$search.q} smarty variable.



If the parameter does exist, it will set the value of the PHP variable called searchterm equal to the value of the php variable called hash.



I am placing my php code block within the customers/views/products/search template file.



Here is my sample code:


{php}
//DECLARE THE VARIABLES
$hash = $_GET['features_hash']; //the feature searched for, collected from the URL parameter

//CHECK WHETHER THE SEARCH IS A REGULAR ONE OR A FEATURES FILTER RESULT PAGE
if ($hash=="") //check if it is a regular search, regular searches have no hash value
{

$searchterm=$search.q; //this should get the value from the smarty variable {$search.q}

}
elseif ($hash=="V39") //check for age filter
{
$searchterm="V39searchphrase";
}
{/php}






I reason that I can rewrite my code above to work with either of the two approaches:



Take a php variable and pass it’s value to a Smarty variable. For example:

Take the value of $term=“fruit”; ( a php variable) and make it a smarty variable that can be used as {$term}



Take a Smarty Variable and pass its value to a PHP variable. For example:

Take the smarty variable {$search.q} and make the PHP variable $searchq equal to {$search.q}'s value. I would be doing this .



Any suggestions on how to make my sample code above work, or a detailed example of passing variables from PHP to smarty or vice versa would be greatly appreciated.



My ultimate goal is to include a different tracking script in the page for a regular search versus a filter search. The search template shows the results for both types of searches, hence I need to show use tracking code if the URL has the &features_hash= parameter and another tracking script if it does not. In addition, because the cart uses separate values for each filter you can “shop by”, I must capture the value of &features_hash to a variable I can use with the tracking script.

I avoided the need to solve the issue by using smarty’s if/else to include different tracking snippets based on a variable’s value.