With the addition of the free “Insert PHP Code Snippet” plugin by xyzscripts.com, you can put this code in your functions.php file, then you can add php code directly into Text widgets.
This will work with classic themes and block themes such as Twenty Twenty-Two.
To insert PHP, select XYZ PHP Code (which is not the same name as the snippet), on the left of the WordPress dashboard, then click PHP Code Snippets. Write your code, then you’ll see a shortcode that you can insert in any page or post.
Note that as of this writing, the plugin had not been updated in more than three months.
Side Note: It is possible this plugin could be discontinued, or changed at some point in the future.
I used to use PHP_Everywhere, a similar plugin. One day, the maker of that plugin pushed an update that no longer works with Classic Editor builds. That caused a need to manually add my PHP code in an entirely different way to several websites.