In classic WordPress themes, you can put this code in your functions.php file, then you can add php code directly into Text widgets.
function php_in_widgets($html){ if(strpos($html,"<"."?php")!==false){ ob_start(); eval("?".">".$html); $html=ob_get_contents(); ob_end_clean(); } return $html; } add_filter('widget_text','php_in_widgets',100);
For example:

Note that you’ll want to be sure to use the [Text] tab, not [Visual].